forked from openMF/android-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor Settings fragment to compose
- Loading branch information
1 parent
5705fb7
commit eef9e47
Showing
15 changed files
with
820 additions
and
140 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
core/common/src/main/java/com/mifos/core/common/enums/MifosAppLanguage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mifos.core.common.enums | ||
|
||
enum class MifosAppLanguage(val code: String, val displayName: String) { | ||
|
||
SYSTEM_LANGUAGE("System_Language", "System Language"), | ||
ENGLISH("en", "English"), | ||
HINDI("hi", "हिंदी"), | ||
ARABIC("ar", "عربى"), | ||
URDU("ur", "اُردُو"), | ||
BENGALI("bn", "বাঙালি"), | ||
SPANISH("es", "Español"), | ||
FRENCH("fr", "français"), | ||
INDONESIAN("in", "bahasa Indonesia"), | ||
KHMER("km", "ភាសាខ្មែរ"), | ||
KANNADA("kn", "ಕನ್ನಡ"), | ||
TELUGU("te", "తెలుగు"), | ||
BURMESE("my", "မြန်မာ"), | ||
POLISH("pl", "Polski"), | ||
PORTUGUESE("pt", "Português"), | ||
RUSSIAN("ru", "русский"), | ||
SWAHILI("sw", "Kiswahili"), | ||
FARSI("fa", "فارسی"); | ||
|
||
companion object { | ||
fun fromCode(code: String): MifosAppLanguage { | ||
return entries.find { it.code.equals(code, ignoreCase = true) } ?: ENGLISH | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
return displayName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
core/common/src/main/java/com/mifos/core/common/utils/LanguageHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.mifos.core.common.utils | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import android.preference.PreferenceManager | ||
import com.mifos.core.common.R | ||
import java.util.Locale | ||
|
||
object LanguageHelper { | ||
// https://gunhansancar.com/change-language-programmatically-in-android/ | ||
fun onAttach(context: Context): Context? { | ||
val preferences = PreferenceManager.getDefaultSharedPreferences(context) | ||
return if (preferences.getBoolean( | ||
context.getString(R.string.core_common_default_system_language), | ||
true | ||
) | ||
) { | ||
if (!context.resources.getStringArray(R.array.core_common_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 | ||
fun onAttach(context: Context, defaultLanguage: String): Context? { | ||
val lang = getPersistedData(context, defaultLanguage) | ||
return lang?.let { setLocale(context, it) } | ||
} | ||
|
||
fun setLocale(context: Context?, language: String): Context? { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
updateResources(context!!, language) | ||
} else { | ||
updateResourcesLegacy(context, language) | ||
} | ||
} | ||
|
||
private fun getPersistedData(context: Context, defaultLanguage: String): String? { | ||
val preferences = PreferenceManager.getDefaultSharedPreferences(context) | ||
return preferences.getString( | ||
context.getString(R.string.core_common_language_type), | ||
defaultLanguage | ||
) | ||
} | ||
|
||
private fun updateResources(context: Context, language: String): Context { | ||
val locale = Locale(language) | ||
Locale.setDefault(locale) | ||
val configuration = context.resources.configuration | ||
configuration.setLocale(locale) | ||
configuration.setLayoutDirection(locale) | ||
return context.createConfigurationContext(configuration) | ||
} | ||
|
||
private fun updateResourcesLegacy(context: Context?, language: String): Context? { | ||
val locale = Locale(language) | ||
Locale.setDefault(locale) | ||
val resources = context?.resources | ||
val configuration = resources?.configuration | ||
configuration?.locale = locale | ||
configuration?.setLayoutDirection(locale) | ||
resources?.updateConfiguration(configuration, resources.displayMetrics) | ||
return context | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
|
||
<string name="core_common_default_system_language" translatable="false">default_system_language</string> | ||
<string name="core_common_language_type" translatable="false">language_type</string> | ||
|
||
<string-array name="core_common_languages_value" translatable="false"> | ||
<item>System_Language</item> | ||
<item>en</item> | ||
<item>hi</item> | ||
<item>ar</item> | ||
<item>ur</item> | ||
<item>bn</item> | ||
<item>es</item> | ||
<item>fr</item> | ||
<item>in</item> | ||
<item>km</item> | ||
<item>kn</item> | ||
<item>te</item> | ||
<item>my</item> | ||
<item>pl</item> | ||
<item>pt</item> | ||
<item>ru</item> | ||
<item>sw</item> | ||
<item>fa</item> | ||
</string-array> | ||
|
||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.