Created
February 18, 2016 15:29
-
-
Save kakkoyun/525e5d7dbbffe39ee101 to your computer and use it in GitHub Desktop.
An Otto event bus subclass with force UI dispatch and stick events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.vispera.shark.miscellaneous; | |
import android.os.Handler; | |
import android.os.Looper; | |
import com.squareup.otto.Bus; | |
import com.squareup.otto.Produce; | |
import java.util.concurrent.ConcurrentHashMap; | |
import co.vispera.shark.events.BarcodeScanned; | |
import co.vispera.shark.events.Event; | |
import co.vispera.shark.events.DeletePhoto; | |
import co.vispera.shark.events.PhotoRetaken; | |
import co.vispera.shark.events.StorePhotoCaptured; | |
import co.vispera.shark.events.QuestionnairePhotoCaptured; | |
import co.vispera.shark.events.VisitSendFailed; | |
public class EventBus extends Bus { | |
private static final Handler mainThreadHandler = new Handler(Looper.getMainLooper()); | |
private final ConcurrentHashMap<Class<? extends Event>, Event> eventCache = new ConcurrentHashMap<>(); | |
private final EventProducer producer = new EventProducer(this); | |
@Override | |
public void post(final Object event) { | |
if (Looper.myLooper() == Looper.getMainLooper()) { | |
super.post(event); | |
} else { | |
mainThreadHandler.post(new Runnable() { | |
@Override public void run() { | |
post(event); | |
} | |
}); | |
} | |
} | |
public void postSticky(final Event event) { | |
eventCache.put(event.getClass(), event); | |
post(event); | |
} | |
public void removeSticky(final Event event) { | |
eventCache.remove(event.getClass(), event); | |
} | |
private Object fetchEvent(final Class<? extends Event> key) { | |
return eventCache.get(key); | |
} | |
class EventProducer { | |
public final EventBus bus; | |
public EventProducer(final EventBus bus) { | |
this.bus = bus; | |
this.bus.register(this); | |
} | |
@SuppressWarnings("unchecked") | |
private <T extends Event> T produce(Class<T> klazz) { | |
Object event = bus.fetchEvent(klazz); | |
if (event != null) { | |
return (T) event; | |
} | |
return null; | |
} | |
@Produce | |
public VisitSendFailed produceVisitSendFailed() { | |
return produce(VisitSendFailed.class); | |
} | |
@Produce | |
public PhotoRetaken producePhotoRetaken() { | |
return produce(PhotoRetaken.class); | |
} | |
@Produce | |
public DeletePhoto producePhotoDeleted() { | |
return produce(DeletePhoto.class); | |
} | |
@Produce | |
public StorePhotoCaptured produceStorePhotoCaptured() { | |
return produce(StorePhotoCaptured.class); | |
} | |
@Produce | |
public QuestionnairePhotoCaptured produceQuestionnairePhotoCaptured() { | |
return produce(QuestionnairePhotoCaptured.class); | |
} | |
@Produce | |
public BarcodeScanned barcodeScanned() { | |
return produce(BarcodeScanned.class); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment