Skip to content

Commit

Permalink
Dark Mode Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
KMaragh committed Aug 26, 2020
1 parent 5778008 commit 5539097
Show file tree
Hide file tree
Showing 39 changed files with 338 additions and 220 deletions.
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
Expand All @@ -57,6 +60,8 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.browser:browser:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

testImplementation 'junit:junit:4.13'
Expand All @@ -69,4 +74,6 @@ dependencies {
implementation "io.github.ponnamkarthik:richlinkpreview:1.0.9"
implementation "com.squareup.picasso:picasso:2.71828"

implementation "androidx.preference:preference:1.1.1"

}
12 changes: 6 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".STNApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:largeHeap="true"
android:usesCleartextTraffic="false">
<activity
android:name=".activity.DetailsActivity"
android:theme="@style/AppTheme2">

</activity>
<activity android:name=".activity.PetitionDetailsActivity" android:theme="@style/AppTheme2"/>
android:theme="@style/AppTheme2"/>
<activity
android:name=".activity.PetitionDetailsActivity"
android:theme="@style/AppTheme2" />
<activity
android:name=".activity.MainActivity"
android:theme="@style/AppTheme2">
Expand All @@ -37,5 +38,4 @@
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>
18 changes: 18 additions & 0 deletions app/src/main/java/io/saytheirnames/STNApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.saytheirnames

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager

