Skip to content

Commit

Permalink
Change RealDrawerConfigManager to register to SettingsChangeBroker fo…
Browse files Browse the repository at this point in the history
…r changed settings updates
  • Loading branch information
wmontwe committed Feb 11, 2025
1 parent acc46fa commit 1258b94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ val preferencesModule = module {
RealDrawerConfigManager(
preferences = get(),
coroutineScope = get(named("AppCoroutineScope")),
changeBroker = get(),
)
} bind DrawerConfigManager::class

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
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)
updateDrawerConfigFlow(config)
}

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) {
Expand All @@ -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()
Expand Down

0 comments on commit 1258b94

Please sign in to comment.