Apply application preferences.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-31 11:54:20 +01:00
parent 3b7b71ce77
commit e01df3a5c7
11 changed files with 124 additions and 161 deletions

View File

@@ -11,6 +11,11 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".SettingsService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".SettingsActivity"
android:exported="false"

View File

@@ -0,0 +1,67 @@
package me.zobrist.tichucounter
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.preference.PreferenceManager
abstract class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
keepScreenOn(sharedPreferences.getBoolean("screen_on", false))
updateTheme(sharedPreferences.getString("theme", null))
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener { sharedPreferences, key ->
if (key == "language") {
setLanguage(sharedPreferences.getString("language", null))
}
if (key == "screen_on") {
keepScreenOn(sharedPreferences.getBoolean("screen_on", false))
}
if (key == "theme") {
updateTheme(sharedPreferences.getString("theme", null))
}
}
}
private fun updateTheme(theme: String?) {
var themeValue = when (theme) {
"light" -> AppCompatDelegate.MODE_NIGHT_NO
"dark" -> AppCompatDelegate.MODE_NIGHT_YES
"default" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
if (themeValue != AppCompatDelegate.getDefaultNightMode()) {
AppCompatDelegate.setDefaultNightMode(themeValue)
delegate.applyDayNight()
}
}
private fun keepScreenOn(keepOn: Boolean) {
if (keepOn) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
private fun setLanguage(locale: String?) {
val currentLocale = AppCompatDelegate.getApplicationLocales()[0].toString()
if (locale != null && locale != currentLocale) {
val newLocale = LocaleListCompat.forLanguageTags(locale)
AppCompatDelegate.setApplicationLocales(newLocale)
}
}
}

View File

@@ -5,11 +5,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@@ -23,7 +19,7 @@ import me.zobrist.tichucounter.repository.GameRepository
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
class MainActivity : BaseActivity() {
private var currentRound: Round = Round(0, 0, null, null)
@@ -45,9 +41,10 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
GlobalScope.launch(Dispatchers.IO) {
val game = gameRepository.getActiveGame()
@@ -56,11 +53,6 @@ class MainActivity : AppCompatActivity() {
}
}
updateTheme(this.getSharedPreferences("Settings", MODE_PRIVATE).getInt("Theme", 2))
keepScreenOn(
this.getSharedPreferences("Settings", MODE_PRIVATE).getBoolean("Screen_On", false)
)
keyboardViewModel.scoreA.observe(this) { value ->
val oldValue = currentRound.scoreA
currentRound.scoreA = value
@@ -98,14 +90,6 @@ class MainActivity : AppCompatActivity() {
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
val prefs = this.getSharedPreferences("Settings", MODE_PRIVATE).edit()
prefs.apply()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
@@ -147,106 +131,7 @@ class MainActivity : AppCompatActivity() {
startActivity(i)
true
}
//R.id.action_theme -> {
// chooseThemeDialog()
// true
//}
//R.id.action_language -> {
// chooseLanguageDialog()
// true
//}
//R.id.action_screenOn -> {
// item.isChecked = !item.isChecked
// keepScreenOn(item.isChecked)
// true
//}
else -> super.onOptionsItemSelected(item)
}
}
private fun chooseThemeDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.choose_theme_text))
val styles = arrayOf(
getString(R.string.light),
getString(R.string.dark),
getString(R.string.android_default_text)
)
val checkedItem = this.getSharedPreferences("Settings", MODE_PRIVATE).getInt("Theme", 2)
val prefs = this.getSharedPreferences("Settings", MODE_PRIVATE).edit()
builder.setSingleChoiceItems(styles, checkedItem) { dialog, which ->
prefs.putInt("Theme", which)
prefs.apply()
updateTheme(which)
dialog.dismiss()
}
val dialog = builder.create()
dialog.show()
}
private fun chooseLanguageDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.choose_language_text))
val languagesMap = mapOf(
getString(R.string.english) to "en", getString(R.string.german) to "de"
)
val languagesDisplayKeys = languagesMap.keys.toTypedArray()
val languagesDisplayValues = languagesMap.values.toTypedArray()
val checkedItem = AppCompatDelegate.getApplicationLocales()[0].toString()
var checkedItemIndex = languagesDisplayValues.indexOf(checkedItem)
if (checkedItemIndex == -1) {
checkedItemIndex = 0
}
builder.setSingleChoiceItems(languagesDisplayKeys, checkedItemIndex) { dialog, which ->
val newLocale =
LocaleListCompat.forLanguageTags(languagesMap[languagesDisplayKeys[which]])
AppCompatDelegate.setApplicationLocales(newLocale)
startActivity(Intent(this, MainActivity::class.java))
finish()
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()
}
private fun keepScreenOn(keepOn: Boolean) {
if (keepOn) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
val prefs = this.getSharedPreferences("Settings", MODE_PRIVATE).edit()
prefs.putBoolean("Screen_On", keepOn)
prefs.apply()
}
}

