mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-22 07:12:19 +00:00
Added support of two channels in MainObservable (UI Actions and Other Actions)
This commit is contained in:
parent
72812f4e03
commit
69a1a685c4
@ -3,7 +3,7 @@ package io.github.chronosx88.influence.contracts.observer;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public interface Observable {
|
||||
void register(Observer observer);
|
||||
void unregister(Observer observer);
|
||||
void notifyObservers(JSONObject jsonObject);
|
||||
void register(Observer observer, int channelID);
|
||||
void unregister(Observer observer, int channelID);
|
||||
void notifyObservers(JSONObject jsonObject, int channelID);
|
||||
}
|
||||
|
@ -8,26 +8,74 @@ import io.github.chronosx88.influence.contracts.observer.Observable;
|
||||
import io.github.chronosx88.influence.contracts.observer.Observer;
|
||||
|
||||
public class MainObservable implements Observable {
|
||||
private ArrayList<Observer> observers;
|
||||
public static final int UI_ACTIONS_CHANNEL = 0;
|
||||
public static final int OTHER_ACTIONS_CHANNEL = 1;
|
||||
|
||||
private ArrayList<Observer> uiObservers;
|
||||
private ArrayList<Observer> otherObservers;
|
||||
|
||||
public MainObservable() {
|
||||
this.observers = new ArrayList<>();
|
||||
this.uiObservers = new ArrayList<>();
|
||||
this.otherObservers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(Observer observer) {
|
||||
observers.add(observer);
|
||||
public void register(Observer observer, int channelID) {
|
||||
switch (channelID) {
|
||||
case UI_ACTIONS_CHANNEL: {
|
||||
uiObservers.add(observer);
|
||||
break;
|
||||
}
|
||||
|
||||
case OTHER_ACTIONS_CHANNEL: {
|
||||
otherObservers.add(observer);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
otherObservers.add(observer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregister(Observer observer) {
|
||||
observers.remove(observer);
|
||||
public void unregister(Observer observer, int channelID) {
|
||||
switch (channelID) {
|
||||
case UI_ACTIONS_CHANNEL: {
|
||||
uiObservers.remove(observer);
|
||||
break;
|
||||
}
|
||||
|
||||
case OTHER_ACTIONS_CHANNEL: {
|
||||
otherObservers.remove(observer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyObservers(JSONObject jsonObject) {
|
||||
for (Observer observer : observers) {
|
||||
public void notifyObservers(JSONObject jsonObject, int channelID) {
|
||||
switch (channelID) {
|
||||
case UI_ACTIONS_CHANNEL: {
|
||||
for (Observer observer : uiObservers) {
|
||||
observer.handleEvent(jsonObject);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case OTHER_ACTIONS_CHANNEL: {
|
||||
for (Observer observer : otherObservers) {
|
||||
observer.handleEvent(jsonObject);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
for (Observer observer : otherObservers) {
|
||||
observer.handleEvent(jsonObject);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user