Added support of two channels in MainObservable (UI Actions and Other Actions)

This commit is contained in:
ChronosX88 2019-03-18 17:34:23 +04:00
parent 72812f4e03
commit 69a1a685c4
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 60 additions and 12 deletions

View File

@ -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);
}

View File

@ -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) {
observer.handleEvent(jsonObject);
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;
}
}
}
}