View File

@@ -1,22 +1,15 @@
package me.zobrist.tichucounter
import android.app.AlertDialog
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import me.zobrist.tichucounter.databinding.SettingsActivityBinding
@AndroidEntryPoint
class SettingsActivity : AppCompatActivity() {
class SettingsActivity : BaseActivity() {
private lateinit var binding: SettingsActivityBinding
@@ -54,8 +47,22 @@ class SettingsActivity : AppCompatActivity() {
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
val index = when (getCurrentLocale()) {
"de" -> 1
"en" -> 0
else -> 0
}
findPreference<ListPreference>("language")?.setValueIndex(index)
}
private fun getCurrentLocale(): String? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
resources.configuration.locales.get(0).language
} else {
resources.configuration.locale.language
}
}
}
}

View File

@@ -1,5 +1,10 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/colorPrimary" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFFFFF"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/colorPrimary"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
</vector>

View File

@@ -1,5 +1,11 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/colorPrimary" android:pathData="M12.5,8c-2.65,0 -5.05,0.99 -6.9,2.6L2,7v9h9l-3.62,-3.62c1.39,-1.16 3.16,-1.88 5.12,-1.88 3.54,0 6.55,2.31 7.6,5.5l2.37,-0.78C21.08,11.03 17.15,8 12.5,8z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="#FFFFFF"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/colorPrimary"
android:pathData="M12.5,8c-2.65,0 -5.05,0.99 -6.9,2.6L2,7v9h9l-3.62,-3.62c1.39,-1.16 3.16,-1.88 5.12,-1.88 3.54,0 6.55,2.31 7.6,5.5l2.37,-0.78C21.08,11.03 17.15,8 12.5,8z" />
</vector>

View File

@@ -10,8 +10,8 @@
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_clear"
android:icon="@drawable/ic_baseline_add_24"
android:checkable="false"
android:icon="@drawable/ic_baseline_add_24"
android:orderInCategory="10"
android:title="@string/clear"
app:showAsAction="ifRoom" />

View File

@@ -15,4 +15,5 @@
<string name="light">Hell</string>
<string name="dark">Dunkel</string>
<string name="settings">Einstellungen</string>
<string name="display">Anzeige</string>
</resources>

View File

@@ -14,12 +14,12 @@
<!-- Language Preference -->
<string-array name="language_entries">
<item>@string/german</item>
<item>@string/english</item>
<item>@string/german</item>
</string-array>
<string-array name="language_values">
<item>german</item>
<item>english</item>
<item>en</item>
<item>de</item>
</string-array>
</resources>

View File

@@ -3,6 +3,7 @@
<string name="app_name" translatable="false">Tichu Counter</string>
<string name="team_a" translatable="false">Team A</string>
<string name="team_b" translatable="false">Team B</string>
<string name="title_activity_settings" translatable="false">SettingsActivity</string>
<string name="clear">Start new game</string>
<string name="undo">Undo last round</string>
@@ -18,21 +19,6 @@
<string name="german">German</string>
<string name="light">Light</string>
<string name="dark">Dark</string>
<string name="title_activity_settings">SettingsActivity</string>
<!-- Preference Titles -->
<string name="messages_header">Messages</string>
<string name="sync_header">Sync</string>
<!-- Messages Preferences -->
<string name="signature_title">Your signature</string>
<string name="reply_title">Theme</string>
<!-- Sync Preferences -->
<string name="sync_title">Sync email periodically</string>
<string name="attachment_title">Download incoming attachments</string>
<string name="attachment_summary_on">Automatically download attachments for incoming emails
</string>
<string name="attachment_summary_off">Only download attachments when manually requested</string>
<string name="display">Display</string>
<string name="settings">Settings</string>
</resources>

View File

@@ -1,6 +1,6 @@
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/messages_header">
<PreferenceCategory app:title="@string/display">
<ListPreference
app:defaultValue="default"
@@ -20,7 +20,8 @@
app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat
app:key="screen"
app:defaultValue="true"
app:key="screen_on"
app:title="@string/keep_screen_on" />
</PreferenceCategory>