Commit 87722d94 authored by Hussein El Feky's avatar Hussein El Feky

Refactored code

parent 0cd0d777
...@@ -31,6 +31,24 @@ import timber.log.Timber ...@@ -31,6 +31,24 @@ import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment" internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment"
private val LOCALES = arrayOf(
"en",
"ar",
"de",
"es",
"fa",
"fr",
"hi,IN",
"it",
"ja",
"pt,BR",
"pt,PT",
"ru,RU",
"tr",
"uk",
"zh,CN",
"zh,TW"
)
fun newInstance(): Fragment = SettingsFragment() fun newInstance(): Fragment = SettingsFragment()
...@@ -165,37 +183,12 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView { ...@@ -165,37 +183,12 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
private fun changeLanguage() { private fun changeLanguage() {
context?.let { context?.let {
// Add [String] for locales without specified countries, and
// add [Pair] for locales with specified countries.
val locales = arrayOf(
"en",
"ar",
"de",
"es",
"fa",
"fr",
Pair("hi", "IN"),
"it",
"ja",
Pair("pt", "BR"),
Pair("pt", "PT"),
Pair("ru", "RU"),
"tr",
"uk",
Pair("zh", "CN"),
Pair("zh", "TW")
)
val selectedLocale = presenter.getCurrentLocale(it) val selectedLocale = presenter.getCurrentLocale(it)
var localeIndex = -1 var localeIndex = -1
locales.forEachIndexed { index, locale -> LOCALES.forEachIndexed { index, locale ->
var language: String? = null val array = locale.split(",")
var country = "" val language = array[0]
if (locale is String) { val country = if (array.size > 1) array[1] else ""
language = locale
} else if (locale is Pair<*, *>) {
language = locale.first as String
country = locale.second as String
}
// If language and country are specified, return the respective locale, else return // If language and country are specified, return the respective locale, else return
// the first locale found if the language is as specified regardless of the country. // the first locale found if the language is as specified regardless of the country.
if (language == selectedLocale.language) { if (language == selectedLocale.language) {
...@@ -212,11 +205,11 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView { ...@@ -212,11 +205,11 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
.setSingleChoiceItems( .setSingleChoiceItems(
resources.getStringArray(R.array.languages), localeIndex resources.getStringArray(R.array.languages), localeIndex
) { dialog, option -> ) { dialog, option ->
if (locales[option] is String) { val array = LOCALES[option].split(",")
updateLanguage(locales[option] as String) if (array.size > 1) {
} else if (locales[option] is Pair<*, *>) { updateLanguage(array[0], array[1])
updateLanguage((locales[option] as Pair<*, *>).first as String, } else {
(locales[option] as Pair<*, *>).second as String) updateLanguage(array[0])
} }
dialog.dismiss() dialog.dismiss()
} }
...@@ -263,4 +256,4 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView { ...@@ -263,4 +256,4 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
.show() .show()
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment