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

Reformatted code

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