Update: Revamp Preferences.Theme values

This commit is contained in:
machiav3lli 2022-10-08 01:41:58 +02:00
parent a300e025b7
commit dd607037d0
3 changed files with 31 additions and 20 deletions

View File

@ -2,7 +2,7 @@ package com.machiav3lli.fdroid.content
import android.content.Context import android.content.Context
import android.content.SharedPreferences 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
import com.machiav3lli.fdroid.PREFS_LANGUAGE_DEFAULT import com.machiav3lli.fdroid.PREFS_LANGUAGE_DEFAULT
import com.machiav3lli.fdroid.R import com.machiav3lli.fdroid.R
@ -310,38 +310,49 @@ object Preferences {
else if (Android.sdk(29)) listOf(System, SystemBlack, Light, Dark, Black) else if (Android.sdk(29)) listOf(System, SystemBlack, Light, Dark, Black)
else listOf(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") { object System : Theme("system") {
override fun getResId(configuration: Configuration): Int { override val resId: Int
return if ((configuration.uiMode and Configuration.UI_MODE_NIGHT_YES) != 0) get() = R.style.Theme_Main
R.style.Theme_Main_Dark else R.style.Theme_Main_Light override val nightMode: Int
} get() = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} }
object SystemBlack : Theme("system-amoled") { object SystemBlack : Theme("system-amoled") {
override fun getResId(configuration: Configuration): Int { override val resId: Int
return if ((configuration.uiMode and Configuration.UI_MODE_NIGHT_YES) != 0) get() = R.style.Theme_Main_Amoled
R.style.Theme_Main_Amoled else R.style.Theme_Main_Light override val nightMode: Int
} get() = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} }
object Dynamic : Theme("dynamic-system") { object Dynamic : Theme("dynamic-system") {
override fun getResId(configuration: Configuration): Int { override val resId: Int
return -1 get() = -1
} override val nightMode: Int
get() = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} }
object Light : Theme("light") { 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") { 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") { 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
} }
} }

View File

@ -81,7 +81,7 @@ class MainActivityX : AppCompatActivity() {
@OptIn(ExperimentalAnimationApi::class, ExperimentalMaterial3Api::class) @OptIn(ExperimentalAnimationApi::class, ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
(application as MainApplication).mActivity = this (application as MainApplication).mActivity = this
currentTheme = Preferences[Preferences.Key.Theme].getResId(resources.configuration) currentTheme = Preferences[Preferences.Key.Theme].resId
setCustomTheme() setCustomTheme()
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -155,7 +155,7 @@ class MainActivityX : AppCompatActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (currentTheme != Preferences[Preferences.Key.Theme].getResId(resources.configuration)) if (currentTheme != Preferences[Preferences.Key.Theme].resId)
recreate() recreate()
if (!powerManager.isIgnoringBatteryOptimizations(this.packageName) && !Preferences[Preferences.Key.IgnoreIgnoreBatteryOptimization]) if (!powerManager.isIgnoringBatteryOptimizations(this.packageName) && !Preferences[Preferences.Key.IgnoreIgnoreBatteryOptimization])
showBatteryOptimizationDialog() showBatteryOptimizationDialog()

View File

@ -538,6 +538,6 @@ fun NavDestination.destinationToItem(): NavItem? = listOf(
).find { this.route == it.destination } ).find { this.route == it.destination }
fun Activity.setCustomTheme() { fun Activity.setCustomTheme() {
AppCompatDelegate.setDefaultNightMode(Preferences[Preferences.Key.Theme].getNightMode()) AppCompatDelegate.setDefaultNightMode(Preferences[Preferences.Key.Theme].nightMode)
if (!isDynamicColorsTheme) setTheme(Preferences[Preferences.Key.Theme].getResId(resources.configuration)) if (!isDynamicColorsTheme) setTheme(Preferences[Preferences.Key.Theme].resId)
} }