Skip to content

Commit

Permalink
3
Browse files Browse the repository at this point in the history
  • Loading branch information
reocat committed Jan 22, 2025
1 parent 4e30e7f commit 3b4f9db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 44 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
android:exported="true"
android:launchMode="singleTask"
android:theme="@style/Theme.OuterTune"
android:configChanges="orientation|screenSize|keyboardHidden|locale"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -60,7 +61,7 @@
</intent-filter>

<!-- Youtube filter -->
<intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
Expand Down
18 changes: 7 additions & 11 deletions app/src/main/java/com/dd3boh/outertune/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,8 @@ class MainActivity : ComponentActivity() {
private lateinit var localeManager: LocaleManager

override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(newBase)
localeManager = LocaleManager(this)
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

// Handle system language changes
lifecycleScope.launch {
localeManager.handleSystemLanguageChange()
}
localeManager = LocaleManager(newBase)
super.attachBaseContext(localeManager.applyStoredLocale(newBase))
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
Expand Down Expand Up @@ -1302,6 +1293,11 @@ class MainActivity : ComponentActivity() {
}
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// Reapply the stored locale when the configuration changes
localeManager.applyStoredLocale(this)
}

private fun setSystemBarAppearance(isDark: Boolean) {
WindowCompat.getInsetsController(window, window.decorView.rootView).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,7 @@ fun ContentSettings(
?: stringResource(R.string.system_default)
},
onValueSelected = { newLanguage ->
if (newLanguage == "system") {
// Use system default language
val systemLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList.getDefault()[0]
} else {
@Suppress("DEPRECATION")
Locale.getDefault()
}

if (localeManager.updateLocale(systemLocale.language)) {
setSelectedLanguage("system")
restartApp(context)
}
} else if (localeManager.updateLocale(newLanguage)) {
if (localeManager.updateLocale(newLanguage)) {
setSelectedLanguage(newLanguage)
restartApp(context)
} else {
Expand Down Expand Up @@ -395,31 +382,27 @@ class LocaleManager(private val context: Context) {
)
}

// Add this function to check the stored language preference
fun applyStoredLocale(context: Context): Context {
val storedLanguage = runBlocking { getStoredLanguage() }
return if (storedLanguage != null) {
updateLocale(storedLanguage)
context.createConfigurationContext(context.resources.configuration)
} else {
context
}
}

private suspend fun getStoredLanguage(): String? {
return try {
context.dataStore.data
.map { preferences ->
preferences[AppLanguageKey]
}
.map { preferences -> preferences[AppLanguageKey] }
.first()
} catch (e: Exception) {
Timber.e(e, "Failed to read stored language preference")
null
}
}

// Modified to handle system language changes
suspend fun handleSystemLanguageChange() {
val storedLanguage = getStoredLanguage()

// Only update if the stored language is not "system"
if (storedLanguage != null && storedLanguage != "system") {
// Re-apply the stored language preference
updateLocale(storedLanguage)
}
}

fun updateLocale(languageCode: String): Boolean {
try {
val locale = when (languageCode) {
Expand All @@ -439,16 +422,14 @@ class LocaleManager(private val context: Context) {
@Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics)

val newContext = context.createConfigurationContext(config)
updateAppContext(newContext)

return true
} catch (e: Exception) {
Timber.tag("LocaleManager").e(e, "Failed to update locale")
return false
}
}


private fun createLocaleFromCode(languageCode: String): Locale {
return when {
languageCode == "zh-CN" -> Locale.SIMPLIFIED_CHINESE
Expand Down

0 comments on commit 3b4f9db

Please sign in to comment.