Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 6c325f11 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5969 from k9mail/locale_changes

Update name/description of notification channels when app locale changes
parents c376e8ef 37225271
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import android.content.res.Resources
import com.fsck.k9.activity.MessageCompose
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.external.MessageProvider
import com.fsck.k9.notification.NotificationChannelManager
import com.fsck.k9.ui.base.AppLanguageManager
import com.fsck.k9.ui.base.ThemeManager
import com.fsck.k9.ui.base.extensions.currentLocale
@@ -13,6 +14,7 @@ import java.util.Locale
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@@ -25,6 +27,7 @@ class App : Application() {
    private val messagingListenerProvider: MessagingListenerProvider by inject()
    private val themeManager: ThemeManager by inject()
    private val appLanguageManager: AppLanguageManager by inject()
    private val notificationChannelManager: NotificationChannelManager by inject()
    private val appCoroutineScope: CoroutineScope = GlobalScope + Dispatchers.Main
    private var appLanguageManagerInitialized = false

@@ -39,6 +42,7 @@ class App : Application() {
        Core.init(this)
        MessageProvider.init()
        initializeAppLanguage()
        updateNotificationChannelsOnAppLanguageChanges()
        themeManager.init()

        messagingListenerProvider.listeners.forEach { listener ->
@@ -109,6 +113,13 @@ class App : Application() {
        return resources
    }

    private fun updateNotificationChannelsOnAppLanguageChanges() {
        appLanguageManager.appLocale
            .distinctUntilChanged()
            .onEach { notificationChannelManager.updateChannels() }
            .launchIn(appCoroutineScope)
    }

    companion object {
        val appConfig = AppConfig(
            componentsToDisable = listOf(MessageCompose::class.java)
+17 −1
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.fsck.k9.ui.base" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fsck.k9.ui.base">

    <application>

        <receiver
            android:name=".locale.LocaleBroadcastReceiver"
            android:exported="false"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.LOCALE_CHANGED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>
+18 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ package com.fsck.k9.ui.base
import android.content.res.Resources
import com.fsck.k9.K9
import com.fsck.k9.ui.base.extensions.currentLocale
import com.fsck.k9.ui.base.locale.SystemLocaleChangeListener
import com.fsck.k9.ui.base.locale.SystemLocaleManager
import java.util.Locale
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -19,11 +21,20 @@ import kotlinx.coroutines.plus
 * - Notifies listeners when the app language has changed.
 */
class AppLanguageManager(
    private val systemLocaleManager: SystemLocaleManager,
    private val coroutineScope: CoroutineScope = GlobalScope + Dispatchers.Main
) {
    private var currentOverrideLocale: Locale? = null
    private val _overrideLocale = MutableSharedFlow<Locale?>(replay = 1)
    private val _appLocale = MutableSharedFlow<Locale>(replay = 1)
    val overrideLocale: Flow<Locale?> = _overrideLocale
    val appLocale: Flow<Locale> = _appLocale

    private val systemLocaleListener = SystemLocaleChangeListener {
        coroutineScope.launch {
            _appLocale.emit(systemLocale)
        }
    }

    fun init() {
        setLocale(K9.k9Language)
@@ -58,8 +69,15 @@ class AppLanguageManager(
        val locale = overrideLocale ?: systemLocale
        Locale.setDefault(locale)

        if (overrideLocale == null) {
            systemLocaleManager.addListener(systemLocaleListener)
        } else {
            systemLocaleManager.removeListener(systemLocaleListener)
        }

        coroutineScope.launch {
            _overrideLocale.emit(overrideLocale)
            _appLocale.emit(locale)
        }
    }

+11 −2
Original line number Diff line number Diff line
package com.fsck.k9.ui.base

import com.fsck.k9.ui.base.locale.SystemLocaleManager
import org.koin.core.qualifier.named
import org.koin.dsl.module

val uiBaseModule = module {
    single { ThemeManager(context = get(), themeProvider = get(), generalSettingsManager = get(), appCoroutineScope = get(named("AppCoroutineScope"))) }
    single { AppLanguageManager() }
    single {
        ThemeManager(
            context = get(),
            themeProvider = get(),
            generalSettingsManager = get(),
            appCoroutineScope = get(named("AppCoroutineScope"))
        )
    }
    single { AppLanguageManager(systemLocaleManager = get()) }
    single { SystemLocaleManager(context = get()) }
}
+17 −0
Original line number Diff line number Diff line
package com.fsck.k9.ui.base.locale

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class LocaleBroadcastReceiver : BroadcastReceiver(), KoinComponent {
    private val systemLocaleManager: SystemLocaleManager by inject()

    override fun onReceive(context: Context, intent: Intent?) {
        if (intent?.action == Intent.ACTION_LOCALE_CHANGED) {
            systemLocaleManager.notifyListeners()
        }
    }
}
Loading