Commit 9bcc0f5c authored by Hussein El Feky's avatar Hussein El Feky

Showed the selected locale in Language setter

parent 521f0a8b
package chat.rocket.android.server.infrastructure package chat.rocket.android.server.infrastructure
import android.content.SharedPreferences import android.content.SharedPreferences
import java.util.*
private const val CURRENT_LANGUAGE = "current_language" private const val CURRENT_LANGUAGE = "current_language"
private const val CURRENT_LANGUAGE_COUNTRY = "current_language_country" private const val CURRENT_LANGUAGE_COUNTRY = "current_language_country"
...@@ -16,10 +17,10 @@ class SharedPrefsCurrentLanguageRepository(private val preferences: SharedPrefer ...@@ -16,10 +17,10 @@ class SharedPrefsCurrentLanguageRepository(private val preferences: SharedPrefer
} }
override fun getLanguage(): String? { override fun getLanguage(): String? {
return preferences.getString(CURRENT_LANGUAGE, "") return preferences.getString(CURRENT_LANGUAGE, Locale.getDefault().language)
} }
override fun getCountry(): String? { override fun getCountry(): String? {
return preferences.getString(CURRENT_LANGUAGE_COUNTRY, "") return preferences.getString(CURRENT_LANGUAGE_COUNTRY, Locale.getDefault().country)
} }
} }
...@@ -40,11 +40,12 @@ class SettingsPresenter @Inject constructor( ...@@ -40,11 +40,12 @@ class SettingsPresenter @Inject constructor(
private val tokenRepository: TokenRepository, private val tokenRepository: TokenRepository,
private val permissions: PermissionsInteractor, private val permissions: PermissionsInteractor,
private val rocketChatClientFactory: RocketChatClientFactory, private val rocketChatClientFactory: RocketChatClientFactory,
private val saveLanguageInteractor: SaveCurrentLanguageInteractor,
val getCurrentLanguageInteractor: GetCurrentLanguageInteractor,
getCurrentServerInteractor: GetCurrentServerInteractor, getCurrentServerInteractor: GetCurrentServerInteractor,
removeAccountInteractor: RemoveAccountInteractor, removeAccountInteractor: RemoveAccountInteractor,
databaseManagerFactory: DatabaseManagerFactory, databaseManagerFactory: DatabaseManagerFactory,
connectionManagerFactory: ConnectionManagerFactory, connectionManagerFactory: ConnectionManagerFactory
private val saveLanguageInteractor: SaveCurrentLanguageInteractor
) : CheckServerPresenter( ) : CheckServerPresenter(
strategy = strategy, strategy = strategy,
factory = rocketChatClientFactory, factory = rocketChatClientFactory,
...@@ -92,7 +93,6 @@ class SettingsPresenter @Inject constructor( ...@@ -92,7 +93,6 @@ class SettingsPresenter @Inject constructor(
fun enableAnalyticsTracking(isEnabled: Boolean) { fun enableAnalyticsTracking(isEnabled: Boolean) {
analyticsTrackingInteractor.save(isEnabled) analyticsTrackingInteractor.save(isEnabled)
} }
fun logout() { fun logout() {
......
...@@ -35,8 +35,10 @@ internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment" ...@@ -35,8 +35,10 @@ internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment"
fun newInstance(): Fragment = SettingsFragment() fun newInstance(): Fragment = SettingsFragment()
class SettingsFragment : Fragment(), SettingsView, AppLanguageView { class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
@Inject lateinit var analyticsManager: AnalyticsManager @Inject
@Inject lateinit var presenter: SettingsPresenter lateinit var analyticsManager: AnalyticsManager
@Inject
lateinit var presenter: SettingsPresenter
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -163,29 +165,45 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView { ...@@ -163,29 +165,45 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
private fun changeLanguage() { private fun changeLanguage() {
context?.let { 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()
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 && (localeIndex == -1 || locale.second == selectedCountry)) {
localeIndex = index
if (locale.second == selectedCountry) return@forEachIndexed
}
}
// 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), -1 resources.getStringArray(R.array.languages), localeIndex
) { dialog, option -> ) { dialog, option ->
when (option) { updateLanguage(locales[option].first, locales[option].second)
0 -> updateLanguage("en")
1 -> updateLanguage("ar")
2 -> updateLanguage("de")
3 -> updateLanguage("es")
4 -> updateLanguage("fa")
5 -> updateLanguage("fr")
6 -> updateLanguage("hi", "IN")
7 -> updateLanguage("it")
8 -> updateLanguage("ja")
9 -> updateLanguage("pt", "BR")
10 -> updateLanguage("pt", "PT")
11 -> updateLanguage("ru", "RU")
12 -> updateLanguage("tr")
13 -> updateLanguage("uk")
14 -> updateLanguage("zh", "CN")
15 -> updateLanguage("zh", "TW")
}
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