diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java index ef9f7f7..4d1f50a 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java @@ -24,6 +24,7 @@ import com.cringe_studios.cringe_authenticator.crypto.Crypto; import com.cringe_studios.cringe_authenticator.crypto.CryptoException; import com.cringe_studios.cringe_authenticator.crypto.CryptoParameters; import com.cringe_studios.cringe_authenticator.databinding.FragmentSettingsBinding; +import com.cringe_studios.cringe_authenticator.util.Appearance; import com.cringe_studios.cringe_authenticator.util.BiometricUtil; import com.cringe_studios.cringe_authenticator.util.DialogUtil; import com.cringe_studios.cringe_authenticator.util.FabUtil; @@ -177,6 +178,27 @@ public class SettingsFragment extends NamedFragment { public void onNothingSelected(AdapterView parent) {} }); + String[] appearanceNames = new String[Appearance.values().length]; + for(int i = 0; i < Appearance.values().length; i++) { + appearanceNames[i] = getResources().getString(Appearance.values()[i].getName()); + } + + binding.settingsAppearance.setAdapter(new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, appearanceNames)); + binding.settingsAppearance.setSelection(SettingsUtil.getAppearance(requireContext()).ordinal()); + binding.settingsAppearance.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + Appearance appearance = Appearance.values()[position]; + if(appearance == SettingsUtil.getAppearance(requireContext())) return; + + SettingsUtil.setAppearance(requireContext(), appearance); + requireActivity().recreate(); + } + + @Override + public void onNothingSelected(AdapterView parent) {} + }); + FabUtil.hideFabs(requireActivity()); return binding.getRoot(); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Appearance.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Appearance.java new file mode 100644 index 0000000..99969ed --- /dev/null +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Appearance.java @@ -0,0 +1,35 @@ +package com.cringe_studios.cringe_authenticator.util; + +import androidx.annotation.StringRes; +import androidx.appcompat.app.AppCompatDelegate; + +import com.cringe_studios.cringe_authenticator.R; + +public enum Appearance { + + FOLLOW_SYSTEM(R.string.appearance_follow_system, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM), + LIGHT(R.string.appearance_light, AppCompatDelegate.MODE_NIGHT_NO), + DARK(R.string.appearance_dark, AppCompatDelegate.MODE_NIGHT_YES), + ; + + @StringRes + private int name; + + @AppCompatDelegate.NightMode + private int value; + + Appearance(@StringRes int name, @AppCompatDelegate.NightMode int value) { + this.name = name; + this.value = value; + } + + @StringRes + public int getName() { + return name; + } + + @AppCompatDelegate.NightMode + public int getValue() { + return value; + } +} diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java index fad2fcf..f96819d 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java @@ -188,6 +188,20 @@ public class SettingsUtil { } } + public static void setAppearance(Context ctx, Appearance appearance) { + SharedPreferences prefs = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE); + prefs.edit().putString("appearance", appearance.name()).apply(); + } + + public static Appearance getAppearance(Context ctx) { + String themeId = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("appearance", Appearance.FOLLOW_SYSTEM.name()); + try { + return Appearance.valueOf(themeId); + }catch(IllegalArgumentException e) { + return Appearance.FOLLOW_SYSTEM; + } + } + public static void setLocale(Context ctx, Locale locale) { SharedPreferences prefs = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE); prefs.edit().putString("locale", locale.getLanguage()).apply(); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java index e95bc9e..903655b 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java @@ -1,5 +1,7 @@ package com.cringe_studios.cringe_authenticator.util; +import android.annotation.SuppressLint; + import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; @@ -11,8 +13,7 @@ public class ThemeUtil { Theme theme = SettingsUtil.getTheme(activity); activity.setTheme(theme.getStyle()); - // TODO: use settings - //AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + AppCompatDelegate.setDefaultNightMode(SettingsUtil.getAppearance(activity).getValue()); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8545503..d680b80 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" - android:background="@drawable/background_blue_green" tools:context=".MainActivity"> Blau/Gelb Grün/Gelb Orange/Türkis + System folgen + Hell + Dunkel \ 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 09db239..91cea61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -82,4 +82,7 @@ Blue/Yellow Green/Yellow Orange/Turquoise + Dark + Light + Follow system \ No newline at end of file