LocaleHelper dependency remove

This commit is contained in:
Milos Kozak 2020-03-30 00:57:33 +02:00
parent d1ee12fafc
commit ddd9110912
3 changed files with 11 additions and 9 deletions

View file

@ -92,7 +92,6 @@ public class MainActivity extends NoSplashAppCompatActivity {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Iconify.with(new FontAwesomeModule()); Iconify.with(new FontAwesomeModule());

View file

@ -10,8 +10,8 @@ import info.nightscout.androidaps.utils.LocaleHelper
open class NoSplashAppCompatActivity : DaggerAppCompatActivity() { open class NoSplashAppCompatActivity : DaggerAppCompatActivity() {
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme_NoActionBar)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme_NoActionBar)
} }
public override fun attachBaseContext(newBase: Context) { public override fun attachBaseContext(newBase: Context) {

View file

@ -4,16 +4,19 @@ import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.os.Build import android.os.Build
import android.os.LocaleList import android.os.LocaleList
import androidx.preference.PreferenceManager
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import java.util.* import java.util.*
object LocaleHelper { object LocaleHelper {
fun currentLanguage(): String = private fun currentLanguage(context: Context): String =
SP.getString(R.string.key_language, Locale.getDefault().language) PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "en")
?: "en"
// injection not possible because of use in attachBaseContext
//SP.getString(R.string.key_language, Locale.getDefault().language)
private fun currentLocale(): Locale { private fun currentLocale(context: Context): Locale {
val language = currentLanguage() val language = currentLanguage(context)
var locale = Locale(language) var locale = Locale(language)
if (language.contains("_")) { if (language.contains("_")) {
// language with country like pt_BR defined in arrays.xml // language with country like pt_BR defined in arrays.xml
@ -26,7 +29,7 @@ object LocaleHelper {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
fun update(context: Context) { fun update(context: Context) {
val locale = currentLocale() val locale = currentLocale(context)
Locale.setDefault(locale) Locale.setDefault(locale)
val resources = context.resources val resources = context.resources
val configuration = resources.configuration val configuration = resources.configuration
@ -39,7 +42,7 @@ object LocaleHelper {
fun wrap(ctx: Context): ContextWrapper { fun wrap(ctx: Context): ContextWrapper {
val res = ctx.resources val res = ctx.resources
val configuration = res.configuration val configuration = res.configuration
val newLocale = currentLocale() val newLocale = currentLocale(ctx)
val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale) configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale) val localeList = LocaleList(newLocale)