From 4cfe0eb0d2f6372a2d783a2fbd3f012ed3e19d0c Mon Sep 17 00:00:00 2001 From: Boy Wonder Date: Fri, 17 Sep 2021 12:04:35 +0500 Subject: [PATCH] Fixed Language Selection. yay! --- .idea/misc.xml | 2 +- app/build.gradle | 3 ++ .../boywonder/playstoredemo/MyApplication.kt | 28 ++++++++++- .../playstoredemo/data/DataStoreRepository.kt | 24 --------- .../ui/dialogs/LanguageBottomSheet.kt | 4 -- .../playstoredemo/ui/main/MainActivity.kt | 50 +++++++++++++++++-- .../ui/settings/SettingsViewModel.kt | 6 ++- app/src/main/res/values-en/strings.xml | 1 - app/src/main/res/values-uz/strings.xml | 1 - app/src/main/res/values/strings.xml | 1 - 10 files changed, 81 insertions(+), 39 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 230ddbd..c505ef5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index f2fdf9e..3cb45e0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,6 +93,9 @@ dependencies { // DataStore implementation 'androidx.datastore:datastore-preferences:1.0.0' + // Android library for in-app language changes + implementation 'com.akexorcist:localization:1.2.10' + } kapt { diff --git a/app/src/main/java/uz/boywonder/playstoredemo/MyApplication.kt b/app/src/main/java/uz/boywonder/playstoredemo/MyApplication.kt index 3ac8323..e886048 100644 --- a/app/src/main/java/uz/boywonder/playstoredemo/MyApplication.kt +++ b/app/src/main/java/uz/boywonder/playstoredemo/MyApplication.kt @@ -1,7 +1,33 @@ package uz.boywonder.playstoredemo import android.app.Application +import android.content.Context +import android.content.res.Configuration +import android.content.res.Resources +import com.akexorcist.localizationactivity.core.LocalizationApplicationDelegate import dagger.hilt.android.HiltAndroidApp +import java.util.* @HiltAndroidApp -class MyApplication : Application() {} \ No newline at end of file +class MyApplication : Application() { + + private val localizationDelegate = LocalizationApplicationDelegate() + + override fun attachBaseContext(base: Context) { + localizationDelegate.setDefaultLanguage(base, Locale.ENGLISH) + super.attachBaseContext(localizationDelegate.attachBaseContext(base)) + } + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + localizationDelegate.onConfigurationChanged(this) + } + + override fun getApplicationContext(): Context { + return localizationDelegate.getApplicationContext(super.getApplicationContext()) + } + + override fun getResources(): Resources { + return localizationDelegate.getResources(baseContext, super.getResources()) + } +} \ No newline at end of file diff --git a/app/src/main/java/uz/boywonder/playstoredemo/data/DataStoreRepository.kt b/app/src/main/java/uz/boywonder/playstoredemo/data/DataStoreRepository.kt index 122cb2c..917813c 100644 --- a/app/src/main/java/uz/boywonder/playstoredemo/data/DataStoreRepository.kt +++ b/app/src/main/java/uz/boywonder/playstoredemo/data/DataStoreRepository.kt @@ -50,30 +50,6 @@ class DataStoreRepository @Inject constructor(@ApplicationContext private val co preferences[PreferencesKeys.selectedLangType] ?: LANG_TYPE_UZ } - -// @TargetApi(Build.VERSION_CODES.N) -// private fun updateResources(context: Context, language: String): Context { -// val locale = Locale(language) -// Locale.setDefault(locale) -// val configuration = context.resources.configuration -// configuration.setLocale(locale) -// configuration.setLayoutDirection(locale) -// return context.createConfigurationContext(configuration) -// } -// -// private fun updateResourcesLegacy(context: Context, language: String): Context { -// val locale = Locale(language) -// Locale.setDefault(locale) -// val resources = context.resources -// val configuration = resources.configuration -// configuration.locale = locale -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { -// configuration.setLayoutDirection(locale) -// } -// resources.updateConfiguration(configuration, resources.displayMetrics) -// return context -// } - /* THEME SELECTION */ suspend fun saveThemeChoice(themeType: String) { diff --git a/app/src/main/java/uz/boywonder/playstoredemo/ui/dialogs/LanguageBottomSheet.kt b/app/src/main/java/uz/boywonder/playstoredemo/ui/dialogs/LanguageBottomSheet.kt index 527f425..47e04ec 100644 --- a/app/src/main/java/uz/boywonder/playstoredemo/ui/dialogs/LanguageBottomSheet.kt +++ b/app/src/main/java/uz/boywonder/playstoredemo/ui/dialogs/LanguageBottomSheet.kt @@ -4,11 +4,9 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.findNavController import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import uz.boywonder.playstoredemo.R import uz.boywonder.playstoredemo.databinding.FragmentBottomSheetLanguageBinding import uz.boywonder.playstoredemo.ui.settings.SettingsViewModel import uz.boywonder.playstoredemo.util.Constants.Companion.LANG_TYPE_EN @@ -42,12 +40,10 @@ class LanguageBottomSheet : BottomSheetDialogFragment() { binding.apply { uzLanguageTextView.setOnClickListener { settingsViewModel.saveLangChoice(LANG_TYPE_UZ) - Toast.makeText(context, getString(R.string.lang_selection_error), Toast.LENGTH_SHORT).show() findNavController().navigate(action) } engLanguageTextView.setOnClickListener { settingsViewModel.saveLangChoice(LANG_TYPE_EN) - Toast.makeText(context, getString(R.string.lang_selection_error), Toast.LENGTH_SHORT).show() findNavController().navigate(action) } } diff --git a/app/src/main/java/uz/boywonder/playstoredemo/ui/main/MainActivity.kt b/app/src/main/java/uz/boywonder/playstoredemo/ui/main/MainActivity.kt index ef1656d..11629fc 100644 --- a/app/src/main/java/uz/boywonder/playstoredemo/ui/main/MainActivity.kt +++ b/app/src/main/java/uz/boywonder/playstoredemo/ui/main/MainActivity.kt @@ -1,6 +1,8 @@ package uz.boywonder.playstoredemo.ui.main +import android.content.Context import android.content.res.Configuration +import android.content.res.Resources import android.os.Bundle import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity @@ -10,6 +12,8 @@ import androidx.navigation.fragment.NavHostFragment import androidx.navigation.fragment.findNavController import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupWithNavController +import com.akexorcist.localizationactivity.core.LocalizationActivityDelegate +import com.akexorcist.localizationactivity.core.OnLocaleChangedListener import dagger.hilt.android.AndroidEntryPoint import uz.boywonder.playstoredemo.R import uz.boywonder.playstoredemo.databinding.ActivityMainBinding @@ -17,16 +21,21 @@ import uz.boywonder.playstoredemo.ui.settings.SettingsViewModel import uz.boywonder.playstoredemo.util.Constants.Companion.THEME_TYPE_DARK import uz.boywonder.playstoredemo.util.Constants.Companion.THEME_TYPE_LIGHT import uz.boywonder.playstoredemo.util.Constants.Companion.THEME_TYPE_SYSTEM +import java.util.* @AndroidEntryPoint -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), OnLocaleChangedListener { + + private val localizationDelegate = LocalizationActivityDelegate(this) private lateinit var binding: ActivityMainBinding private lateinit var navController: NavController private val settingsViewModel: SettingsViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { + localizationDelegate.addOnLocaleChangedListener(this) + localizationDelegate.onCreate() super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) @@ -40,6 +49,11 @@ class MainActivity : AppCompatActivity() { binding.bottomNavigationView.setupWithNavController(navController) setupActionBarWithNavController(navController) + settingsViewModel.readLangChoice() + settingsViewModel.langType.observe(this) { lang -> + setLanguage(lang) + } + settingsViewModel.readThemeChoice() settingsViewModel.themeType.observe(this) { theme -> applyDayNight(theme) @@ -59,6 +73,8 @@ class MainActivity : AppCompatActivity() { return navController.navigateUp() || super.onSupportNavigateUp() } + /* MANUAL THEME CHANGE */ + private fun applyDayNight(state: String) { when (state) { THEME_TYPE_LIGHT -> { @@ -76,8 +92,34 @@ class MainActivity : AppCompatActivity() { } } - override fun recreate() { - super.recreate() - overridePendingTransition(R.anim.nav_default_enter_anim, R.anim.nav_default_exit_anim) + /* MANUAL LANGUAGE CHANGE */ + + public override fun onResume() { + super.onResume() + localizationDelegate.onResume(this) + } + + override fun attachBaseContext(newBase: Context) { + applyOverrideConfiguration(localizationDelegate.updateConfigurationLocale(newBase)) + super.attachBaseContext(newBase) + } + + override fun getApplicationContext(): Context { + return localizationDelegate.getApplicationContext(super.getApplicationContext()) + } + + override fun getResources(): Resources { + return localizationDelegate.getResources(super.getResources()) + } + + private fun setLanguage(language: String?) { + localizationDelegate.setLanguage(this, language!!) } + + val currentLanguage: Locale + get() = localizationDelegate.getLanguage(this) + + override fun onAfterLocaleChanged() {} + + override fun onBeforeLocaleChanged() {} } \ No newline at end of file diff --git a/app/src/main/java/uz/boywonder/playstoredemo/ui/settings/SettingsViewModel.kt b/app/src/main/java/uz/boywonder/playstoredemo/ui/settings/SettingsViewModel.kt index 749e102..c312ede 100644 --- a/app/src/main/java/uz/boywonder/playstoredemo/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/uz/boywonder/playstoredemo/ui/settings/SettingsViewModel.kt @@ -18,7 +18,9 @@ class SettingsViewModel @Inject constructor( private val dataStoreRepository: DataStoreRepository ) : ViewModel() { - private var langType = LANG_TYPE_UZ + private var _langType: MutableLiveData = MutableLiveData(LANG_TYPE_UZ) + val langType: LiveData get() = _langType + private var _themeType: MutableLiveData = MutableLiveData(THEME_TYPE_SYSTEM) val themeType: LiveData get() = _themeType @@ -27,7 +29,7 @@ class SettingsViewModel @Inject constructor( fun readLangChoice() { viewModelScope.launch { readLangChoice.collect { value -> - langType = value + _langType.value = value } } } diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index 0c1cba7..7e77edf 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -20,7 +20,6 @@ Cats Animals Tech - Not implemented yet, But works for system language.. ¯\\_(ツ)_/¯ No Internet Connection. Back Online! \ No newline at end of file diff --git a/app/src/main/res/values-uz/strings.xml b/app/src/main/res/values-uz/strings.xml index 7772d71..ef907d9 100644 --- a/app/src/main/res/values-uz/strings.xml +++ b/app/src/main/res/values-uz/strings.xml @@ -20,7 +20,6 @@ Mushuklar Hayvonlar Texnologiya - To\'girlanmagan hali, tizim tili uchun ishlaydi. ¯\\_(ツ)_/¯ Internetga ulanishda xatolik. Internet qaytdi! \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c9f4b7f..c0f150e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,7 +24,6 @@ Cats Animals Tech - Not implemented yet, But works for system language.. ¯\\_(ツ)_/¯ No Internet Connection. Back Online! \ No newline at end of file