blob: 4c13e3f6dabca37cb00e616f132ebad9437189b9 (
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.*;
public abstract class UIObservable {
Map<Object, UIObserver> observers = new WeakHashMap<>();
public void addObserver(Object weakKey, UIObserver observer) {
synchronized(this) {
observers.put(weakKey, observer);
}
}
public UIObserver removeObserver(Object weakKey) {
synchronized (this) {
return observers.remove(weakKey);
}
}
public void notifyChange(RBinding flag) {
notifyChange(EnumSet.of(flag));
}
public void notifyChange(EnumSet<RBinding> flags) {
List<UIObserver> obs = new ArrayList<>();
synchronized(this) {
obs.addAll(observers.values());
}
new UIHandler().post(() -> {
for (UIObserver observer : obs) {
observer.update(this, flags);
}
});
}
}
|