diff --git a/src/main/kotlin/com/machiav3lli/fdroid/content/Preferences.kt b/src/main/kotlin/com/machiav3lli/fdroid/content/Preferences.kt index 3bc6b354..2915f4e6 100644 --- a/src/main/kotlin/com/machiav3lli/fdroid/content/Preferences.kt +++ b/src/main/kotlin/com/machiav3lli/fdroid/content/Preferences.kt @@ -2,7 +2,7 @@ package com.machiav3lli.fdroid.content import android.content.Context import android.content.SharedPreferences -import android.content.res.Configuration +import androidx.appcompat.app.AppCompatDelegate import com.machiav3lli.fdroid.PREFS_LANGUAGE import com.machiav3lli.fdroid.PREFS_LANGUAGE_DEFAULT import com.machiav3lli.fdroid.R @@ -310,38 +310,49 @@ object Preferences { else if (Android.sdk(29)) listOf(System, SystemBlack, Light, Dark, Black) else listOf(Light, Dark, Black) - abstract fun getResId(configuration: Configuration): Int + abstract val resId: Int + abstract val nightMode: Int object System : Theme("system") { - override fun getResId(configuration: Configuration): Int { - return if ((configuration.uiMode and Configuration.UI_MODE_NIGHT_YES) != 0) - R.style.Theme_Main_Dark else R.style.Theme_Main_Light - } + override val resId: Int + get() = R.style.Theme_Main + override val nightMode: Int + get() = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM } object SystemBlack : Theme("system-amoled") { - override fun getResId(configuration: Configuration): Int { - return if ((configuration.uiMode and Configuration.UI_MODE_NIGHT_YES) != 0) - R.style.Theme_Main_Amoled else R.style.Theme_Main_Light - } + override val resId: Int + get() = R.style.Theme_Main_Amoled + override val nightMode: Int + get() = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM } object Dynamic : Theme("dynamic-system") { - override fun getResId(configuration: Configuration): Int { - return -1 - } + override val resId: Int + get() = -1 + override val nightMode: Int + get() = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM } object Light : Theme("light") { - override fun getResId(configuration: Configuration): Int = R.style.Theme_Main_Light + override val resId: Int + get() = R.style.Theme_Main + override val nightMode: Int + get() = AppCompatDelegate.MODE_NIGHT_NO } object Dark : Theme("dark") { - override fun getResId(configuration: Configuration): Int = R.style.Theme_Main_Dark + override val resId: Int + get() = R.style.Theme_Main + override val nightMode: Int + get() = AppCompatDelegate.MODE_NIGHT_YES } object Black : Theme("amoled") { - override fun getResId(configuration: Configuration): Int = R.style.Theme_Main_Amoled + override val resId: Int + get() = R.style.Theme_Main_Amoled + override val nightMode: Int + get() = AppCompatDelegate.MODE_NIGHT_YES } } diff --git a/src/main/kotlin/com/machiav3lli/fdroid/ui/activities/MainActivityX.kt b/src/main/kotlin/com/machiav3lli/fdroid/ui/activities/MainActivityX.kt index 59a83bab..3fd86481 100644 --- a/src/main/kotlin/com/machiav3lli/fdroid/ui/activities/MainActivityX.kt +++ b/src/main/kotlin/com/machiav3lli/fdroid/ui/activities/MainActivityX.kt @@ -81,7 +81,7 @@ class MainActivityX : AppCompatActivity() { @OptIn(ExperimentalAnimationApi::class, ExperimentalMaterial3Api::class) override fun onCreate(savedInstanceState: Bundle?) { (application as MainApplication).mActivity = this - currentTheme = Preferences[Preferences.Key.Theme].getResId(resources.configuration) + currentTheme = Preferences[Preferences.Key.Theme].resId setCustomTheme() super.onCreate(savedInstanceState) @@ -155,7 +155,7 @@ class MainActivityX : AppCompatActivity() { override fun onResume() { super.onResume() - if (currentTheme != Preferences[Preferences.Key.Theme].getResId(resources.configuration)) + if (currentTheme != Preferences[Preferences.Key.Theme].resId) recreate() if (!powerManager.isIgnoringBatteryOptimizations(this.packageName) && !Preferences[Preferences.Key.IgnoreIgnoreBatteryOptimization]) showBatteryOptimizationDialog() diff --git a/src/main/kotlin/com/machiav3lli/fdroid/utility/Utils.kt b/src/main/kotlin/com/machiav3lli/fdroid/utility/Utils.kt index f3e4ec79..b3d30984 100644 --- a/src/main/kotlin/com/machiav3lli/fdroid/utility/Utils.kt +++ b/src/main/kotlin/com/machiav3lli/fdroid/utility/Utils.kt @@ -538,6 +538,6 @@ fun NavDestination.destinationToItem(): NavItem? = listOf( ).find { this.route == it.destination } fun Activity.setCustomTheme() { - AppCompatDelegate.setDefaultNightMode(Preferences[Preferences.Key.Theme].getNightMode()) - if (!isDynamicColorsTheme) setTheme(Preferences[Preferences.Key.Theme].getResId(resources.configuration)) + AppCompatDelegate.setDefaultNightMode(Preferences[Preferences.Key.Theme].nightMode) + if (!isDynamicColorsTheme) setTheme(Preferences[Preferences.Key.Theme].resId) } \ No newline at end of file