Skip to content

Commit

Permalink
Fix: #2068 and #2397 Fixed Theme Issue and Added System Language Option
Browse files Browse the repository at this point in the history
  • Loading branch information
SekhGulamMainuddin authored and therajanmaurya committed Feb 14, 2024
1 parent b4ba372 commit 1f01942
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
8 changes: 6 additions & 2 deletions app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.mifos.mobile.utils.ConfigurationDialogFragmentCompat
import org.mifos.mobile.utils.ConfigurationPreference
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.LanguageHelper
import java.util.Locale

/**
* Created by dilpreet on 02/10/17.
Expand Down Expand Up @@ -132,7 +133,23 @@ 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])
prefsHelper.putBoolean(
context?.getString(R.string.default_system_language),
isSystemLanguage
)
if (!isSystemLanguage) {
LanguageHelper.setLocale(context, preference.value)
} else {
if (!resources.getStringArray(R.array.languages_value)
.contains(Locale.getDefault().language)
) {
LanguageHelper.setLocale(context, "en")
} else {
LanguageHelper.setLocale(context, Locale.getDefault().language)
}
}
val intent = Intent(activity, activity?.javaClass)
intent.putExtra(Constants.HAS_SETTINGS_CHANGED, true)
startActivity(intent)
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.default_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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
</string-array>

<string name="language_type" translatable="false">language_type</string>
<string name="default_system_language" translatable="false">default_system_language</string>
<string name="theme_type" translatable="false">theme_type</string>
<string name="total_saving_balance">Total Savings Balance</string>
<string name="total_loan_balance">Total Loan Balance</string>
Expand Down Expand Up @@ -600,6 +601,7 @@
<string name="change_app_theme">Change the application theme</string>

<string-array name="languages" translatable="false">
<item>System Language</item>
<item>English</item>
<item>हिंदी</item>
<item>عربى</item>
Expand All @@ -620,6 +622,7 @@
</string-array>

<string-array name="languages_value" translatable="false">
<item>System_Language</item>
<item>en</item>
<item>hi</item>
<item>ar</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/settings_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<PreferenceCategory android:title="@string/other">
<androidx.preference.ListPreference
android:defaultValue="en"
android:defaultValue="System_Language"
android:entries="@array/languages"
android:entryValues="@array/languages_value"
android:icon="@drawable/ic_translate"
Expand Down

0 comments on commit 1f01942

Please sign in to comment.