Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mechanism to listen for general settings changes #8763

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/detekt/detekt-baseline-feature-account-setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
<CurrentIssues>
<ID>ViewModelForwarding:AccountAutoDiscoveryContent.kt$AccountOAuthView( onOAuthResult = { result -&gt; onEvent(Event.OnOAuthResult(result)) }, viewModel = oAuthViewModel, isEnabled = isAutoDiscoverySettingsTrusted || isConfigurationApproved, )</ID>
<ID>ViewModelForwarding:AccountAutoDiscoveryContent.kt$AutoDiscoveryContent( state = state, onEvent = onEvent, oAuthViewModel = oAuthViewModel, )</ID>
<ID>ViewModelForwarding:AccountAutoDiscoveryContent.kt$ContentView( state = state, onEvent = onEvent, oAuthViewModel = oAuthViewModel, resources = resources, )</ID>
</CurrentIssues>
</SmellBaseline>
4 changes: 0 additions & 4 deletions config/detekt/detekt-baseline-legacy-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ID>LongMethod:AccountPreferenceSerializer.kt$AccountPreferenceSerializer$@Synchronized fun loadAccount(account: Account, storage: Storage)</ID>
<ID>LongMethod:AccountPreferenceSerializer.kt$AccountPreferenceSerializer$@Synchronized fun save(editor: StorageEditor, storage: Storage, account: Account)</ID>
<ID>LongMethod:AccountPreferenceSerializer.kt$AccountPreferenceSerializer$fun loadDefaults(account: Account)</ID>
<ID>LongMethod:K9.kt$K9$@JvmStatic fun loadPrefs(storage: Storage)</ID>
<ID>LongMethod:MessageListRepositoryTest.kt$MessageListRepositoryTest$@Test fun `getThread() should use flag values from the cache`()</ID>
<ID>LongMethod:NotificationDataStore.kt$NotificationDataStore$@Synchronized fun removeNotifications( account: Account, selector: (List&lt;MessageReference&gt;) -&gt; List&lt;MessageReference&gt;, ): RemoveNotificationsResult?</ID>
<ID>LongMethod:SettingsExporter.kt$SettingsExporter$private fun writeAccount( serializer: XmlSerializer, account: Account, prefs: Map&lt;String, Any&gt;, includePasswords: Boolean, )</ID>
Expand Down Expand Up @@ -47,7 +46,6 @@
<ID>MagicNumber:NotificationLightDecoder.kt$NotificationLightDecoder$0xFF00FF</ID>
<ID>MagicNumber:NotificationLightDecoder.kt$NotificationLightDecoder$0xFFFF00</ID>
<ID>MagicNumber:NotificationLightDecoder.kt$NotificationLightDecoder$0xFFFFFF</ID>
<ID>MagicNumber:PushService.kt$PushService$29</ID>
<ID>MagicNumber:ServerSettingsSerializer.kt$ServerSettingsAdapter$3</ID>
<ID>MagicNumber:ServerSettingsSerializer.kt$ServerSettingsAdapter$4</ID>
<ID>MagicNumber:ServerSettingsSerializer.kt$ServerSettingsAdapter$5</ID>
Expand Down Expand Up @@ -91,7 +89,6 @@
<ID>TooGenericExceptionCaught:MessageRepository.kt$MessageRepository$e: Exception</ID>
<ID>TooGenericExceptionCaught:PushServiceManager.kt$PushServiceManager$e: Exception</ID>
<ID>TooGenericExceptionCaught:QuoteDateFormatter.kt$QuoteDateFormatter$e: Exception</ID>
<ID>TooGenericExceptionCaught:RealGeneralSettingsManager.kt$e: Exception</ID>
<ID>TooGenericExceptionCaught:SettingsExporter.kt$SettingsExporter$e: Exception</ID>
<ID>TooManyFunctions:CoreResourceProvider.kt$CoreResourceProvider</ID>
<ID>TooManyFunctions:HttpUriParser.kt$HttpUriParser : UriParser</ID>
Expand All @@ -105,7 +102,6 @@
<ID>TooManyFunctions:NotifierMessageStore.kt$NotifierMessageStore : MessageStore</ID>
<ID>TooManyFunctions:Preferences.kt$Preferences : AccountManager</ID>
<ID>TooManyFunctions:PushController.kt$PushController</ID>
<ID>TooManyFunctions:RealGeneralSettingsManager.kt$RealGeneralSettingsManager : GeneralSettingsManager</ID>
<ID>TooManyFunctions:SettingsExporter.kt$SettingsExporter</ID>
<ID>TooManyFunctions:SingleMessageNotificationCreator.kt$SingleMessageNotificationCreator</ID>
<ID>TooManyFunctions:SummaryNotificationCreator.kt$SummaryNotificationCreator</ID>
Expand Down
1 change: 0 additions & 1 deletion config/detekt/detekt-baseline-legacy-ui-legacy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<ID>MagicNumber:AutocryptSetupTransferLiveEvent.kt$AutocryptSetupTransferLiveEvent$2000</ID>
<ID>MagicNumber:ContactLetterBitmapCreator.kt$ContactLetterBitmapCreator$0.65f</ID>
<ID>MagicNumber:ContactLetterBitmapCreator.kt$ContactLetterBitmapCreator$255</ID>
<ID>MagicNumber:GeneralSettingsFragment.kt$GeneralSettingsFragment$28</ID>
<ID>MagicNumber:MessageContainerView.kt$MessageContainerView$29</ID>
<ID>MagicNumber:MessageListItemAnimator.kt$MessageListItemAnimator$120</ID>
<ID>MagicNumber:MessageListItemMapper.kt$MessageListItemMapper$52</ID>
Expand Down
1 change: 1 addition & 0 deletions feature/navigation/drawer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
implementation(projects.legacy.account)
implementation(projects.legacy.mailstore)
implementation(projects.legacy.message)
implementation(projects.legacy.preferences)
implementation(projects.legacy.search)
implementation(projects.legacy.ui.folder)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ val navigationDrawerModule: Module = module {
single<UseCase.GetDrawerConfig> {
GetDrawerConfig(
configProver = get(),
generalSettingsManager = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@ package app.k9mail.feature.navigation.drawer.domain.usecase
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfig
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfigLoader
import app.k9mail.feature.navigation.drawer.domain.DomainContract.UseCase
import app.k9mail.legacy.preferences.GeneralSettingsChangeListener
import app.k9mail.legacy.preferences.GeneralSettingsManager
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged

internal class GetDrawerConfig(
private val configProver: DrawerConfigLoader,
private val generalSettingsManager: GeneralSettingsManager,
) : UseCase.GetDrawerConfig {
override operator fun invoke(): Flow<DrawerConfig> {
// TODO This needs to be updated when the config changes
return flow {
emit(configProver.loadDrawerConfig())
}
return callbackFlow {
send(configProver.loadDrawerConfig())

val listener = GeneralSettingsChangeListener {
trySendBlocking(configProver.loadDrawerConfig())
}

generalSettingsManager.addListener(listener)

awaitClose {
generalSettingsManager.removeListener(listener)
}
}.distinctUntilChanged()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.k9mail.feature.navigation.drawer.domain.usecase

import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfig
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfigLoader

internal class FakeDrawerConfigLoader(
var drawerConfig: DrawerConfig = DrawerConfig(showUnifiedFolders = false, showStarredCount = false),
) : DrawerConfigLoader {
override fun loadDrawerConfig(): DrawerConfig {
return drawerConfig
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package app.k9mail.feature.navigation.drawer.domain.usecase

import app.k9mail.legacy.preferences.AppTheme
import app.k9mail.legacy.preferences.GeneralSettings
import app.k9mail.legacy.preferences.GeneralSettingsChangeListener
import app.k9mail.legacy.preferences.GeneralSettingsManager
import app.k9mail.legacy.preferences.SubTheme
import java.util.concurrent.CopyOnWriteArraySet
import kotlinx.coroutines.flow.Flow

internal class FakeGeneralSettingsManager : GeneralSettingsManager {
private val listeners = CopyOnWriteArraySet<GeneralSettingsChangeListener>()

override fun getSettings(): GeneralSettings {
TODO("Not yet implemented")
}

override fun getSettingsFlow(): Flow<GeneralSettings> {
TODO("Not yet implemented")
}

override fun setShowRecentChanges(showRecentChanges: Boolean) {
TODO("Not yet implemented")
}

override fun setAppTheme(appTheme: AppTheme) {
TODO("Not yet implemented")
}

override fun setMessageViewTheme(subTheme: SubTheme) {
TODO("Not yet implemented")
}

override fun setMessageComposeTheme(subTheme: SubTheme) {
TODO("Not yet implemented")
}

override fun setFixedMessageViewTheme(fixedMessageViewTheme: Boolean) {
TODO("Not yet implemented")
}

override fun addListener(listener: GeneralSettingsChangeListener) {
listeners.add(listener)
}

override fun removeListener(listener: GeneralSettingsChangeListener) {
listeners.remove(listener)
}

fun notifyListeners() {
for (listener in listeners) {
listener.onGeneralSettingsChanged()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.k9mail.feature.navigation.drawer.domain.usecase

import app.cash.turbine.turbineScope
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfig
import assertk.assertThat
import assertk.assertions.isEqualTo
Expand All @@ -8,25 +9,66 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest

internal class GetDrawerConfigTest {
private val drawerConfigLoader = FakeDrawerConfigLoader()
private val generalSettingsManager = FakeGeneralSettingsManager()
private val getDrawerConfig = GetDrawerConfig(
configProver = drawerConfigLoader,
generalSettingsManager = generalSettingsManager,
)

@Test
fun `should get drawer config`() = runTest {
val drawerConfig = DrawerConfig(
showUnifiedFolders = true,
setDrawerConfig(
showUnifiedFolders = false,
showStarredCount = true,
)

val testSubject = GetDrawerConfig(
configProver = { drawerConfig },
)

val result = testSubject().first()
val result = getDrawerConfig().first()

assertThat(result).isEqualTo(
DrawerConfig(
showUnifiedFolders = true,
showUnifiedFolders = false,
showStarredCount = true,
),
)
}

@Test
fun `changing drawer config should emit`() = runTest {
setDrawerConfig(
showUnifiedFolders = true,
showStarredCount = true,
)

turbineScope {
val drawerConfigTurbine = getDrawerConfig().testIn(backgroundScope)

assertThat(drawerConfigTurbine.awaitItem()).isEqualTo(
DrawerConfig(
showUnifiedFolders = true,
showStarredCount = true,
),
)

setDrawerConfig(
showUnifiedFolders = true,
showStarredCount = false,
)

assertThat(drawerConfigTurbine.awaitItem()).isEqualTo(
DrawerConfig(
showUnifiedFolders = true,
showStarredCount = false,
),
)
}
}

private fun setDrawerConfig(
showUnifiedFolders: Boolean,
showStarredCount: Boolean,
) {
drawerConfigLoader.drawerConfig = DrawerConfig(showUnifiedFolders, showStarredCount)
generalSettingsManager.notifyListeners()
}
}
4 changes: 2 additions & 2 deletions legacy/core/src/main/java/com/fsck/k9/K9.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import app.k9mail.legacy.di.DI
import com.fsck.k9.core.BuildConfig
import com.fsck.k9.mail.K9MailLib
import com.fsck.k9.mailstore.LocalStore
import com.fsck.k9.preferences.RealGeneralSettingsManager
import com.fsck.k9.preferences.DefaultGeneralSettingsManager
import com.fsck.k9.preferences.Storage
import com.fsck.k9.preferences.StorageEditor
import kotlinx.datetime.Clock
Expand All @@ -20,7 +20,7 @@ import timber.log.Timber.DebugTree

// TODO "Use GeneralSettingsManager and GeneralSettings instead"
object K9 : KoinComponent {
private val generalSettingsManager: RealGeneralSettingsManager by inject()
private val generalSettingsManager: DefaultGeneralSettingsManager by inject()
private val telemetryManager: TelemetryManager by inject()

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@file:Suppress("DEPRECATION")

package com.fsck.k9.preferences

import app.k9mail.legacy.preferences.AppTheme
import app.k9mail.legacy.preferences.BackgroundSync
import app.k9mail.legacy.preferences.GeneralSettings
import app.k9mail.legacy.preferences.GeneralSettingsChangeListener
import app.k9mail.legacy.preferences.GeneralSettingsManager
import app.k9mail.legacy.preferences.SubTheme
import com.fsck.k9.K9
import com.fsck.k9.Preferences
import java.util.concurrent.CopyOnWriteArraySet
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -27,14 +27,17 @@ import timber.log.Timber
* The [GeneralSettings] instance managed by this class is updated with state from [K9] when [K9.saveSettingsAsync] is
* called.
*/
internal class RealGeneralSettingsManager(
@Suppress("TooManyFunctions")
internal class DefaultGeneralSettingsManager(
private val preferences: Preferences,
private val coroutineScope: CoroutineScope,
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : GeneralSettingsManager {
private val settingsFlow = MutableSharedFlow<GeneralSettings>(replay = 1)
private var generalSettings: GeneralSettings? = null

private val listeners = CopyOnWriteArraySet<GeneralSettingsChangeListener>()

@Deprecated("This only exists for collaboration with the K9 class")
val storage: Storage
get() = preferences.storage
Expand Down Expand Up @@ -88,6 +91,8 @@ internal class RealGeneralSettingsManager(
K9.save(editor)
writeSettings(editor, settings)
editor.commit()

notifyListeners()
}

@Synchronized
Expand Down Expand Up @@ -152,6 +157,20 @@ internal class RealGeneralSettingsManager(

return settings
}

override fun addListener(listener: GeneralSettingsChangeListener) {
listeners.add(listener)
}

override fun removeListener(listener: GeneralSettingsChangeListener) {
listeners.remove(listener)
}

private fun notifyListeners() {
for (listener in listeners) {
listener.onGeneralSettingsChanged()
}
}
}

private fun K9.BACKGROUND_OPS.toBackgroundSync(): BackgroundSync {
Expand All @@ -162,6 +181,7 @@ private fun K9.BACKGROUND_OPS.toBackgroundSync(): BackgroundSync {
}
}

@Suppress("TooGenericExceptionCaught")
private inline fun <reified T : Enum<T>> Storage.getEnum(key: String, defaultValue: T): T {
return try {
val value = getString(key, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import timber.log.Timber

internal class GeneralSettingsWriter(
private val preferences: Preferences,
private val generalSettingsManager: RealGeneralSettingsManager,
private val generalSettingsManager: DefaultGeneralSettingsManager,
) {
fun write(settings: InternalSettingsMap): Boolean {
// Convert general settings to the string representation used in preference storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ val preferencesModule = module {
factory { FolderSettingsProvider(folderRepository = get()) }
factory<AccountManager> { get<Preferences>() }
single {
RealGeneralSettingsManager(
DefaultGeneralSettingsManager(
preferences = get(),
coroutineScope = get(named("AppCoroutineScope")),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app.k9mail.legacy.preferences

fun interface GeneralSettingsChangeListener {
fun onGeneralSettingsChanged()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ interface GeneralSettingsManager {
fun setMessageViewTheme(subTheme: SubTheme)
fun setMessageComposeTheme(subTheme: SubTheme)
fun setFixedMessageViewTheme(fixedMessageViewTheme: Boolean)

fun addListener(listener: GeneralSettingsChangeListener)
fun removeListener(listener: GeneralSettingsChangeListener)
}
Loading