Store theme settings and apply at startup.

This commit is contained in:
2020-08-20 00:07:09 +02:00
parent 26495f11a3
commit 1a2a724bb2

View File

@@ -1,6 +1,8 @@
package me.zobrist.tichucounter
import android.app.AlertDialog
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
@@ -36,7 +38,7 @@ class MainActivity : AppCompatActivity() {
inputTeamB.setRawInputType(InputType.TYPE_NULL)
inputTeamA.requestFocus()
disableSubmitButton()
updateTheme(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getInt("Theme", 2))
inputTeamA.doOnTextChanged { text, start, count, after ->
@@ -330,37 +332,31 @@ class MainActivity : AppCompatActivity() {
builder.setTitle(getString(R.string.choose_theme_text))
val styles = arrayOf("Light","Dark","System default")
var checkedItem = when(AppCompatDelegate.getDefaultNightMode()) {
AppCompatDelegate.MODE_NIGHT_NO -> 0
AppCompatDelegate.MODE_NIGHT_YES -> 1
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> 2
else -> -1
}
var checkedItem = this.getSharedPreferences("", Context.MODE_PRIVATE).getInt("Theme", 2)
var prefs = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
builder.setSingleChoiceItems(styles, checkedItem) { dialog, which ->
when (which) {
0 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
delegate.applyDayNight()
dialog.dismiss()
}
1 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
delegate.applyDayNight()
prefs.putInt("Theme", which)
prefs.apply()
dialog.dismiss()
}
2 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
delegate.applyDayNight()
dialog.dismiss()
}
}
updateTheme(which)
dialog.dismiss()
}
val dialog = builder.create()
dialog.show()
}
private fun updateTheme(which: Int) {
when (which) {
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
delegate.applyDayNight()
}
}