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

Unverified Commit 84c7678a authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

Change RealDrawerConfigManager to register to SettingsChangeBroker for changed settings updates

parent acc46fa6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ val preferencesModule = module {
        RealDrawerConfigManager(
            preferences = get(),
            coroutineScope = get(named("AppCoroutineScope")),
            changeBroker = get(),
        )
    } bind DrawerConfigManager::class

+33 −8
Original line number Diff line number Diff line
package com.fsck.k9.preferences

import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfig
import app.k9mail.legacy.preferences.SettingsChangeBroker
import app.k9mail.legacy.preferences.SettingsChangeSubscriber
import com.fsck.k9.K9
import com.fsck.k9.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch

internal class RealDrawerConfigManager(
    private val preferences: Preferences,
    private val coroutineScope: CoroutineScope,
    private val changeBroker: SettingsChangeBroker,
) : DrawerConfigManager {
    private val drawerConfigFlow = MutableSharedFlow<DrawerConfig>(replay = 1)
    private var drawerConfig: DrawerConfig? = null

    init {
        coroutineScope.launch {
            asSettingsFlow().collect { config ->
                drawerConfigFlow.emit(config)
            }
        }
    }

    override fun save(config: DrawerConfig) {
        saveDrawerConfig(config)
@@ -22,15 +34,11 @@ internal class RealDrawerConfigManager(
    }

    private fun loadDrawerConfig(): DrawerConfig {
        val drawerConfig = DrawerConfig(
        return DrawerConfig(
            showAccountSelector = K9.isShowAccountSelector,
            showStarredCount = K9.isShowStarredCount,
            showUnifiedFolders = K9.isShowUnifiedInbox,
        )

        updateDrawerConfigFlow(drawerConfig)

        return drawerConfig
    }

    private fun updateDrawerConfigFlow(config: DrawerConfig) {
@@ -41,14 +49,31 @@ internal class RealDrawerConfigManager(

    @Synchronized
    override fun getConfig(): DrawerConfig {
        return drawerConfig ?: loadDrawerConfig().also { drawerConfig = it }
        return loadDrawerConfig().also {
            updateDrawerConfigFlow(it)
        }
    }

    override fun getConfigFlow(): Flow<DrawerConfig> {
        getConfig()
        return drawerConfigFlow.distinctUntilChanged()
    }

    private fun asSettingsFlow(): Flow<DrawerConfig> {
        return callbackFlow {
            send(loadDrawerConfig())

            val subscriber = SettingsChangeSubscriber {
                drawerConfigFlow.tryEmit(loadDrawerConfig())
            }

            changeBroker.subscribe(subscriber)

            awaitClose {
                changeBroker.unsubscribe(subscriber)
            }
        }
    }

    @Synchronized
    private fun saveDrawerConfig(config: DrawerConfig) {
        val editor = preferences.createStorageEditor()
+3 −1
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ class DefaultSettingsChangeBroker(
    }

    override fun publish() {
        for (subscriber in subscribers) {
        val currentSubscribers = synchronized(lock) { HashSet(subscribers) }

        for (subscriber in currentSubscribers) {
            subscriber.receive()
        }
    }