Add listener mechanism to BackendManager
@@ -3,16 +3,19 @@ package com.fsck.k9.backend
import com.fsck.k9.Account
import com.fsck.k9.backend.api.Backend
import com.fsck.k9.mail.ServerSettings
+import java.util.concurrent.CopyOnWriteArraySet
class BackendManager(private val backendFactories: Map<String, BackendFactory>) {
private val backendCache = mutableMapOf<String, BackendContainer>()
+ private val listeners = CopyOnWriteArraySet<BackendChangedListener>()
fun getBackend(account: Account): Backend {
- synchronized(backendCache) {
+ val newBackend = synchronized(backendCache) {
val container = backendCache[account.uuid]
- return if (container != null && isBackendStillValid(container, account)) {
- container.backend
- } else {
+ if (container != null && isBackendStillValid(container, account)) {
+ return container.backend
+ }
+
createBackend(account).also { backend ->
backendCache[account.uuid] = BackendContainer(
backend,
@@ -21,7 +24,10 @@ class BackendManager(private val backendFactories: Map<String, BackendFactory>)
)
}
}
- }
+
+ notifyListeners(account)
+
+ return newBackend
}
private fun isBackendStillValid(container: BackendContainer, account: Account): Boolean {
@@ -33,6 +39,8 @@ class BackendManager(private val backendFactories: Map<String, BackendFactory>)
synchronized(backendCache) {
backendCache.remove(account.uuid)
}
+
+ notifyListeners(account)
}
private fun createBackend(account: Account): Backend {
@@ -41,9 +49,27 @@ class BackendManager(private val backendFactories: Map<String, BackendFactory>)
return backendFactory.createBackend(account)
}
+ fun addListener(listener: BackendChangedListener) {
+ listeners.add(listener)
+ }
+
+ fun removeListener(listener: BackendChangedListener) {
+ listeners.remove(listener)
+ }
+
+ private fun notifyListeners(account: Account) {
+ for (listener in listeners) {
+ listener.onBackendChanged(account)
+ }
+ }
+}
+
private data class BackendContainer(
val backend: Backend,
val incomingServerSettings: ServerSettings,
val outgoingServerSettings: ServerSettings
)
+
+fun interface BackendChangedListener {
+ fun onBackendChanged(account: Account)
}
