mirror of
https://github.com/DrKLO/Telegram.git
synced 2025-03-25 16:12:00 +01:00
simplification and code cleanup
This commit is contained in:
parent
1a4d0786ec
commit
b7692f2d85
1 changed files with 26 additions and 45 deletions
|
@ -10,58 +10,57 @@ package org.telegram.messenger;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class NotificationCenter {
|
public class NotificationCenter {
|
||||||
final private HashMap<Integer, ArrayList<Object>> observers = new HashMap<Integer, ArrayList<Object>>();
|
|
||||||
final private HashMap<Integer, Object> memCache = new HashMap<Integer, Object>();
|
public static NotificationCenter Instance = new NotificationCenter();
|
||||||
final private HashMap<String, Object> memCacheString = new HashMap<String, Object>();
|
|
||||||
|
private final Map<Integer, List<Object>> observers = new HashMap<Integer, List<Object>>();
|
||||||
|
|
||||||
|
private Map<String, Object> memCache = new HashMap<String, Object>();
|
||||||
|
private Map<Integer, Object> removeAfterBroadcast = new HashMap<Integer, Object>();
|
||||||
|
|
||||||
private boolean broadcasting = false;
|
private boolean broadcasting = false;
|
||||||
final private HashMap<Integer, Object> removeAfterBroadcast = new HashMap<Integer, Object>();
|
|
||||||
|
|
||||||
public interface NotificationCenterDelegate {
|
public interface NotificationCenterDelegate {
|
||||||
public abstract void didReceivedNotification(int id, Object... args);
|
public abstract void didReceivedNotification(int id, Object... args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NotificationCenter Instance = new NotificationCenter();
|
|
||||||
|
|
||||||
public void addToMemCache(int id, Object object) {
|
public void addToMemCache(int id, Object object) {
|
||||||
memCache.put(id, object);
|
addToMemCache(String.valueOf(id), object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToMemCache(String id, Object object) {
|
public void addToMemCache(String id, Object object) {
|
||||||
memCacheString.put(id, object);
|
memCache.put(id, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getFromMemCache(int id) {
|
public Object getFromMemCache(int id) {
|
||||||
Object obj = memCache.get(id);
|
return getFromMemCache(String.valueOf(id), null);
|
||||||
if (obj != null) {
|
|
||||||
memCache.remove(id);
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getFromMemCache(String id, Object defaultValue) {
|
public Object getFromMemCache(String id, Object defaultValue) {
|
||||||
Object obj = memCacheString.get(id);
|
Object obj = memCache.get(id);
|
||||||
if (obj != null) {
|
if (obj != null) {
|
||||||
memCacheString.remove(id);
|
memCache.remove(id);
|
||||||
} else {
|
return obj;
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
return obj;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void postNotificationName(int id, Object... args) {
|
public void postNotificationName(int id, Object... args) {
|
||||||
synchronized (observers) {
|
synchronized (observers) {
|
||||||
broadcasting = true;
|
broadcasting = true;
|
||||||
ArrayList<Object> objects = observers.get(id);
|
List<Object> objects = observers.get(id);
|
||||||
if (objects != null) {
|
if (objects != null) {
|
||||||
for (Object obj : objects) {
|
for (Object obj : objects) {
|
||||||
((NotificationCenterDelegate)obj).didReceivedNotification(id, args);
|
((NotificationCenterDelegate) obj).didReceivedNotification(id, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
broadcasting = false;
|
broadcasting = false;
|
||||||
if (!removeAfterBroadcast.isEmpty()) {
|
if (!removeAfterBroadcast.isEmpty()) {
|
||||||
for (HashMap.Entry<Integer, Object> entry : removeAfterBroadcast.entrySet()) {
|
for (Map.Entry<Integer, Object> entry : removeAfterBroadcast.entrySet()) {
|
||||||
removeObserver(entry.getValue(), entry.getKey());
|
removeObserver(entry.getValue(), entry.getKey());
|
||||||
}
|
}
|
||||||
removeAfterBroadcast.clear();
|
removeAfterBroadcast.clear();
|
||||||
|
@ -71,44 +70,26 @@ public class NotificationCenter {
|
||||||
|
|
||||||
public void addObserver(Object observer, int id) {
|
public void addObserver(Object observer, int id) {
|
||||||
synchronized (observers) {
|
synchronized (observers) {
|
||||||
ArrayList<Object> objects = observers.get(id);
|
if (!observers.containsKey(id)) {
|
||||||
if (objects == null) {
|
observers.put(id, new ArrayList<Object>());
|
||||||
objects = new ArrayList<Object>();
|
|
||||||
observers.put(id, objects);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Object> objects = observers.get(id);
|
||||||
if (objects.contains(observer)) {
|
if (objects.contains(observer)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
objects.add(observer);
|
objects.add(observer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void removeObserver(Object observer) {
|
|
||||||
// synchronized (observers) {
|
|
||||||
// if (broadcasting) {
|
|
||||||
// removeAfterBroadcast.put(-1, observer);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// Integer[] keyArr = (Integer[])observers.keySet().toArray();
|
|
||||||
// for (int a = 0; a < observers.size(); a++) {
|
|
||||||
// Integer id = keyArr[a];
|
|
||||||
// ArrayList<Object> objects = observers.get(id);
|
|
||||||
// objects.remove(observer);
|
|
||||||
// if (objects.size() == 0) {
|
|
||||||
// observers.remove(id);
|
|
||||||
// a--;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void removeObserver(Object observer, int id) {
|
public void removeObserver(Object observer, int id) {
|
||||||
synchronized (observers) {
|
synchronized (observers) {
|
||||||
if (broadcasting) {
|
if (broadcasting) {
|
||||||
removeAfterBroadcast.put(id, observer);
|
removeAfterBroadcast.put(id, observer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Object> objects = observers.get(id);
|
List<Object> objects = observers.get(id);
|
||||||
if (objects != null) {
|
if (objects != null) {
|
||||||
objects.remove(observer);
|
objects.remove(observer);
|
||||||
if (objects.size() == 0) {
|
if (objects.size() == 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue