Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
refactor: Set night mode correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rickardzettervall committed Mar 23, 2020
1 parent fc77ec7 commit ecf327e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
android:required="false" />

<application
android:name="tech.zettervall.notes.BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_agenda"
android:label="@string/app_name"
Expand Down
22 changes: 4 additions & 18 deletions app/src/main/java/tech/zettervall/notes/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;

import org.parceler.Parcels;
Expand All @@ -18,28 +17,15 @@
*/
public abstract class BaseActivity extends AppCompatActivity {

protected boolean mEnableDarkTheme, mIsTablet;
protected boolean mNightMode, mIsTablet;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEnableDarkTheme = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getString(R.string.dark_theme_key), false);
mNightMode = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getString(R.string.dark_theme_key),
getResources().getBoolean(R.bool.defaultNightMode));
mIsTablet = getResources().getBoolean(R.bool.isTablet);

// Set Theme
setTheme();
}

/**
* Set App theme.
*/
public void setTheme() {
if (mEnableDarkTheme) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/tech/zettervall/notes/BaseApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tech.zettervall.notes;

import android.app.Application;
import android.content.SharedPreferences;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;

import tech.zettervall.mNotes.R;

public class BaseApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean nightMode = sharedPreferences.getBoolean(getString(R.string.dark_theme_key),
getResources().getBoolean(R.bool.defaultNightMode));
setTheme(nightMode);
}

private void setTheme(boolean nightMode) {
if (nightMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/tech/zettervall/notes/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void onCreate(Bundle savedInstanceState) {
mNavView.setNavigationItemSelectedListener(this);

// Set Navigation Drawer item background/text/icon color (for currently checked item)
if (!mEnableDarkTheme) { // Light Theme
if (!mNightMode) { // Light Theme
mNavView.setItemBackground(getResources().getDrawable(R.color.selector_navitem));
} else { // Dark Theme
mNavView.setItemTextColor(ColorStateListUtil.getNavigationDrawerNightColorStateList(this));
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/tech/zettervall/notes/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference == findPreference(getString(R.string.dark_theme_key))) { // Dark Theme
boolean setDarkTheme = mSharedPreferences.getBoolean(getString(R.string.dark_theme_key), false);
if (setDarkTheme) {
boolean nightMode = mSharedPreferences.getBoolean(getString(R.string.dark_theme_key),
getResources().getBoolean(R.bool.defaultNightMode));
if (nightMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
getActivity().recreate();
} else if (preference == findPreference(getString(R.string.backup_key))) { // Backup Db
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, BACKUP_DB_REQUEST_CODE);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/bools.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="defaultNightMode">false</bool>
<bool name="isLandscape">false</bool>
<bool name="isTablet">false</bool>
</resources>

0 comments on commit ecf327e

Please sign in to comment.