Commit 2ec5c91c authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Add change app language feature.

parent 0afc2bef
package chat.rocket.android.core.behaviours
interface AppLanguageView {
/**
* Updates the app language
*
* @param language The app language to be updated.
* @param country Opcional. The country code to be updated.
*/
fun updateLanguage(language: String, country: String? = null)
}
\ No newline at end of file
package chat.rocket.android.main.di
import androidx.lifecycle.LifecycleOwner
import chat.rocket.android.core.behaviours.AppLanguageView
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.dagger.scope.PerActivity
import chat.rocket.android.main.presentation.MainNavigator
......@@ -16,6 +17,12 @@ class MainModule {
@PerActivity
fun provideMainNavigator(activity: MainActivity) = MainNavigator(activity)
@Provides
@PerActivity
fun appLanguageView(activity: MainActivity): AppLanguageView {
return activity
}
@Provides
@PerActivity
fun provideJob() = Job()
......
package chat.rocket.android.main.presentation
import android.content.Context
import android.content.res.Configuration
import chat.rocket.android.core.behaviours.AppLanguageView
import chat.rocket.android.push.GroupedPush
import chat.rocket.android.server.domain.GetCurrentLanguageInteractor
import chat.rocket.android.server.domain.RefreshPermissionsInteractor
import chat.rocket.android.server.domain.RefreshSettingsInteractor
import chat.rocket.android.server.domain.SaveCurrentLanguageInteractor
import chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import java.util.Locale
import javax.inject.Inject
import javax.inject.Named
class MainPresenter @Inject constructor(
@Named("currentServer") private val currentServerUrl: String,
private val mainNavigator: MainNavigator,
private val appLanguageView: AppLanguageView,
private val refreshSettingsInteractor: RefreshSettingsInteractor,
private val refreshPermissionsInteractor: RefreshPermissionsInteractor,
private val connectionManagerFactory: ConnectionManagerFactory,
private val saveLanguageInteractor: SaveCurrentLanguageInteractor,
private var getLanguageInteractor: GetCurrentLanguageInteractor,
private val groupedPush: GroupedPush
) {
......@@ -39,28 +37,10 @@ class MainPresenter @Inject constructor(
fun showChatList(chatRoomId: String? = null) = mainNavigator.toChatList(chatRoomId)
fun setLocale(language: String, baseContext: Context) {
val locale = Locale(language)
Locale.setDefault(locale)
val config = Configuration()
config.locale = locale
baseContext.resources.updateConfiguration(config, baseContext.resources.displayMetrics)
saveLanguageInteractor.save(language)
}
fun setLocaleWithRegion(lang: String, country: String, baseContext: Context) {
val locale = Locale(lang, country)
Locale.setDefault(locale)
val config = Configuration()
config.locale = locale
baseContext.resources.updateConfiguration(config, baseContext.resources.displayMetrics)
saveLanguageInteractor.save(lang)
}
fun loadLocale(baseContext: Context) {
val currentLanguage = getLanguageInteractor.get()
if (currentLanguage != null) {
setLocale(currentLanguage, baseContext)
fun getAppLanguage() {
getLanguageInteractor.get()?.let { currentLanguage ->
appLanguageView.updateLanguage(currentLanguage)
}
}
}
\ No newline at end of file
......@@ -3,10 +3,14 @@ package chat.rocket.android.main.ui
import android.app.Activity
import android.app.NotificationManager
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import chat.rocket.android.R
import chat.rocket.android.core.behaviours.AppLanguageView
import chat.rocket.android.main.presentation.MainPresenter
import chat.rocket.android.push.refreshPushToken
import chat.rocket.android.server.ui.INTENT_CHAT_ROOM_ID
......@@ -15,10 +19,11 @@ import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import dagger.android.support.HasSupportFragmentInjector
import java.util.*
import javax.inject.Inject
class MainActivity : AppCompatActivity(), HasActivityInjector,
HasSupportFragmentInjector {
HasSupportFragmentInjector, AppLanguageView {
@Inject
lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject
......@@ -30,11 +35,11 @@ class MainActivity : AppCompatActivity(), HasActivityInjector,
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
presenter.loadLocale(baseContext)
refreshPushToken()
with(presenter) {
connect()
getAppLanguage()
intent.getStringExtra(INTENT_CHAT_ROOM_ID).let {
clearNotificationsForChatRoom(it)
showChatList(it)
......@@ -53,6 +58,26 @@ class MainActivity : AppCompatActivity(), HasActivityInjector,
override fun supportFragmentInjector(): AndroidInjector<Fragment> =
fagmentDispatchingAndroidInjector
override fun updateLanguage(language: String, country: String?) {
val locale: Locale = if (country != null) {
Locale(language, country)
} else {
Locale(language)
}
Locale.setDefault(locale)
val config = Configuration()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.locales = LocaleList(locale)
} else {
config.locale = locale
}
createConfigurationContext(config)
}
private fun clearAppNotifications() =
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).cancelAll()
}
......@@ -6,6 +6,6 @@ import javax.inject.Inject
class GetCurrentLanguageInteractor @Inject constructor(
private val repository: CurrentLanguageRepository
) {
fun get(): String? = repository.get()
fun get(): String? = repository.get()
}
\ No newline at end of file
......@@ -5,9 +5,11 @@ 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
import chat.rocket.android.server.domain.SaveCurrentLanguageInteractor
import chat.rocket.android.server.domain.TokenRepository
import chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
......@@ -28,7 +30,6 @@ import timber.log.Timber
import javax.inject.Inject
import javax.inject.Named
class SettingsPresenter @Inject constructor(
private val view: SettingsView,
private val strategy: CancelStrategy,
......@@ -42,7 +43,8 @@ class SettingsPresenter @Inject constructor(
getCurrentServerInteractor: GetCurrentServerInteractor,
removeAccountInteractor: RemoveAccountInteractor,
databaseManagerFactory: DatabaseManagerFactory,
connectionManagerFactory: ConnectionManagerFactory
connectionManagerFactory: ConnectionManagerFactory,
private val saveLanguageInteractor: SaveCurrentLanguageInteractor
) : CheckServerPresenter(
strategy = strategy,
factory = rocketChatClientFactory,
......@@ -124,6 +126,10 @@ class SettingsPresenter @Inject constructor(
}
}
fun saveLocale(language: String) {
saveLanguageInteractor.save(language)
}
fun toProfile() = navigator.toProfile()
fun toAdmin() = tokenRepository.get(currentServer)?.let {
......
......@@ -2,8 +2,11 @@ package chat.rocket.android.settings.ui
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -17,8 +20,8 @@ import chat.rocket.android.BuildConfig
import chat.rocket.android.R
import chat.rocket.android.analytics.AnalyticsManager
import chat.rocket.android.analytics.event.ScreenViewEvent
import chat.rocket.android.core.behaviours.AppLanguageView
import chat.rocket.android.helper.TextHelper.getDeviceAndAppInformation
import chat.rocket.android.main.presentation.MainPresenter
import chat.rocket.android.settings.presentation.SettingsPresenter
import chat.rocket.android.settings.presentation.SettingsView
import chat.rocket.android.util.extensions.inflate
......@@ -28,17 +31,16 @@ import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.app_bar.*
import kotlinx.android.synthetic.main.fragment_settings.*
import timber.log.Timber
import java.util.*
import javax.inject.Inject
internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment"
fun newInstance(): Fragment = SettingsFragment()
class SettingsFragment : Fragment(), SettingsView {
class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
@Inject lateinit var analyticsManager: AnalyticsManager
@Inject lateinit var presenter: SettingsPresenter
@Inject lateinit var mainPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -112,6 +114,27 @@ class SettingsFragment : Fragment(), SettingsView {
}
}
override fun updateLanguage(language: String, country: String?) {
val locale: Locale = if (country != null) {
Locale(language, country)
} else {
Locale(language)
}
Locale.setDefault(locale)
val config = Configuration()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.locales = LocaleList(locale)
} else {
config.locale = locale
}
context?.createConfigurationContext(config)
presenter.saveLocale(language)
}
override fun invalidateToken(token: String) = invalidateFirebaseToken(token)
override fun showLoading() {
......@@ -143,89 +166,90 @@ class SettingsFragment : Fragment(), SettingsView {
}
}
fun changeLanguage() {
private fun contactSupport() {
val uriText = "mailto:${"support@rocket.chat"}" +
"?subject=" + Uri.encode(getString(R.string.msg_android_app_support)) +
"&body=" + Uri.encode(getDeviceAndAppInformation())
with(Intent(Intent.ACTION_SENDTO)) {
data = uriText.toUri()
try {
startActivity(Intent.createChooser(this, getString(R.string.msg_send_email)))
} catch (ex: ActivityNotFoundException) {
Timber.e(ex)
}
}
}
private fun changeLanguage() {
val languages = resources.getStringArray(R.array.languages)
context?.let {
AlertDialog.Builder(it)
.setTitle(R.string.title_choose_language)
.setSingleChoiceItems(languages, -1) { dialog, which ->
when (which) {
.setSingleChoiceItems(languages, -1) { dialog, option ->
when (option) {
0 -> {
mainPresenter.setLocale("en", it)
updateLanguage("en")
activity?.recreate()
}
1 -> {
mainPresenter.setLocale("hi", it)
updateLanguage("hi")
activity?.recreate()
}
2 -> {
mainPresenter.setLocale("ja", it)
updateLanguage("ja")
activity?.recreate()
}
3 -> {
mainPresenter.setLocale("ru", it)
updateLanguage("ru")
activity?.recreate()
}
4 -> {
mainPresenter.setLocale("it", it)
updateLanguage("it")
activity?.recreate()
}
5->{
mainPresenter.setLocaleWithRegion("pt", "BR", it)
5 -> {
updateLanguage("pt", "BR")
activity?.recreate()
}
6->{
mainPresenter.setLocaleWithRegion("pt", "PT", it)
6 -> {
updateLanguage("pt", "PT")
activity?.recreate()
}
7->{
mainPresenter.setLocale("zh", it)
7 -> {
updateLanguage("zh")
activity?.recreate()
}
8->{
mainPresenter.setLocale("de", it)
8 -> {
updateLanguage("de")
activity?.recreate()
}
9->{
mainPresenter.setLocale("es", it)
9 -> {
updateLanguage("es")
activity?.recreate()
}
10->{
mainPresenter.setLocale("fa", it)
10 -> {
updateLanguage("fa")
activity?.recreate()
}
11->{
mainPresenter.setLocale("fr", it)
11 -> {
updateLanguage("fr")
activity?.recreate()
}
12->{
mainPresenter.setLocale("tr", it)
12 -> {
updateLanguage("tr")
activity?.recreate()
}
13->{
mainPresenter.setLocale("uk", it)
13 -> {
updateLanguage("uk")
activity?.recreate()
}
}
dialog.dismiss()
}
.create().show()
}
}
private fun contactSupport() {
val uriText = "mailto:${"support@rocket.chat"}" +
"?subject=" + Uri.encode(getString(R.string.msg_android_app_support)) +
"&body=" + Uri.encode(getDeviceAndAppInformation())
with(Intent(Intent.ACTION_SENDTO)) {
data = uriText.toUri()
try {
startActivity(Intent.createChooser(this, getString(R.string.msg_send_email)))
} catch (ex: ActivityNotFoundException) {
Timber.e(ex)
}
.create()
.show()
}
}
......
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