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

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add listener mechanism to GeneralSettingsManager
cketti committed Jan 21, 2025
commit 04fae12006928287e68d8ca5fdaba3949ece3c48
Original file line number Diff line number Diff line change
@@ -3,10 +3,12 @@ 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
@@ -34,6 +36,8 @@ internal class DefaultGeneralSettingsManager(
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
@@ -87,6 +91,8 @@ internal class DefaultGeneralSettingsManager(
K9.save(editor)
writeSettings(editor, settings)
editor.commit()

notifyListeners()
}

@Synchronized
@@ -151,6 +157,20 @@ internal class DefaultGeneralSettingsManager(

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()
}
}
Comment on lines +160 to +173
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to extract this into it's own class to allow reuse in feature configurations to avoid the need to always depend on GeneralSettingsManager when interested in change notifications.

}

private fun K9.BACKGROUND_OPS.toBackgroundSync(): BackgroundSync {
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app.k9mail.legacy.preferences

fun interface GeneralSettingsChangeListener {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be more generic like SettingsChangeListener and onSettingsChanged()

fun onGeneralSettingsChanged()
}
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +19 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, could be part of a specialized class to handle listener registration.

}