-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8813 from wmontwe/add-settings-change-notifier
Add settings change notifier
- Loading branch information
Showing
8 changed files
with
169 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...cy/preferences/src/main/java/app/k9mail/legacy/preferences/DefaultSettingsChangeBroker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package app.k9mail.legacy.preferences | ||
|
||
class DefaultSettingsChangeBroker( | ||
private val subscribers: MutableSet<SettingsChangeSubscriber> = mutableSetOf(), | ||
) : SettingsChangeBroker, SettingsChangePublisher { | ||
|
||
private val lock = Any() | ||
|
||
override fun subscribe(subscriber: SettingsChangeSubscriber) { | ||
synchronized(lock) { | ||
subscribers.add(subscriber) | ||
} | ||
} | ||
|
||
override fun unsubscribe(subscriber: SettingsChangeSubscriber) { | ||
synchronized(lock) { | ||
subscribers.remove(subscriber) | ||
} | ||
} | ||
|
||
override fun publish() { | ||
val currentSubscribers = synchronized(lock) { HashSet(subscribers) } | ||
|
||
for (subscriber in currentSubscribers) { | ||
subscriber.receive() | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
legacy/preferences/src/main/java/app/k9mail/legacy/preferences/SettingsChangeBroker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app.k9mail.legacy.preferences | ||
|
||
/** | ||
* Broker to manage subscribers and notify them about changes in the settings, when the | ||
* [SettingsChangePublisher] publishes a change. | ||
*/ | ||
interface SettingsChangeBroker { | ||
|
||
/** | ||
* Subscribe to settings changes. | ||
* | ||
* @param subscriber The subscriber to be notified about settings changes. | ||
*/ | ||
fun subscribe(subscriber: SettingsChangeSubscriber) | ||
|
||
/** | ||
* Unsubscribe from settings changes. | ||
* | ||
* @param subscriber The subscriber that no longer wants to be notified about settings changes. | ||
*/ | ||
fun unsubscribe(subscriber: SettingsChangeSubscriber) | ||
} |
12 changes: 12 additions & 0 deletions
12
legacy/preferences/src/main/java/app/k9mail/legacy/preferences/SettingsChangePublisher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.k9mail.legacy.preferences | ||
|
||
/** | ||
* Publishes changes in the settings. | ||
*/ | ||
interface SettingsChangePublisher { | ||
|
||
/** | ||
* Publish a change in the settings. | ||
*/ | ||
fun publish() | ||
} |
12 changes: 12 additions & 0 deletions
12
legacy/preferences/src/main/java/app/k9mail/legacy/preferences/SettingsChangeSubscriber.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.k9mail.legacy.preferences | ||
|
||
/** | ||
* Subscribe to be notified about changes in the settings. | ||
*/ | ||
fun interface SettingsChangeSubscriber { | ||
|
||
/** | ||
* Called when settings change. | ||
*/ | ||
fun receive() | ||
} |
49 changes: 49 additions & 0 deletions
49
...references/src/test/java/app/k9mail/legacy/preferences/DefaultSettingsChangeBrokerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package app.k9mail.legacy.preferences | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.contains | ||
import assertk.assertions.doesNotContain | ||
import assertk.assertions.isEqualTo | ||
import kotlin.test.Test | ||
|
||
class DefaultSettingsChangeBrokerTest { | ||
|
||
@Test | ||
fun `subscribe should add subscriber`() { | ||
val subscriber = SettingsChangeSubscriber { } | ||
val subscribers = mutableSetOf<SettingsChangeSubscriber>() | ||
val broker = DefaultSettingsChangeBroker(subscribers) | ||
|
||
broker.subscribe(subscriber) | ||
|
||
assertThat(subscribers.size).isEqualTo(1) | ||
assertThat(subscribers).contains(subscriber) | ||
} | ||
|
||
@Test | ||
fun `unsubscribe should remove subscriber`() { | ||
val subscriber = SettingsChangeSubscriber { } | ||
val subscribers = mutableSetOf<SettingsChangeSubscriber>(subscriber) | ||
val broker = DefaultSettingsChangeBroker(subscribers) | ||
|
||
broker.unsubscribe(subscriber) | ||
|
||
assertThat(subscribers.size).isEqualTo(0) | ||
assertThat(subscribers).doesNotContain(subscriber) | ||
} | ||
|
||
@Test | ||
fun `publish should notify subscribers`() { | ||
var received = false | ||
val subscriber = SettingsChangeSubscriber { received = true } | ||
var receivedOther = false | ||
val otherSubscriber = SettingsChangeSubscriber { receivedOther = true } | ||
val subscribers = mutableSetOf<SettingsChangeSubscriber>(subscriber, otherSubscriber) | ||
val broker = DefaultSettingsChangeBroker(subscribers) | ||
|
||
broker.publish() | ||
|
||
assertThat(received).isEqualTo(true) | ||
assertThat(receivedOther).isEqualTo(true) | ||
} | ||
} |