diff options
author | stefan-niedermann <info@niedermann.it> | 2019-09-27 23:22:43 +0300 |
---|---|---|
committer | stefan-niedermann <info@niedermann.it> | 2019-09-27 23:22:43 +0300 |
commit | 56e1f8e9b0ec3833e5c2155c43c3a71a52ee039f (patch) | |
tree | 6e3135688b7021fd787d7f4a65948de10cf394f1 /app/src/main | |
parent | a9f9bedf187f8a2b963ce638f8a5905efe4f7d36 (diff) |
#108 dark mode
Diffstat (limited to 'app/src/main')
11 files changed, 61 insertions, 7 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f39d20a13..7980f8ebf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application + android:name="it.niedermann.nextcloud.deck.Application" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/Application.java b/app/src/main/java/it/niedermann/nextcloud/deck/Application.java new file mode 100644 index 000000000..4543389b4 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/Application.java @@ -0,0 +1,29 @@ +package it.niedermann.nextcloud.deck; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import androidx.appcompat.app.AppCompatDelegate; + +public class Application extends android.app.Application { + + @Override + public void onCreate() { + setAppTheme(getAppTheme(getApplicationContext())); + super.onCreate(); + } + + public static void setAppTheme(Boolean darkTheme) { + if (darkTheme) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } + } + + public static boolean getAppTheme(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean(context.getString(R.string.pref_key_dark_theme), false); + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/AboutActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/AboutActivity.java index 1031dfe89..419bb34e6 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/AboutActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/AboutActivity.java @@ -12,6 +12,7 @@ import com.google.android.material.tabs.TabLayout; import butterknife.BindView; import butterknife.ButterKnife; +import it.niedermann.nextcloud.deck.Application; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.ui.about.AboutFragmentContributingTab; import it.niedermann.nextcloud.deck.ui.about.AboutFragmentCreditsTab; @@ -27,6 +28,7 @@ public class AboutActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { + setTheme(Application.getAppTheme(this) ? R.style.DarkAppTheme : R.style.AppTheme); super.onCreate(savedInstanceState); Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this)); setContentView(R.layout.activity_about); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java index 2a2acfbf9..7b0864ea4 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java @@ -22,6 +22,7 @@ import java.util.Objects; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; +import it.niedermann.nextcloud.deck.Application; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.model.Card; import it.niedermann.nextcloud.deck.model.Label; @@ -67,6 +68,7 @@ public class EditActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { + setTheme(Application.getAppTheme(this) ? R.style.DarkAppTheme : R.style.AppTheme); super.onCreate(savedInstanceState); Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this)); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java index fd298d89b..9ab0a2106 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java @@ -28,6 +28,7 @@ import java.util.Objects; import butterknife.BindView; import butterknife.ButterKnife; +import it.niedermann.nextcloud.deck.Application; import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.model.Account; @@ -76,8 +77,7 @@ public class MainActivity extends DrawerActivity { @Override protected void onCreate(Bundle savedInstanceState) { - setTheme(R.style.AppTheme_NoActionBar); - setTheme(R.style.DarkAppTheme); + setTheme(Application.getAppTheme(this) ? R.style.DarkAppTheme_NoActionBar : R.style.AppTheme_NoActionBar); setContentView(R.layout.activity_main); ButterKnife.bind(this); super.onCreate(savedInstanceState); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/SettingsActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/SettingsActivity.java index d41147cc5..a0d7312bd 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/SettingsActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/SettingsActivity.java @@ -5,6 +5,8 @@ import android.os.Bundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import it.niedermann.nextcloud.deck.Application; +import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler; import it.niedermann.nextcloud.deck.ui.settings.SettingsFragment; @@ -13,6 +15,7 @@ public class SettingsActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { + setTheme(Application.getAppTheme(this) ? R.style.DarkAppTheme : R.style.AppTheme); super.onCreate(savedInstanceState); Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this)); setResult(RESULT_CANCELED); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java index 30de63958..120d619bf 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java @@ -1,5 +1,6 @@ package it.niedermann.nextcloud.deck.ui.settings; +import android.app.Activity; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceFragment; @@ -7,10 +8,11 @@ import android.preference.SwitchPreference; import androidx.annotation.Nullable; +import it.niedermann.nextcloud.deck.Application; import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.R; -public class SettingsFragment extends PreferenceFragment { +public class SettingsFragment extends PreferenceFragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -22,5 +24,15 @@ public class SettingsFragment extends PreferenceFragment { DeckLog.log("syncOnWifiOnly: " + syncOnWifiOnly); return true; }); + + final SwitchPreference themePref = (SwitchPreference) findPreference(getString(R.string.pref_key_dark_theme)); + themePref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { + Boolean darkTheme = (Boolean) newValue; + DeckLog.log("darkTheme: " + darkTheme); + Application.setAppTheme(darkTheme); + getActivity().setResult(Activity.RESULT_OK); + getActivity().recreate(); + return true; + }); } } diff --git a/app/src/main/res/values/setup.xml b/app/src/main/res/values/setup.xml index c375cc994..66aa2cefb 100644 --- a/app/src/main/res/values/setup.xml +++ b/app/src/main/res/values/setup.xml @@ -4,7 +4,7 @@ <string name="shared_preference_last_account" translatable="false">it.niedermann.nextcloud.deck.last_account</string> <string name="pref_key_wifi_only" translatable="false">wifiOnly</string> - <string name="pref_key_wifi_only" translatable="false">wifiOnly</string> + <string name="pref_key_dark_theme" translatable="false">darkTheme</string> <!-- To be concatenated with the account id --> <string name="shared_preference_last_board_for_account_" translatable="false">it.niedermann.nextcloud.deck.last_board_for_account_</string> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bcfa7f19a..bfc427406 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -131,6 +131,7 @@ <string name="action_card_list_rename_column">Rename column</string> <string name="simple_rename">Rename</string> <string name="simple_settings">Settings</string> + <string name="settings_theme_title">Dark theme</string> <string name="pref_value_wifi_and_mobile">Sync on Wi-Fi and mobile data</string> <string name="pref_value_wifi_only">Sync only on Wi-Fi</string> <string name="pref_value_theme_light">Light</string> diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index cffc67981..79a74c776 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -7,14 +7,18 @@ <item name="colorAccent">@color/primary</item> </style> - <!-- Base application theme. --> + <style name="AppTheme.NoActionBar" parent="AppTheme"> + <item name="windowActionBar">false</item> + <item name="windowNoTitle">true</item> + </style> + <style name="DarkAppTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary</item> <item name="colorAccent">@color/primary</item> </style> - <style name="AppTheme.NoActionBar" parent="AppTheme"> + <style name="DarkAppTheme.NoActionBar" parent="DarkAppTheme"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index 0cb12500a..98a7220fa 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -11,6 +11,6 @@ android:defaultValue="@string/pref_value_theme_light" android:icon="@drawable/ic_brightness_2_grey600_24dp" android:key="@string/pref_key_dark_theme" - android:title="@string/pref_value_dark_theme" /> + android:title="@string/settings_theme_title" /> </PreferenceScreen> |