blob: a8f895852d934432255eab2f77dbc67e1381c48d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package net.lacolaco.smileessence.util
import net.lacolaco.smileessence.entity.RBinding
import java.util.*
abstract class UIObservable {
private val observers = WeakHashMap<Any, (EnumSet<RBinding>) -> Unit>()
fun addObserver(weakKey: Any, observer: (EnumSet<RBinding>) -> Unit) {
synchronized(this) {
observers.put(weakKey, observer)
}
}
fun removeObserver(weakKey: Any) {
synchronized(this) {
observers.remove(weakKey)
}
}
protected fun notifyChange(flag: RBinding) {
notifyChange(EnumSet.of(flag))
}
protected fun notifyChange(flags: EnumSet<RBinding>) {
val obs = ArrayList<(EnumSet<RBinding>) -> Unit>()
synchronized(this) {
obs.addAll(observers.values)
}
UIHandler().post {
for (observer in obs) {
observer(flags)
}
}
}
}
|