Update. Add language setting.
This commit is contained in:
34
app/src/main/java/me/zobrist/tichucounter/ContextUtils.kt
Normal file
34
app/src/main/java/me/zobrist/tichucounter/ContextUtils.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package me.zobrist.tichucounter
|
||||
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import android.os.LocaleList
|
||||
import java.util.*
|
||||
|
||||
class ContextUtils(base: Context) : ContextWrapper(base) {
|
||||
|
||||
companion object {
|
||||
|
||||
fun updateLocale(c: Context, localeToSwitchTo: Locale): ContextWrapper {
|
||||
var context = c
|
||||
val resources: Resources = context.resources
|
||||
val configuration: Configuration = resources.configuration
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
val localeList = LocaleList(localeToSwitchTo)
|
||||
LocaleList.setDefault(localeList)
|
||||
configuration.setLocales(localeList)
|
||||
} else {
|
||||
configuration.locale = localeToSwitchTo
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
context = context.createConfigurationContext(configuration)
|
||||
} else {
|
||||
resources.updateConfiguration(configuration, resources.displayMetrics)
|
||||
}
|
||||
return ContextUtils(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package me.zobrist.tichucounter
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.text.InputType
|
||||
import android.view.Menu
|
||||
@@ -14,6 +16,7 @@ import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.widget.doOnTextChanged
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.android.synthetic.main.content_main.*
|
||||
import java.util.*
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -21,6 +24,15 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var history: History
|
||||
private var currentRound = Round()
|
||||
private var systemLocale = Locale.getDefault()
|
||||
|
||||
override fun attachBaseContext(newBase: Context) {
|
||||
// get chosen language from shread preference
|
||||
val localeString = newBase.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", systemLocale.toString())
|
||||
val localeToSwitchTo = Locale(localeString)
|
||||
val localeUpdatedContext: ContextWrapper = ContextUtils.updateLocale(newBase, localeToSwitchTo)
|
||||
super.attachBaseContext(localeUpdatedContext)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -36,26 +48,32 @@ class MainActivity : AppCompatActivity() {
|
||||
.getBoolean("Screen_On", false)
|
||||
)
|
||||
|
||||
|
||||
val json = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("history", "{\"scores\":[]}")
|
||||
history = Gson().fromJson(json, History::class.java)
|
||||
nameTeamA.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamA", "TeamA"))
|
||||
nameTeamB.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamB", "TeamB"))
|
||||
updateView()
|
||||
|
||||
this.setListenes()
|
||||
|
||||
inputTeamA.setOnFocusChangeListener { view, b ->
|
||||
}
|
||||
|
||||
private fun setListenes()
|
||||
{
|
||||
inputTeamA.setOnFocusChangeListener { _, b ->
|
||||
if (b) {
|
||||
hideKeyboard()
|
||||
}
|
||||
}
|
||||
|
||||
inputTeamB.setOnFocusChangeListener { view, b ->
|
||||
inputTeamB.setOnFocusChangeListener { _, b ->
|
||||
if (b) {
|
||||
hideKeyboard()
|
||||
}
|
||||
}
|
||||
|
||||
inputTeamA.doOnTextChanged { text, start, count, after ->
|
||||
inputTeamA.doOnTextChanged { text, _, _, _ ->
|
||||
if (inputTeamA.isFocused) {
|
||||
if (inputTeamA.text.isNotEmpty()) {
|
||||
if (updateOnChange) {
|
||||
@@ -82,9 +100,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
inputTeamB.doOnTextChanged { text, start, count, after ->
|
||||
inputTeamB.doOnTextChanged { text, _, _, _ ->
|
||||
if (inputTeamB.isFocused) {
|
||||
if (inputTeamB.text.isNotEmpty()) {
|
||||
if (updateOnChange) {
|
||||
@@ -323,11 +339,11 @@ class MainActivity : AppCompatActivity() {
|
||||
builder.setMessage(getString(R.string.confirmClear))
|
||||
.setTitle(R.string.clear)
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(getString(R.string.yes)) { dialog, id ->
|
||||
.setPositiveButton(getString(R.string.yes)) { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
clearAll()
|
||||
}
|
||||
.setNegativeButton(getString(R.string.no)) { dialog, id ->
|
||||
.setNegativeButton(getString(R.string.no)) { dialog, _ ->
|
||||
dialog.cancel()
|
||||
}
|
||||
|
||||
@@ -342,6 +358,10 @@ class MainActivity : AppCompatActivity() {
|
||||
chooseThemeDialog()
|
||||
true
|
||||
}
|
||||
R.id.action_language -> {
|
||||
chooseLanguageDialog()
|
||||
true
|
||||
}
|
||||
R.id.action_screenOn -> {
|
||||
item.isChecked = !item.isChecked
|
||||
keepScreenOn(item.isChecked)
|
||||
@@ -409,7 +429,7 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
val builder = AlertDialog.Builder(this)
|
||||
builder.setTitle(getString(R.string.choose_theme_text))
|
||||
val styles = arrayOf("Light", "Dark", "System default")
|
||||
val styles = arrayOf(getString(R.string.light), getString(R.string.dark), getString(R.string.android_default_text))
|
||||
|
||||
val checkedItem =
|
||||
this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getInt("Theme", 2)
|
||||
@@ -431,6 +451,42 @@ class MainActivity : AppCompatActivity() {
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun chooseLanguageDialog() {
|
||||
|
||||
val builder = AlertDialog.Builder(this)
|
||||
builder.setTitle(getString(R.string.choose_language_text))
|
||||
|
||||
val languages_map = mapOf(
|
||||
getString(R.string.android_default_text) to systemLocale.toString(),
|
||||
getString(R.string.english) to "en",
|
||||
getString(R.string.german) to "de")
|
||||
|
||||
val languages_display_keys = languages_map.keys.toTypedArray()
|
||||
val languages_display_values = languages_map.values.toTypedArray()
|
||||
|
||||
|
||||
val checkedItem = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", R.string.android_default_text.toString())
|
||||
val checkedItemIndex = languages_display_values.indexOf(checkedItem)
|
||||
|
||||
val prefs = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
|
||||
|
||||
|
||||
builder.setSingleChoiceItems(languages_display_keys, checkedItemIndex) { dialog, which ->
|
||||
|
||||
val temp = languages_map[languages_display_keys[which]]
|
||||
prefs.putString("Language", temp)
|
||||
prefs.apply()
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user