class STNApplication : Application() {

override fun onCreate() {
super.onCreate()
initTheme()
}

private fun initTheme() {
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
ThemeManager.applyTheme(preferences.getInt(R.string.preference_key_theme.toString(), AppCompatDelegate.MODE_NIGHT_NO))
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/io/saytheirnames/ThemeManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.saytheirnames

import android.content.Context
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager

object ThemeManager {

fun applyTheme(mode: Int) {
if (AppCompatDelegate.getDefaultNightMode() == mode) return
AppCompatDelegate.setDefaultNightMode(mode)
}

fun setTheme(mode: Int, context: Context) {
applyTheme(mode)

val preferences = PreferenceManager.getDefaultSharedPreferences(context)
preferences.edit().putInt(R.string.preference_key_theme.toString(), mode)
.apply()
}

fun isNightMode(): Boolean {
return getCurrentTheme() == AppCompatDelegate.MODE_NIGHT_YES
}

private fun getCurrentTheme(): Int {
return AppCompatDelegate.getDefaultNightMode()
}

fun toggleTheme(context: Context) {
if (isNightMode()) setTheme(AppCompatDelegate.MODE_NIGHT_NO, context)
else setTheme(AppCompatDelegate.MODE_NIGHT_YES, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.saytheirnames.network.Utils;
import io.saytheirnames.utils.CustomTabUtil;
import io.saytheirnames.utils.ShareUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.snackbar.Snackbar;
Expand All @@ -36,12 +37,12 @@

public class DonationDetailsActivity extends AppCompatActivity implements View.OnClickListener {

String identifier,image, title, desc, donationLink;
String identifier, image, title, desc, donationLink;

public static final String EXTRA_ID = "identifier";

private BlurImageView blurImageView;
private ImageView donationImage,close;
private ImageView donationImage, close;
private TextView donationTitle, subTitle, donationDesc, socialHashtags;
private Button donationButton;
private View progress;
Expand All @@ -54,14 +55,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donation_details);


initView();

Intent intent = getIntent();

identifier = intent.getStringExtra(EXTRA_ID);

loadData();
loadData();
}

@SuppressLint("StaticFieldLeak")
Expand Down Expand Up @@ -186,7 +183,6 @@ public void onClick(View v) {
case R.id.close:
finish();
break;

case R.id.imgShare:
share(donationLink);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class PetitionDetailsActivity extends AppCompatActivity implements View.O

private ImageView imgClose, imgShare;

private TextView txtName, txtDescription, txtShareThisPetition;
private TextView txtName, txtDescription;

private Button btnSignThisPetition;
private Button btnSignThisPetition, txtShareThisPetition;

private ImageView actualImage;
private BlurImageView blurImageView;
Expand Down Expand Up @@ -174,7 +174,6 @@ public void onClick(View v) {
visitPage(petitionLink);
break;
case R.id.btnShareThisPetition:

case R.id.imgShare:
share(petitionLink);
break;
Expand All @@ -184,7 +183,6 @@ public void onClick(View v) {
}
}


private void visitPage(String url) {
CustomTabUtil.openCustomTabForUrl(this, url);
}
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/io/saytheirnames/fragments/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
Expand All @@ -21,6 +23,7 @@
import java.util.ArrayList;

import io.saytheirnames.R;
import io.saytheirnames.ThemeManager;
import io.saytheirnames.activity.MainActivity;
import io.saytheirnames.adapters.FilterHomeAdapter;
import io.saytheirnames.adapters.HeaderCardRecyclerAdapter;
Expand Down Expand Up @@ -77,6 +80,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
initializeRecyclerView();
loadData();

initializeToolbar();
return mContent;
}

Expand All @@ -102,15 +106,16 @@ public int getSpanSize(int position) {
@NotNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup viewGroup, @NotNull LoadState loadState) {
return new RecyclerView.ViewHolder(progressBar){};
return new RecyclerView.ViewHolder(progressBar) {
};
}

@Override
public void onBindViewHolder(@NotNull RecyclerView.ViewHolder viewHolder, @NotNull LoadState loadState) {
if (loadState.equals(LoadState.Loading.INSTANCE)) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
}
});
Expand All @@ -124,6 +129,16 @@ private void loadData() {
peoplePager.loadPeopleFromPagination();
}

private void initializeToolbar() {
androidx.appcompat.widget.Toolbar toolbar = mContent.findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.fragment_home_menu);
toolbar.setOnMenuItemClickListener(item -> onOptionsItemSelected(item));
if (ThemeManager.INSTANCE.isNightMode())
toolbar.getMenu().findItem(R.id.theme).setIcon(R.drawable.night_theme_white_24dp);
else
toolbar.getMenu().findItem(R.id.theme).setIcon(R.drawable.light_theme_white_24dp);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Expand All @@ -143,4 +158,15 @@ public void onHeaderClick() {
((MainActivity) getActivity()).updateBottomNavBasedOnTag(tag);
}
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.theme:
ThemeManager.INSTANCE.toggleTheme(getContext());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/io/saytheirnames/utils/CustomTabUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ private static void openCustomChromeTab(Context context, Uri uri) {
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = intentBuilder.build();

intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.Primary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.PrimaryDark));

CustomTabActivityHelper.openCustomTab(context, customTabsIntent, uri, (activity, uri1) -> openWebView(context, uri1));
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/color/bottom_nav_color.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/OnSurface" android:state_checked="true" />
<item android:color="@color/Secondary" android:state_checked="false" />
</selector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/button_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<stroke android:color="@color/colorPrimary" android:width="1dp"/>
<stroke android:color="@color/Primary" android:width="1dp"/>
</shape>
</item>
</selector>
1 change: 1 addition & 0 deletions app/src/main/res/drawable/card_border.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:width="2dp"/>

<padding android:top="2dp"
android:left="2dp"
android:right="2dp"
android:bottom="2dp"/>
</shape>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/default_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:thickness="2dp"
android:useLevel="false">
<stroke android:width="1dp"
android:color="@color/colorSecondaryGrey"/>
android:color="@color/colorGrey"/>
<size android:height="2dp" />
</shape>
</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/donation_btn.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="@color/colorBlack"
<stroke android:color="@color/Primary"
android:width="2dp"/>

<padding android:top="2dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/find_out_btn_border.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<stroke android:color="@color/colorPrimary"
<stroke android:color="@color/Primary"
android:width="2dp"/>

<solid android:color="@android:color/white"/>
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/light_theme_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M20,8.69L20,4h-4.69L12,0.69 8.69,4L4,4v4.69L0.69,12 4,15.31L4,20h4.69L12,23.31 15.31,20L20,20v-4.69L23.31,12 20,8.69zM18,14.48L18,18h-3.52L12,20.48 9.52,18L6,18v-3.52L3.52,12 6,9.52L6,6h3.52L12,3.52 14.48,6L18,6v3.52L20.48,12 18,14.48zM12,6.5c-3.03,0 -5.5,2.47 -5.5,5.5s2.47,5.5 5.5,5.5 5.5,-2.47 5.5,-5.5 -2.47,-5.5 -5.5,-5.5zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"
android:fillColor="#ffffff"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/night_theme_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M20,8.69L20,4h-4.69L12,0.69 8.69,4L4,4v4.69L0.69,12 4,15.31L4,20h4.69L12,23.31 15.31,20L20,20v-4.69L23.31,12 20,8.69zM18,14.48L18,18h-3.52L12,20.48 9.52,18L6,18v-3.52L3.52,12 6,9.52L6,6h3.52L12,3.52 14.48,6L18,6v3.52L20.48,12 18,14.48zM12.29,7c-0.74,0 -1.45,0.17 -2.08,0.46 1.72,0.79 2.92,2.53 2.92,4.54s-1.2,3.75 -2.92,4.54c0.63,0.29 1.34,0.46 2.08,0.46 2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5z"
android:fillColor="#ffffff"/>
</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/selected_button_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<stroke android:color="@color/colorPrimary" android:width="1dp"/>
<solid android:color="@color/Primary"/>
<stroke android:color="@color/Primary" android:width="1dp"/>
</shape>
</item>
</selector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/selected_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:thickness="2dp"
android:useLevel="false">
<stroke android:width="1dp"
android:color="@color/colorAccent"/>
android:color="@color/OnBackground"/>
<size android:height="2dp" />
</shape>
</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/social_media_button.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<stroke android:color="@color/colorPrimary" android:width="1dp"/>
<stroke android:color="@color/Primary" android:width="1dp"/>
</shape>
</item>
</selector>
Loading

0 comments on commit 5539097

Please sign in to comment.