Add theme settings

pull/1/head
Koitharu 6 years ago
parent 21c2a4aa9a
commit 2dc5840872

@ -1,6 +1,7 @@
package org.koitharu.kotatsu package org.koitharu.kotatsu
import android.app.Application import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import androidx.room.Room import androidx.room.Room
import coil.Coil import coil.Coil
import coil.ImageLoader import coil.ImageLoader
@ -23,6 +24,7 @@ class KotatsuApp : Application() {
super.onCreate() super.onCreate()
initKoin() initKoin()
initCoil() initCoil()
AppCompatDelegate.setDefaultNightMode(AppSettings(this).theme)
} }
private fun initKoin() { private fun initKoin() {

@ -3,9 +3,11 @@ package org.koitharu.kotatsu.core.prefs
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.Resources import android.content.res.Resources
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.utils.delegates.prefs.EnumPreferenceDelegate import org.koitharu.kotatsu.utils.delegates.prefs.EnumPreferenceDelegate
import org.koitharu.kotatsu.utils.delegates.prefs.StringIntPreferenceDelegate
class AppSettings private constructor(resources: Resources, private val prefs: SharedPreferences) : SharedPreferences by prefs { class AppSettings private constructor(resources: Resources, private val prefs: SharedPreferences) : SharedPreferences by prefs {
@ -13,6 +15,8 @@ class AppSettings private constructor(resources: Resources, private val prefs: S
var listMode by EnumPreferenceDelegate(ListMode::class.java, resources.getString(R.string.key_list_mode), ListMode.DETAILED_LIST) var listMode by EnumPreferenceDelegate(ListMode::class.java, resources.getString(R.string.key_list_mode), ListMode.DETAILED_LIST)
val theme by StringIntPreferenceDelegate(resources.getString(R.string.key_theme), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
fun subscribe(listener: SharedPreferences.OnSharedPreferenceChangeListener) { fun subscribe(listener: SharedPreferences.OnSharedPreferenceChangeListener) {
prefs.registerOnSharedPreferenceChangeListener(listener) prefs.registerOnSharedPreferenceChangeListener(listener)
} }

@ -1,6 +1,5 @@
package org.koitharu.kotatsu.ui.common package org.koitharu.kotatsu.ui.common
import android.content.Context
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
@ -13,8 +12,8 @@ abstract class BasePreferenceFragment(@StringRes private val titleId: Int) :
protected val settings by inject<AppSettings>() protected val settings by inject<AppSettings>()
override fun onAttach(context: Context) { override fun onResume() {
super.onAttach(context) super.onResume()
activity?.setTitle(titleId) activity?.setTitle(titleId)
} }

@ -3,12 +3,15 @@ package org.koitharu.kotatsu.ui.settings
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatDelegate
import androidx.collection.arrayMapOf import androidx.collection.arrayMapOf
import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.prefs.ListMode import org.koitharu.kotatsu.core.prefs.ListMode
import org.koitharu.kotatsu.ui.common.BasePreferenceFragment import org.koitharu.kotatsu.ui.common.BasePreferenceFragment
import org.koitharu.kotatsu.ui.main.list.ListModeSelectDialog import org.koitharu.kotatsu.ui.main.list.ListModeSelectDialog
import org.koitharu.kotatsu.utils.ext.bindSummary
class AppearanceSettingsFragment : BasePreferenceFragment(R.string.appearance), class AppearanceSettingsFragment : BasePreferenceFragment(R.string.appearance),
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
@ -18,6 +21,7 @@ class AppearanceSettingsFragment : BasePreferenceFragment(R.string.appearance),
findPreference<Preference>(R.string.key_list_mode)?.summary = findPreference<Preference>(R.string.key_list_mode)?.summary =
listModes[settings.listMode]?.let(::getString) listModes[settings.listMode]?.let(::getString)
findPreference<ListPreference>(R.string.key_theme)?.bindSummary()
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -44,6 +48,9 @@ class AppearanceSettingsFragment : BasePreferenceFragment(R.string.appearance),
when (key) { when (key) {
getString(R.string.key_list_mode) -> findPreference<Preference>(R.string.key_list_mode)?.summary = getString(R.string.key_list_mode) -> findPreference<Preference>(R.string.key_list_mode)?.summary =
listModes[settings.listMode]?.let(::getString) listModes[settings.listMode]?.let(::getString)
getString(R.string.key_theme) -> {
AppCompatDelegate.setDefaultNightMode(settings.theme)
}
} }
} }

@ -0,0 +1,20 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class StringIntPreferenceDelegate(private val key: String, private val defValue: Int) :
ReadWriteProperty<SharedPreferences, Int> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Int {
return thisRef.getString(key, defValue.toString())?.toIntOrNull() ?: defValue
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Int) {
thisRef.edit {
putString(key, value.toString())
}
}
}

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes">
<item>@string/automatic</item>
<item>@string/light</item>
<item>@string/dark</item>
</string-array>
</resources>

@ -1,4 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="key_list_mode">list_mode</string> <string name="key_list_mode">list_mode</string>
<string name="key_theme">theme</string>
<string-array name="values_theme">
<item>-1</item>
<item>1</item>
<item>2</item>
</string-array>
</resources> </resources>

@ -56,4 +56,8 @@
<string name="genre">Genre</string> <string name="genre">Genre</string>
<string name="filter">Filter</string> <string name="filter">Filter</string>
<string name="appearance">Appearance</string> <string name="appearance">Appearance</string>
<string name="theme">Theme</string>
<string name="light">Light</string>
<string name="dark">Dark</string>
<string name="automatic">Automatic</string>
</resources> </resources>

@ -3,10 +3,19 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<ListPreference
android:defaultValue="-1"
android:entries="@array/themes"
android:entryValues="@array/values_theme"
android:key="@string/key_theme"
android:title="@string/theme"
app:iconSpaceReserved="false" />
<Preference <Preference
android:key="@string/key_list_mode" android:key="@string/key_list_mode"
android:title="@string/list_mode"
android:persistent="false" android:persistent="false"
android:title="@string/list_mode"
app:allowDividerAbove="true"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
</PreferenceScreen> </PreferenceScreen>
Loading…
Cancel
Save