Skip to content

Commit

Permalink
Fixed Language Selection. yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
boy12hoody committed Sep 17, 2021
1 parent 1e89a44 commit 4cfe0eb
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/uz/boywonder/playstoredemo/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -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() {}
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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,23 +12,30 @@ 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
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)
Expand All @@ -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)
Expand All @@ -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 -> {
Expand All @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class SettingsViewModel @Inject constructor(
private val dataStoreRepository: DataStoreRepository
) : ViewModel() {

private var langType = LANG_TYPE_UZ
private var _langType: MutableLiveData<String> = MutableLiveData(LANG_TYPE_UZ)
val langType: LiveData<String> get() = _langType

private var _themeType: MutableLiveData<String> = MutableLiveData(THEME_TYPE_SYSTEM)
val themeType: LiveData<String> get() = _themeType

Expand All @@ -27,7 +29,7 @@ class SettingsViewModel @Inject constructor(
fun readLangChoice() {
viewModelScope.launch {
readLangChoice.collect { value ->
langType = value
_langType.value = value
}
}
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<string name="cat_cats">Cats</string>
<string name="cat_animals">Animals</string>
<string name="cat_tech">Tech</string>
<string name="lang_selection_error">Not implemented yet, But works for system language.. ¯\\_(ツ)_/¯</string>
<string name="no_connection">No Internet Connection.</string>
<string name="back_online">Back Online!</string>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values-uz/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<string name="cat_cats">Mushuklar</string>
<string name="cat_animals">Hayvonlar</string>
<string name="cat_tech">Texnologiya</string>
<string name="lang_selection_error">To\'girlanmagan hali, tizim tili uchun ishlaydi. ¯\\_(ツ)_/¯</string>
<string name="no_connection">Internetga ulanishda xatolik.</string>
<string name="back_online">Internet qaytdi!</string>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<string name="cat_cats">Cats</string>
<string name="cat_animals">Animals</string>
<string name="cat_tech">Tech</string>
<string name="lang_selection_error">Not implemented yet, But works for system language.. ¯\\_(ツ)_/¯</string>
<string name="no_connection">No Internet Connection.</string>
<string name="back_online">Back Online!</string>
</resources>

0 comments on commit 4cfe0eb

Please sign in to comment.