Add theme settings

This commit is contained in:
Koitharu
2020-02-21 18:57:50 +02:00
parent 21c2a4aa9a
commit 2dc5840872
9 changed files with 63 additions and 4 deletions

View File

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

View File

@@ -3,9 +3,11 @@ package org.koitharu.kotatsu.core.prefs
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
import org.koitharu.kotatsu.R
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 {
@@ -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)
val theme by StringIntPreferenceDelegate(resources.getString(R.string.key_theme), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
fun subscribe(listener: SharedPreferences.OnSharedPreferenceChangeListener) {
prefs.registerOnSharedPreferenceChangeListener(listener)
}

View File

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

View File

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

View File

@@ -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())
}
}
}

View File

@@ -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>

View File

@@ -1,4 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>

View File

@@ -56,4 +56,8 @@
<string name="genre">Genre</string>
<string name="filter">Filter</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>

View File

@@ -3,10 +3,19 @@
xmlns:android="http://schemas.android.com/apk/res/android"
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
android:key="@string/key_list_mode"
android:title="@string/list_mode"
android:persistent="false"
android:title="@string/list_mode"
app:allowDividerAbove="true"
app:iconSpaceReserved="false" />
</PreferenceScreen>