Add: Lanugage preference listener

This commit is contained in:
machiav3lli 2021-11-16 23:48:34 +01:00
parent 606372bc14
commit a8dc3a255b
2 changed files with 29 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package com.looker.droidify
import android.content.Context
import android.content.Intent
import com.looker.droidify.ContextWraperX.Companion.wrap
import com.looker.droidify.screen.ScreenActivity
class MainActivity : ScreenActivity() {
@ -23,4 +25,8 @@ class MainActivity : ScreenActivity() {
else -> super.handleIntent(intent)
}
}
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(wrap(newBase))
}
}

View File

@ -16,6 +16,7 @@ import com.looker.droidify.network.CoilDownloader
import com.looker.droidify.network.Downloader
import com.looker.droidify.service.Connection
import com.looker.droidify.service.SyncService
import com.looker.droidify.utility.Utils.setLanguage
import com.looker.droidify.utility.Utils.toInstalledItem
import com.looker.droidify.utility.extension.android.Android
import kotlinx.coroutines.MainScope
@ -87,6 +88,7 @@ class MainApplication : Application(), ImageLoaderFactory {
updateProxy()
var lastAutoSync = Preferences[Preferences.Key.AutoSync]
var lastUpdateUnstable = Preferences[Preferences.Key.UpdateUnstable]
var lastLanguage = Preferences[Preferences.Key.Language]
MainScope().launch {
Preferences.subject.collect {
if (it == Preferences.Key.ProxyType || it == Preferences.Key.ProxyHost || it == Preferences.Key.ProxyPort) {
@ -103,6 +105,18 @@ class MainApplication : Application(), ImageLoaderFactory {
lastUpdateUnstable = updateUnstable
forceSyncAll()
}
} else if (it == Preferences.Key.Language) {
val language = Preferences[Preferences.Key.Language]
if (language != lastLanguage) {
lastLanguage = language
val refresh = Intent.makeRestartActivityTask(
ComponentName(
baseContext,
MainActivity::class.java
)
)
applicationContext.startActivity(refresh)
}
}
}
}
@ -189,3 +203,12 @@ class MainApplication : Application(), ImageLoaderFactory {
.build()
}
}
class ContextWraperX(base: Context) : ContextWrapper(base) {
companion object {
fun wrap(context: Context): ContextWrapper {
val config = context.setLanguage()
return ContextWraperX(context.createConfigurationContext(config))
}
}
}