Moved dark theme setting to setup

This commit is contained in:
M66B 2018-08-04 15:37:42 +00:00
parent fe9c0a4a8f
commit 4467a088ea
5 changed files with 56 additions and 15 deletions

View File

@ -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();
}
}

View File

@ -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));
}

View File

@ -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);

View File

@ -160,5 +160,16 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPermissions" />
<CheckBox
android:id="@+id/cbDarkTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/title_setup_dark_theme"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPermissionsDone" />
</android.support.constraint.ConstraintLayout>
</ScrollView>

View File

@ -10,7 +10,6 @@
<string name="menu_unified">Unified inbox</string>
<string name="menu_folders">Folders</string>
<string name="menu_theme">Dark theme</string>
<string name="menu_setup">Setup</string>
<string name="menu_debug">Debug info</string>
@ -36,6 +35,7 @@
<string name="title_setup_permissions">Grant permissions</string>
<string name="title_setup_permissions_remark">To read contacts (optional)</string>
<string name="title_setup_done">Done</string>
<string name="title_setup_dark_theme">Dark theme</string>
<string name="title_name">Your name</string>
<string name="title_email">Your email address</string>