From 8f71af48414c5fce7b2f1e70cdc44f0eb788bb96 Mon Sep 17 00:00:00 2001 From: Sekh Gulam Mainuddin Date: Sun, 15 Oct 2023 19:47:46 +0530 Subject: [PATCH] Fix: #2397 and #2398 Fixed Theme Issue and Added System Language Option --- .../org/mifos/mobile/MifosSelfServiceApp.kt | 8 ++++++-- .../mobile/ui/fragments/SettingsFragment.kt | 8 +++++++- .../org/mifos/mobile/utils/LanguageHelper.kt | 17 ++++++++++++----- app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/xml/settings_preference.xml | 2 +- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt b/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt index 6029990435..0de31d6f74 100644 --- a/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt +++ b/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt @@ -1,6 +1,7 @@ package org.mifos.mobile import android.content.Context +import android.content.res.Configuration import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex import androidx.multidex.MultiDexApplication @@ -11,6 +12,7 @@ import com.raizlabs.android.dbflow.config.FlowManager import dagger.hilt.android.HiltAndroidApp import org.mifos.mobile.api.local.PreferencesHelper import org.mifos.mobile.ui.fragments.applySavedTheme +import org.mifos.mobile.utils.LanguageHelper import org.mifos.mobile.utils.LanguageHelper.onAttach import java.util.Locale @@ -45,7 +47,9 @@ class MifosSelfServiceApp : MultiDexApplication() { PreferencesHelper(this).applySavedTheme() } - override fun attachBaseContext(base: Context) { - super.attachBaseContext(onAttach(base, Locale.getDefault().language)) + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + context?.let { onAttach(it) } } + } diff --git a/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt b/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt index 352a370a94..45eabaa744 100644 --- a/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt +++ b/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt @@ -10,6 +10,7 @@ import androidx.fragment.app.DialogFragment import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat +import androidx.preference.PreferenceManager import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.mifos.mobile.passcode.utils.PasscodePreferencesHelper import dagger.hilt.android.AndroidEntryPoint @@ -132,7 +133,12 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis override fun onSharedPreferenceChanged(p0: SharedPreferences?, p1: String?) { val preference = findPreference(p1) if (preference is ListPreference) { - LanguageHelper.setLocale(context, preference.value) + val isSystemLanguage = (preference.value == resources.getStringArray(R.array.languages_value)[0]) + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + preferences.edit().putBoolean(context?.getString(R.string.system_language), isSystemLanguage).apply() + if(!isSystemLanguage){ + LanguageHelper.setLocale(context, preference.value) + } val intent = Intent(activity, activity?.javaClass) intent.putExtra(Constants.HAS_SETTINGS_CHANGED, true) startActivity(intent) diff --git a/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt b/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt index d10bd15224..abda9efd05 100644 --- a/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt +++ b/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt @@ -13,8 +13,17 @@ import java.util.* object LanguageHelper { // https://gunhansancar.com/change-language-programmatically-in-android/ fun onAttach(context: Context): Context? { - val lang = getPersistedData(context, Locale.getDefault().language) - return lang?.let { setLocale(context, it) } + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + return if (preferences.getBoolean(context.getString(R.string.system_language), true)) { + if(!context.resources.getStringArray(R.array.languages_value).contains(Locale.getDefault().language)){ + setLocale(context, "en") + }else{ + setLocale(context, Locale.getDefault().language) + } + } else { + val lang = getPersistedData(context, Locale.getDefault().language) + lang?.let { setLocale(context, it) } + } } @JvmStatic @@ -52,9 +61,7 @@ object LanguageHelper { val resources = context?.resources val configuration = resources?.configuration configuration?.locale = locale - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - configuration?.setLayoutDirection(locale) - } + configuration?.setLayoutDirection(locale) resources?.updateConfiguration(configuration, resources.displayMetrics) return context } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 06e8f8b0e2..ddaee249b1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -535,6 +535,7 @@ language_type + system_language theme_type Total Savings Balance Total Loan Balance @@ -600,6 +601,7 @@ Change the application theme + System Language English हिंदी عربى @@ -620,6 +622,7 @@ + System_Language en hi ar diff --git a/app/src/main/res/xml/settings_preference.xml b/app/src/main/res/xml/settings_preference.xml index 483335fe9a..a33ab733ec 100644 --- a/app/src/main/res/xml/settings_preference.xml +++ b/app/src/main/res/xml/settings_preference.xml @@ -19,7 +19,7 @@