Skip to content

Instantly share code, notes, and snippets.

@aercanozcan
Last active July 15, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aercanozcan/eac0d7da9c651eb982ba to your computer and use it in GitHub Desktop.
Save aercanozcan/eac0d7da9c651eb982ba to your computer and use it in GitHub Desktop.
Simple Event Dispatcher
public abstract class Command {
private Command chain;
private CommandListener commandListener;
public Command(Command chain) {
this.chain = chain;
}
public Command() {
}
public abstract void execute(CommandListener listener);
public void executeChained(final CommandListener listener){
CommandListener chainedCommandListener = new CommandListener() {
@Override
public void onSuccess() {
listener.onSuccess();
if(chain.getChain()==null) {
chain.execute(listener);
}
else{
chain.executeChained(listener);
}
}
@Override
public void onFail(String msg, Exception error) {
listener.onFail(msg,error);
}
@Override
public void onStart() {
listener.onStart();
}
@Override
public void onFinish() {
}
};
execute(chainedCommandListener);
}
public Command getChain() {
return chain;
}
public void setChain(Command chain) {
this.chain = chain;
}
//recursively finds total serial commands
public int getLoadCount(){
if(chain == null) {
return 1;
}
else{
return 1 + chain.getLoadCount();
}
}
}
public interface CommandConfig {
List<Command> getCommandSet();
}
public interface CommandListener {
void onSuccess();
void onFail(String msg, Exception error);
void onStart();
void onFinish();
}
public class CommandManager extends Command {
private int loadingItemCount = 0;
private int loadedItems = 0;
private int failedItems = 0;
private UpdateListener updateListener = new UpdateListener();
private CommandListener listener;
private String errorMessages = "";
private CommandConfig commandConfig;
/**
* Creates an instance with a custom {@link CommandConfig}
* @param listener
* @param context
* @param config a custom config
*/
public CommandManager(Listener listener, Context context,Config config) {
super(app);
this.listener = listener;
commandConfig = config;
init();
}
private void init(){
loadingItemCount = 0 ;
for(Command command : commandConfig.getCommandSet()){
loadingItemCount += command.getLoadCount();
}
}
/**
* Executes all commands in commandConfig
*/
public void startEvents(){
loadedItems = 0;
failedItems = 0;
listener.onStart();
for (Command command : commandConfig.getCommandSet()) {
if (command.getChain() == null) {
command.execute(updateListener);
} else {
command.executeChained(updateListener);
}
}
}
/**
* This listener actually joins commands together
*/
class UpdateListener implements CommandListener{
@Override
public void onSuccess() {
increaseLoadedItemsCount();
if(isEnded()){
if(isSuccessful()){
listener.onSuccess();
}
else{
listener.onFail(errorMessages,null);
}
listener.onFinish();
}
}
@Override
public void onFail(String msg, Exception error) {
increaseFailedItemsCount();
errorMessages+= (" "+msg);
if(isEnded()){
listener.onFail(errorMessages,error);
listener.onFinish();
}
}
@Override
public void onStart() {
listener.onStart();
}
@Override
public void onFinish() {
}
}
@Override
public void execute(CommandListener listener) {
this.listener = listener;
startEvents();
}
/**
* synchronized method for thread join
* @return
*/
public synchronized int getLoadedItems() {
return loadedItems;
}
/**
* synchronized method for thread join
* @return number of failed items
*/
public synchronized int getFailedItems() {
return failedItems;
}
/**
* synchronized method for thread join
* @param failedItems to set
*/
public synchronized void setFailedItems(int failedItems) {
this.failedItems = failedItems;
}
/**
* synchronized method for thread join
* @param loadedItems to set
*/
public synchronized void setLoadedItems(int loadedItems) {
this.loadedItems = loadedItems;
}
/**
* synchronized method for thread join
*/
public synchronized void increaseLoadedItemsCount(){
setLoadedItems(getLoadedItems()+1);
}
/**
* synchronized method for thread join
*/
public synchronized void increaseFailedItemsCount(){ setFailedItems(getFailedItems() + 1);}
/**
* to show if all Commands are executed
* @return
*/
public synchronized boolean isEnded(){
return getLoadedItems() + getFailedItems() == loadingItemCount;
}
/**
* to show if all commands are successful
* @return is successful or not
*/
public synchronized boolean isSuccessful(){
return getLoadedItems() == loadingItemCount;
}
public class SampleUsage implements CommandConfig{
public void doStuff(Context context){
new CommandManager(new CommandListener() {
@Override
public void onSuccess() {
//all events are done
}
@Override
public void onFail(String msg, Exception error) {
//error occured
}
@Override
public void onStart() {
// started to execute commands
}
@Override
public void onFinish() {
// all commands are executed
}
}context,this).startEvents();
}
@Override
List<Command> getCommandSet(){
Command someCommand = new Command() {
@Override
public void execute(final CommandListener listener) {
AsyncTask someTtask =new AsyncTask<Void,Void,Boolean>(){
@Override
protected Boolean doInBackground(Void... params) {
//do stuff
return Boolean.TRUE;//returm success
}
@Override
protected void onPostExecute(Boolean aBoolean) {
if(aBoolean.booleanValue()){
listener.onSuccess();
}
else {
listener.onFail("some error",null);
}
listener.onFinish();
super.onPostExecute(aBoolean);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
listener.onStart();
}
};
}
};
Command otherCommand = new Command() {
@Override
public void execute(final CommandListener listener) {
AsyncTask someTtask =new AsyncTask<Void,Void,Boolean>(){
@Override
protected Boolean doInBackground(Void... params) {
//do some other stuff
return Boolean.TRUE;//returm success
}
@Override
protected void onPostExecute(Boolean aBoolean) {
if(aBoolean.booleanValue()){
listener.onSuccess();
}
else {
listener.onFail("some error",null);
}
listener.onFinish();
super.onPostExecute(aBoolean);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
listener.onStart();
}
};
}
};
Command yetAnotherCommand = new Command() {
@Override
public void execute(final CommandListener listener) {
AsyncTask someTtask =new AsyncTask<Void,Void,Boolean>(){
@Override
protected Boolean doInBackground(Void... params) {
//do yet another stuff
return Boolean.TRUE;//returm success
}
@Override
protected void onPostExecute(Boolean aBoolean) {
if(aBoolean.booleanValue()){
listener.onSuccess();
}
else {
listener.onFail("some error",null);
}
listener.onFinish();
super.onPostExecute(aBoolean);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
listener.onStart();
}
};
}
};
someCommand.setChain(otherCommand);// serial call
List<Command> commands = new ArrayList();
commands.add(someCommand);
commands.add(yetAnotherCommand);// parallel call
return commands;
}
}
@aaronfyber
Copy link

Go, go Ercan! Go with speed, aplomb and pizazz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment