From 4467a088eacae1366c5797183bbb1261929eade0 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 4 Aug 2018 15:37:42 +0000 Subject: [PATCH] Moved dark theme setting to setup --- .../java/eu/faircode/email/ActivityBase.java | 21 ++++++++++++++-- .../java/eu/faircode/email/ActivityView.java | 13 +--------- .../java/eu/faircode/email/FragmentSetup.java | 24 +++++++++++++++++++ app/src/main/res/layout/fragment_setup.xml | 11 +++++++++ app/src/main/res/values/strings.xml | 2 +- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityBase.java b/app/src/main/java/eu/faircode/email/ActivityBase.java index a290990b..2a342dc3 100644 --- a/app/src/main/java/eu/faircode/email/ActivityBase.java +++ b/app/src/main/java/eu/faircode/email/ActivityBase.java @@ -23,13 +23,30 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; +import android.util.Log; -abstract class ActivityBase extends AppCompatActivity { +abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { + Log.i(Helper.TAG, "Create " + this.getClass().getName()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String theme = prefs.getString("theme", "light"); - setTheme("dark".equals(theme) ? R.style.AppThemeDark : R.style.AppThemeLight); + setTheme("light".equals(theme) ? R.style.AppThemeLight : R.style.AppThemeDark); + PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); super.onCreate(savedInstanceState); } + + @Override + protected void onDestroy() { + Log.i(Helper.TAG, "Destroy " + this.getClass().getName()); + PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); + super.onDestroy(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { + Log.i(Helper.TAG, "Preference " + key + "=" + prefs.getAll().get(key)); + if ("theme".equals(key)) + recreate(); + } } diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index dc88e52f..d997ccd3 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -111,9 +111,6 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack case R.string.menu_folders: onMenuFolders(); break; - case R.string.menu_theme: - onMenuTheme(); - break; case R.string.menu_setup: onMenuSetup(); break; @@ -184,6 +181,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { + super.onSharedPreferenceChanged(prefs, key); if ("eula".equals(key)) init(); } @@ -244,7 +242,6 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack ArrayAdapterDrawer drawerArray = new ArrayAdapterDrawer(this, R.layout.item_drawer); drawerArray.add(new DrawerItem(ActivityView.this, R.string.menu_unified)); drawerArray.add(new DrawerItem(ActivityView.this, R.string.menu_folders)); - drawerArray.add(new DrawerItem(ActivityView.this, R.string.menu_theme, "dark".equals(prefs.getString("theme", "light")))); drawerArray.add(new DrawerItem(ActivityView.this, R.string.menu_setup)); drawerArray.add(new DrawerItem(ActivityView.this, R.string.menu_debug)); drawerList.setAdapter(drawerArray); @@ -268,14 +265,6 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack fragmentTransaction.commit(); } - private void onMenuTheme() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - String theme = prefs.getString("theme", "light"); - theme = ("dark".equals(theme) ? "light" : "dark"); - prefs.edit().putString("theme", theme).apply(); - recreate(); - } - private void onMenuSetup() { startActivity(new Intent(ActivityView.this, ActivitySetup.class)); } diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index 3a828470..c1b2c633 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -21,8 +21,10 @@ package eu.faircode.email; import android.Manifest; import android.arch.lifecycle.Observer; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -33,6 +35,8 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; import android.widget.ProgressBar; import android.widget.TextView; @@ -54,6 +58,8 @@ public class FragmentSetup extends Fragment { private Button btnPermissions; private TextView tvPermissionsDone; + private CheckBox cbDarkTheme; + private ExecutorService executor = Executors.newCachedThreadPool(); private static final String[] permissions = new String[]{ @@ -79,6 +85,8 @@ public class FragmentSetup extends Fragment { btnPermissions = view.findViewById(R.id.btnPermissions); tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone); + cbDarkTheme = view.findViewById(R.id.cbDarkTheme); + // Wire controls btnAccount.setOnClickListener(new View.OnClickListener() { @@ -175,6 +183,22 @@ public class FragmentSetup extends Fragment { } }); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + String theme = prefs.getString("theme", "light"); + boolean dark = "dark".equals(theme); + cbDarkTheme.setTag(dark); + cbDarkTheme.setChecked(dark); + cbDarkTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton button, boolean checked) { + if (checked != (Boolean) button.getTag()) { + button.setTag(checked); + cbDarkTheme.setChecked(checked); + prefs.edit().putString("theme", checked ? "dark" : "light").apply(); + } + } + }); + // Initialize pbAccount.setVisibility(View.GONE); diff --git a/app/src/main/res/layout/fragment_setup.xml b/app/src/main/res/layout/fragment_setup.xml index 545fcf0d..55ec15ae 100644 --- a/app/src/main/res/layout/fragment_setup.xml +++ b/app/src/main/res/layout/fragment_setup.xml @@ -160,5 +160,16 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvPermissions" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ed9745f1..49cb5ad2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,7 +10,6 @@ Unified inbox Folders - Dark theme Setup Debug info @@ -36,6 +35,7 @@ Grant permissions To read contacts (optional) Done + Dark theme Your name Your email address