Store theme settings and apply at startup.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package me.zobrist.tichucounter
|
package me.zobrist.tichucounter
|
||||||
|
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
@@ -36,7 +38,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
inputTeamB.setRawInputType(InputType.TYPE_NULL)
|
inputTeamB.setRawInputType(InputType.TYPE_NULL)
|
||||||
inputTeamA.requestFocus()
|
inputTeamA.requestFocus()
|
||||||
disableSubmitButton()
|
disableSubmitButton()
|
||||||
|
updateTheme(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getInt("Theme", 2))
|
||||||
|
|
||||||
|
|
||||||
inputTeamA.doOnTextChanged { text, start, count, after ->
|
inputTeamA.doOnTextChanged { text, start, count, after ->
|
||||||
@@ -330,37 +332,31 @@ class MainActivity : AppCompatActivity() {
|
|||||||
builder.setTitle(getString(R.string.choose_theme_text))
|
builder.setTitle(getString(R.string.choose_theme_text))
|
||||||
val styles = arrayOf("Light","Dark","System default")
|
val styles = arrayOf("Light","Dark","System default")
|
||||||
|
|
||||||
var checkedItem = when(AppCompatDelegate.getDefaultNightMode()) {
|
var checkedItem = this.getSharedPreferences("", Context.MODE_PRIVATE).getInt("Theme", 2)
|
||||||
AppCompatDelegate.MODE_NIGHT_NO -> 0
|
|
||||||
AppCompatDelegate.MODE_NIGHT_YES -> 1
|
var prefs = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
|
||||||
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> 2
|
|
||||||
else -> -1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
builder.setSingleChoiceItems(styles, checkedItem) { dialog, which ->
|
builder.setSingleChoiceItems(styles, checkedItem) { dialog, which ->
|
||||||
|
|
||||||
when (which) {
|
prefs.putInt("Theme", which)
|
||||||
0 -> {
|
prefs.apply()
|
||||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
|
||||||
delegate.applyDayNight()
|
|
||||||
dialog.dismiss()
|
|
||||||
}
|
|
||||||
1 -> {
|
|
||||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
|
||||||
delegate.applyDayNight()
|
|
||||||
|
|
||||||
dialog.dismiss()
|
updateTheme(which)
|
||||||
}
|
|
||||||
2 -> {
|
dialog.dismiss()
|
||||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
|
||||||
delegate.applyDayNight()
|
|
||||||
dialog.dismiss()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val dialog = builder.create()
|
val dialog = builder.create()
|
||||||
dialog.show()
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user