Replaced theme switches with selector dialog

This commit is contained in:
Corewala 2021-12-08 13:57:06 -05:00
parent aba73330f3
commit 16ea35c18c
4 changed files with 33 additions and 42 deletions

View File

@ -1,24 +1,9 @@
package corewala.buran package corewala.buran
import android.app.Application import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
class Buran: Application() { class Buran: Application() {
override fun onCreate() {
super.onCreate()
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
when {
prefs.getBoolean("theme_Light", false) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
prefs.getBoolean("theme_Dark", false) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
prefs.getBoolean("theme_FollowSystem", true) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
companion object{ companion object{
const val DEFAULT_HOME_CAPSULE = "gemini://rawtext.club/~sloum/spacewalk.gmi" const val DEFAULT_HOME_CAPSULE = "gemini://rawtext.club/~sloum/spacewalk.gmi"

View File

@ -113,40 +113,44 @@ class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChang
themeCategory.title = getString(R.string.theme) themeCategory.title = getString(R.string.theme)
appCategory.addPreference(themeCategory) appCategory.addPreference(themeCategory)
val themeFollowSystemPreference = SwitchPreferenceCompat(context) val themeLabels = mutableListOf<String>()
themeFollowSystemPreference.key = "theme_FollowSystem" val themeValues = mutableListOf<String>()
themeFollowSystemPreference.title = getString(R.string.system_default) themeLabels.add(getString(R.string.system_default))
themeFollowSystemPreference.onPreferenceChangeListener = this themeLabels.add(getString(R.string.light))
themeCategory.addPreference(themeFollowSystemPreference) themeLabels.add(getString(R.string.dark))
themeValues.add("theme_FollowSystem")
themeValues.add("theme_Light")
themeValues.add("theme_Dark")
val themeLightPreference = SwitchPreferenceCompat(context) val themePreference = ListPreference(context)
themeLightPreference.key = "theme_Light" themePreference.key = "theme"
themeLightPreference.title = getString(R.string.light) themePreference.setDialogTitle(R.string.theme)
themeLightPreference.onPreferenceChangeListener = this themePreference.setTitle(R.string.theme)
themeCategory.addPreference(themeLightPreference) themePreference.setSummary(R.string.prefs_override_theme)
themePreference.setDefaultValue("theme_FollowSystem")
themePreference.entries = themeLabels.toTypedArray()
themePreference.entryValues = themeValues.toTypedArray()
themeCategory.addPreference(themePreference)
val themeDarkPreference = SwitchPreferenceCompat(context) themePreference.setOnPreferenceChangeListener{ _, theme ->
themeDarkPreference.key = "theme_Dark" when (theme) {
themeDarkPreference.title = getString(R.string.dark) "theme_FollowSystem" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
themeDarkPreference.onPreferenceChangeListener = this "theme_Light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
themeCategory.addPreference(themeDarkPreference) "theme_Dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
true
val isThemePrefSet = }
prefs.getBoolean("theme_FollowSystem", false) ||
prefs.getBoolean("theme_Light", false) ||
prefs.getBoolean("theme_Dark", false)
if (!isThemePrefSet) themeFollowSystemPreference.isChecked = true
val coloursCSV = resources.openRawResource(R.raw.colours).bufferedReader().use { it.readLines() } val coloursCSV = resources.openRawResource(R.raw.colours).bufferedReader().use { it.readLines() }
val labels = mutableListOf<String>() val colourLabels = mutableListOf<String>()
val values = mutableListOf<String>() val colourValues = mutableListOf<String>()
coloursCSV.forEach{ line -> coloursCSV.forEach{ line ->
val colour = line.split(",") val colour = line.split(",")
labels.add(colour[0]) colourLabels.add(colour[0])
values.add(colour[1]) colourValues.add(colour[1])
} }
val backgroundColourPreference = ListPreference(context) val backgroundColourPreference = ListPreference(context)
@ -155,8 +159,8 @@ class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChang
backgroundColourPreference.setTitle(R.string.prefs_override_page_background_title) backgroundColourPreference.setTitle(R.string.prefs_override_page_background_title)
backgroundColourPreference.setSummary(R.string.prefs_override_page_background) backgroundColourPreference.setSummary(R.string.prefs_override_page_background)
backgroundColourPreference.setDefaultValue("#XXXXXX") backgroundColourPreference.setDefaultValue("#XXXXXX")
backgroundColourPreference.entries = labels.toTypedArray() backgroundColourPreference.entries = colourLabels.toTypedArray()
backgroundColourPreference.entryValues = values.toTypedArray() backgroundColourPreference.entryValues = colourValues.toTypedArray()
backgroundColourPreference.setOnPreferenceChangeListener { _, colour -> backgroundColourPreference.setOnPreferenceChangeListener { _, colour ->
when (colour) { when (colour) {

View File

@ -46,6 +46,7 @@
<string name="home_capsule">Capsule d\'accueil</string> <string name="home_capsule">Capsule d\'accueil</string>
<string name="update">Mettre à jour</string> <string name="update">Mettre à jour</string>
<string name="theme">Thème</string> <string name="theme">Thème</string>
<string name="prefs_override_theme">Choisir un thème</string>
<string name="system_default">Paramètre système</string> <string name="system_default">Paramètre système</string>
<string name="light">Clair</string> <string name="light">Clair</string>
<string name="dark">Sombre</string> <string name="dark">Sombre</string>

View File

@ -46,6 +46,7 @@
<string name="home_capsule">Home Capsule</string> <string name="home_capsule">Home Capsule</string>
<string name="update">Update</string> <string name="update">Update</string>
<string name="theme">Theme</string> <string name="theme">Theme</string>
<string name="prefs_override_theme">Select a theme</string>
<string name="system_default">System Default</string> <string name="system_default">System Default</string>
<string name="light">Light</string> <string name="light">Light</string>
<string name="dark">Dark</string> <string name="dark">Dark</string>