From cfcccf811af8832eec45762af6d87d4acc5bd27d Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Fri, 25 Aug 2023 13:10:22 +0530 Subject: [PATCH] ThemeEngine: Rework theme configuration Signed-off-by: Aayush Gupta --- .../java/com/aurora/extensions/Platform.kt | 4 + .../java/com/aurora/extensions/ThemeEngine.kt | 132 +++++++----------- .../java/com/aurora/store/MainActivity.kt | 6 +- .../view/ui/onboarding/AccentFragment.kt | 3 +- .../store/view/ui/onboarding/ThemeFragment.kt | 3 +- .../store/view/ui/preferences/UIPreference.kt | 9 +- app/src/main/res/values-night-v23/themes.xml | 7 + app/src/main/res/values-night-v27/themes.xml | 11 ++ app/src/main/res/values-night/themes.xml | 4 + app/src/main/res/values-v21/themes.xml | 7 + app/src/main/res/values-v23/themes.xml | 8 ++ app/src/main/res/values-v27/themes.xml | 11 ++ .../res/values/{styles.xml => themes.xml} | 13 +- 13 files changed, 112 insertions(+), 106 deletions(-) create mode 100644 app/src/main/res/values-night-v23/themes.xml create mode 100644 app/src/main/res/values-night-v27/themes.xml create mode 100644 app/src/main/res/values-night/themes.xml create mode 100644 app/src/main/res/values-v21/themes.xml create mode 100644 app/src/main/res/values-v23/themes.xml create mode 100644 app/src/main/res/values-v27/themes.xml rename app/src/main/res/values/{styles.xml => themes.xml} (91%) diff --git a/app/src/main/java/com/aurora/extensions/Platform.kt b/app/src/main/java/com/aurora/extensions/Platform.kt index 4200364f9..a4af2cca3 100644 --- a/app/src/main/java/com/aurora/extensions/Platform.kt +++ b/app/src/main/java/com/aurora/extensions/Platform.kt @@ -37,6 +37,10 @@ fun isOAndAbove(): Boolean { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O } +fun isOMR1AndAbove(): Boolean { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 +} + fun isPAndAbove(): Boolean { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P } diff --git a/app/src/main/java/com/aurora/extensions/ThemeEngine.kt b/app/src/main/java/com/aurora/extensions/ThemeEngine.kt index bf441b37f..3dbfaf780 100644 --- a/app/src/main/java/com/aurora/extensions/ThemeEngine.kt +++ b/app/src/main/java/com/aurora/extensions/ThemeEngine.kt @@ -19,106 +19,70 @@ package com.aurora.extensions -import android.content.res.Configuration +import android.app.UiModeManager +import android.content.Context import android.graphics.Color -import android.os.Build -import android.view.View -import android.view.WindowInsetsController -import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate import androidx.core.graphics.ColorUtils -import androidx.fragment.app.Fragment +import androidx.core.view.WindowInsetsControllerCompat import com.aurora.store.util.CommonUtil +import com.aurora.store.util.Preferences -fun Fragment.applyTheme(themeId: Int) { - val themeStyle = CommonUtil.getThemeStyleById(themeId) +fun AppCompatActivity.applyThemeAccent() { + val themeId = Preferences.getInteger(this, Preferences.PREFERENCE_THEME_TYPE) + val accentId = Preferences.getInteger(this, Preferences.PREFERENCE_THEME_ACCENT) + val uiModeManager = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager - if (themeId == 0) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) - (requireActivity() as AppCompatActivity).applyDayNightMask() - } else { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) - } - - /*Apply Theme*/ - requireContext().theme.applyStyle(themeStyle, true) - (requireActivity() as AppCompatActivity).recreate() - - if (themeId == 1) { - (requireActivity() as AppCompatActivity).setLightConfiguration() - } -} - -fun AppCompatActivity.applyTheme(themeId: Int, accentId: Int = 1) { val themeStyle = CommonUtil.getThemeStyleById(themeId) val accentStyle = CommonUtil.getAccentStyleById(accentId) - if (themeId == 0) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) - applyDayNightMask() - } else { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) - } - /*Apply Theme*/ setTheme(themeStyle) - - /*Apply Accent*/ theme.applyStyle(accentStyle, true) - /*Apply Light Configuration*/ - if (themeId == 1) { - setLightConfiguration() + when (themeId) { + 0 -> { + if (isSAndAbove()) { + uiModeManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_CUSTOM) + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) + } + } + + 1 -> { + setSystemBarConfiguration(light = true) + if (isSAndAbove()) { + uiModeManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_NO) + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) + } + } + + else -> { + setSystemBarConfiguration(light = false) + if (isSAndAbove()) { + uiModeManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES) + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) + } + } } } -fun AppCompatActivity.applyDayNightMask() { - val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) { - setLightConfiguration() +private fun AppCompatActivity.setSystemBarConfiguration(light: Boolean) { + WindowInsetsControllerCompat(this.window, this.window.decorView.rootView).apply { + // Status bar color + if (isMAndAbove()) { + isAppearanceLightStatusBars = light + } else { + window.statusBarColor = ColorUtils.setAlphaComponent(Color.BLACK, 120) + } + + // Navigation bar color + if (isOMR1AndAbove()) { + isAppearanceLightNavigationBars = light + window.navigationBarColor = getStyledAttributeColor(android.R.attr.colorBackground) + } } } - -fun AppCompatActivity.setLightConfiguration() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - setLightConfigurationO() - } else { - setLightConfigurationO() - } -} - -@RequiresApi(Build.VERSION_CODES.R) -private fun AppCompatActivity.setLightConfigurationR() { - window?.insetsController?.setSystemBarsAppearance( - WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS - or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS, - WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS - ) -} - -private fun AppCompatActivity.setLightConfigurationO() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - setLightStatusBar() - setLightNavigationBar() - } else { - window.statusBarColor = ColorUtils.setAlphaComponent(Color.BLACK, 120) - } -} - -private fun AppCompatActivity.setLightStatusBar() { - var flags = window.decorView.systemUiVisibility - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR - } - window.decorView.systemUiVisibility = flags -} - -private fun AppCompatActivity.setLightNavigationBar() { - var flags = window.decorView.systemUiVisibility - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - window.navigationBarColor = getStyledAttributeColor(android.R.attr.colorBackground) - flags = flags or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR - } - window.decorView.systemUiVisibility = flags -} diff --git a/app/src/main/java/com/aurora/store/MainActivity.kt b/app/src/main/java/com/aurora/store/MainActivity.kt index 640b64850..577acbd6b 100644 --- a/app/src/main/java/com/aurora/store/MainActivity.kt +++ b/app/src/main/java/com/aurora/store/MainActivity.kt @@ -48,7 +48,7 @@ import coil.load import coil.transform.RoundedCornersTransformation import com.aurora.Constants import com.aurora.extensions.accentColor -import com.aurora.extensions.applyTheme +import com.aurora.extensions.applyThemeAccent import com.aurora.extensions.isRAndAbove import com.aurora.extensions.toast import com.aurora.gplayapi.data.models.AuthData @@ -89,9 +89,7 @@ class MainActivity : AppCompatActivity() { ) override fun onCreate(savedInstanceState: Bundle?) { - val themeId = Preferences.getInteger(this, Preferences.PREFERENCE_THEME_TYPE) - val accentId = Preferences.getInteger(this, Preferences.PREFERENCE_THEME_ACCENT) - applyTheme(themeId, accentId) + applyThemeAccent() super.onCreate(savedInstanceState) B = ActivityMainBinding.inflate(layoutInflater) diff --git a/app/src/main/java/com/aurora/store/view/ui/onboarding/AccentFragment.kt b/app/src/main/java/com/aurora/store/view/ui/onboarding/AccentFragment.kt index 264d694d6..6e7b8e922 100644 --- a/app/src/main/java/com/aurora/store/view/ui/onboarding/AccentFragment.kt +++ b/app/src/main/java/com/aurora/store/view/ui/onboarding/AccentFragment.kt @@ -29,7 +29,6 @@ import android.view.View import android.view.ViewAnimationUtils import android.view.ViewGroup import androidx.recyclerview.widget.StaggeredGridLayoutManager -import com.aurora.extensions.applyTheme import com.aurora.extensions.hide import com.aurora.extensions.isSAndAbove import com.aurora.extensions.isVisible @@ -124,7 +123,7 @@ class AccentFragment : BaseFragment() { } private fun updateAccent(accentId: Int) { - applyTheme(themeId) + requireActivity().recreate() save(PREFERENCE_THEME_ACCENT, accentId) } diff --git a/app/src/main/java/com/aurora/store/view/ui/onboarding/ThemeFragment.kt b/app/src/main/java/com/aurora/store/view/ui/onboarding/ThemeFragment.kt index e49e48ef5..1257e5f1c 100644 --- a/app/src/main/java/com/aurora/store/view/ui/onboarding/ThemeFragment.kt +++ b/app/src/main/java/com/aurora/store/view/ui/onboarding/ThemeFragment.kt @@ -28,7 +28,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewAnimationUtils import android.view.ViewGroup -import com.aurora.extensions.applyTheme import com.aurora.extensions.hide import com.aurora.extensions.isVisible import com.aurora.extensions.show @@ -110,7 +109,7 @@ class ThemeFragment : BaseFragment() { } private fun update(themeId: Int) { - applyTheme(themeId) + requireActivity().recreate() save(PREFERENCE_THEME_TYPE, themeId) } diff --git a/app/src/main/java/com/aurora/store/view/ui/preferences/UIPreference.kt b/app/src/main/java/com/aurora/store/view/ui/preferences/UIPreference.kt index 26136f8c6..75e58fdc8 100644 --- a/app/src/main/java/com/aurora/store/view/ui/preferences/UIPreference.kt +++ b/app/src/main/java/com/aurora/store/view/ui/preferences/UIPreference.kt @@ -29,7 +29,6 @@ import androidx.navigation.fragment.findNavController import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat -import com.aurora.extensions.applyTheme import com.aurora.extensions.isTAndAbove import com.aurora.store.R import com.aurora.store.util.Preferences @@ -71,7 +70,7 @@ class UIPreference : PreferenceFragmentCompat() { val themeId = Integer.parseInt(newValue.toString()) save(Preferences.PREFERENCE_THEME_TYPE, themeId) - applyTheme(themeId) + requireActivity().recreate() true } } @@ -79,14 +78,10 @@ class UIPreference : PreferenceFragmentCompat() { val accentPreference: ListPreference? = findPreference(Preferences.PREFERENCE_THEME_ACCENT) accentPreference?.let { it.setOnPreferenceChangeListener { _, newValue -> - val themeId = Preferences.getInteger( - requireContext(), - Preferences.PREFERENCE_THEME_TYPE - ) val accentId = Integer.parseInt(newValue.toString()) save(Preferences.PREFERENCE_THEME_ACCENT, accentId) - applyTheme(themeId) + requireActivity().recreate() true } } diff --git a/app/src/main/res/values-night-v23/themes.xml b/app/src/main/res/values-night-v23/themes.xml new file mode 100644 index 000000000..be2c3f9d1 --- /dev/null +++ b/app/src/main/res/values-night-v23/themes.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/app/src/main/res/values-night-v27/themes.xml b/app/src/main/res/values-night-v27/themes.xml new file mode 100644 index 000000000..d7837c5e6 --- /dev/null +++ b/app/src/main/res/values-night-v27/themes.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml new file mode 100644 index 000000000..4f067a632 --- /dev/null +++ b/app/src/main/res/values-night/themes.xml @@ -0,0 +1,4 @@ + + + + diff --git a/app/src/main/res/values-v23/themes.xml b/app/src/main/res/values-v23/themes.xml new file mode 100644 index 000000000..c570d5c37 --- /dev/null +++ b/app/src/main/res/values-v23/themes.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/app/src/main/res/values-v27/themes.xml b/app/src/main/res/values-v27/themes.xml new file mode 100644 index 000000000..156c61c46 --- /dev/null +++ b/app/src/main/res/values-v27/themes.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/themes.xml similarity index 91% rename from app/src/main/res/values/styles.xml rename to app/src/main/res/values/themes.xml index 6d32e06b6..b62525626 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/themes.xml @@ -19,10 +19,11 @@ - +