Commit b91de115 authored by Hussein El Feky's avatar Hussein El Feky

Reformatted code

parent 1f56cb65
package chat.rocket.android.settings.presentation
import android.content.Context
import android.os.Build
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManagerFactory
import chat.rocket.android.helper.UserHelper
import chat.rocket.android.main.presentation.MainNavigator
import chat.rocket.android.server.domain.AnalyticsTrackingInteractor
import chat.rocket.android.server.domain.GetCurrentLanguageInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.RemoveAccountInteractor
......@@ -27,6 +28,7 @@ import chat.rocket.core.internal.rest.serverInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.*
import javax.inject.Inject
import javax.inject.Named
......@@ -41,7 +43,6 @@ class SettingsPresenter @Inject constructor(
private val permissions: PermissionsInteractor,
private val rocketChatClientFactory: RocketChatClientFactory,
private val saveLanguageInteractor: SaveCurrentLanguageInteractor,
val getCurrentLanguageInteractor: GetCurrentLanguageInteractor,
getCurrentServerInteractor: GetCurrentServerInteractor,
removeAccountInteractor: RemoveAccountInteractor,
databaseManagerFactory: DatabaseManagerFactory,
......@@ -127,6 +128,14 @@ class SettingsPresenter @Inject constructor(
}
}
fun getCurrentLocale(context: Context): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.resources.configuration.locales.get(0)
} else {
context.resources.configuration.locale
}
}
fun saveLocale(language: String, country: String? = null) {
saveLanguageInteractor.save(language, country)
}
......
......@@ -165,33 +165,41 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
private fun changeLanguage() {
context?.let {
val locales = ArrayList<Pair<String, String>>()
with(locales) {
add(Pair("en", ""))
add(Pair("ar", ""))
add(Pair("de", ""))
add(Pair("es", ""))
add(Pair("fa", ""))
add(Pair("fr", ""))
add(Pair("hi", "IN"))
add(Pair("it", ""))
add(Pair("ja", ""))
add(Pair("pt", "BR"))
add(Pair("pt", "PT"))
add(Pair("ru", "RU"))
add(Pair("tr", ""))
add(Pair("uk", ""))
add(Pair("zh", "CN"))
add(Pair("zh", "TW"))
}
val selectedLanguage = presenter.getCurrentLanguageInteractor.getLanguage()
val selectedCountry = presenter.getCurrentLanguageInteractor.getCountry()
// 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)
var localeIndex = -1
locales.forEachIndexed { index, locale ->
// If country is specified, then return the respective locale, else return the
// first locale found if the language is as specified regardless of the country.
if (locale.first == selectedLanguage) {
if (locale.second == selectedCountry) {
var language: String? = null
var country = ""
if (locale is String) {
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
// the first locale found if the language is as specified regardless of the country.
if (language == selectedLocale.language) {
if (country == selectedLocale.country) {
localeIndex = index
return@forEachIndexed
} else if (localeIndex == -1) {
......@@ -199,15 +207,17 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
}
}
}
// This is needed if none of the device locales is implemented in the app.
// By default, set locale will be English
if (localeIndex == -1) localeIndex = 0
AlertDialog.Builder(it)
.setTitle(R.string.title_choose_language)
.setSingleChoiceItems(
resources.getStringArray(R.array.languages), localeIndex
) { dialog, option ->
updateLanguage(locales[option].first, locales[option].second)
if (locales[option] is String) {
updateLanguage(locales[option] as String)
} else if (locales[option] is Pair<*, *>) {
updateLanguage((locales[option] as Pair<*, *>).first as String,
(locales[option] as Pair<*, *>).second as String)
}
dialog.dismiss()
}
.create()
......
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