Add "System" theme which respects day/night state on Android 10

This commit is contained in:
kitsunyan
2020-07-31 00:40:17 +03:00
parent 175448dc8d
commit edd82e2797
4 changed files with 25 additions and 6 deletions
@@ -2,10 +2,12 @@ package nya.kitsunyan.foxydroid.content
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.Configuration
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject import io.reactivex.rxjava3.subjects.PublishSubject
import nya.kitsunyan.foxydroid.R import nya.kitsunyan.foxydroid.R
import nya.kitsunyan.foxydroid.entity.ProductItem import nya.kitsunyan.foxydroid.entity.ProductItem
import nya.kitsunyan.foxydroid.utility.extension.android.*
import java.net.Proxy import java.net.Proxy
object Preferences { object Preferences {
@@ -84,7 +86,8 @@ object Preferences {
object ProxyPort: Key<Int>("proxy_port", Value.IntValue(9050)) object ProxyPort: Key<Int>("proxy_port", Value.IntValue(9050))
object ProxyType: Key<Preferences.ProxyType>("proxy_type", Value.EnumerationValue(Preferences.ProxyType.Direct)) object ProxyType: Key<Preferences.ProxyType>("proxy_type", Value.EnumerationValue(Preferences.ProxyType.Direct))
object SortOrder: Key<Preferences.SortOrder>("sort_order", Value.EnumerationValue(Preferences.SortOrder.Update)) object SortOrder: Key<Preferences.SortOrder>("sort_order", Value.EnumerationValue(Preferences.SortOrder.Update))
object Theme: Key<Preferences.Theme>("theme", Value.EnumerationValue(Preferences.Theme.Light)) object Theme: Key<Preferences.Theme>("theme", Value.EnumerationValue(if (Android.sdk(29))
Preferences.Theme.System else Preferences.Theme.Light))
object UpdateNotify: Key<Boolean>("update_notify", Value.BooleanValue(true)) object UpdateNotify: Key<Boolean>("update_notify", Value.BooleanValue(true))
object UpdateUnstable: Key<Boolean>("update_unstable", Value.BooleanValue(false)) object UpdateUnstable: Key<Boolean>("update_unstable", Value.BooleanValue(false))
} }
@@ -116,12 +119,26 @@ object Preferences {
object Update: SortOrder("update", ProductItem.Order.LAST_UPDATE) object Update: SortOrder("update", ProductItem.Order.LAST_UPDATE)
} }
sealed class Theme(override val valueString: String, val resId: Int): Enumeration<Theme> { sealed class Theme(override val valueString: String): Enumeration<Theme> {
override val values: List<Theme> override val values: List<Theme>
get() = listOf(Light, Dark) get() = if (Android.sdk(29)) listOf(System, Light, Dark) else listOf(Light, Dark)
object Light: Theme("light", R.style.Theme_Main_Light) abstract fun getResId(configuration: Configuration): Int
object Dark: Theme("dark", R.style.Theme_Main_Dark)
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
}
}
object Light: Theme("light") {
override fun getResId(configuration: Configuration): Int = R.style.Theme_Main_Light
}
object Dark: Theme("dark") {
override fun getResId(configuration: Configuration): Int = R.style.Theme_Main_Dark
}
} }
operator fun <T> get(key: Key<T>): T { operator fun <T> get(key: Key<T>): T {
@@ -77,6 +77,7 @@ class PreferencesFragment: ScreenFragment() {
preferences.addCategory(getString(R.string.other)) { preferences.addCategory(getString(R.string.other)) {
addEnumeration(Preferences.Key.Theme, getString(R.string.theme)) { addEnumeration(Preferences.Key.Theme, getString(R.string.theme)) {
when (it) { when (it) {
is Preferences.Theme.System -> getString(R.string.system)
is Preferences.Theme.Light -> getString(R.string.light) is Preferences.Theme.Light -> getString(R.string.light)
is Preferences.Theme.Dark -> getString(R.string.dark) is Preferences.Theme.Dark -> getString(R.string.dark)
} }
@@ -69,7 +69,7 @@ abstract class ScreenActivity: FragmentActivity() {
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(Preferences[Preferences.Key.Theme].resId) setTheme(Preferences[Preferences.Key.Theme].getResId(resources.configuration))
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
+1
View File
@@ -141,6 +141,7 @@
<string name="sync_repositories_automatically">Sync repositories automatically</string> <string name="sync_repositories_automatically">Sync repositories automatically</string>
<string name="syncing">Syncing</string> <string name="syncing">Syncing</string>
<string name="syncing_FORMAT">Syncing %s</string> <string name="syncing_FORMAT">Syncing %s</string>
<string name="system">System</string>
<string name="tap_to_install_DESC">Tap to install.</string> <string name="tap_to_install_DESC">Tap to install.</string>
<string name="theme">Theme</string> <string name="theme">Theme</string>
<string name="tracks_or_reports_your_activity">Tracks or reports your activity</string> <string name="tracks_or_reports_your_activity">Tracks or reports your activity</string>