Created
December 4, 2014 21:40
-
-
Save mustafa01ali/3d03d1be131a806ce561 to your computer and use it in GitHub Desktop.
Otto wrapper
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
import android.os.Handler; | |
import android.os.Looper; | |
import com.squareup.otto.Bus; | |
/** | |
* @author Mustafa Ali | |
*/ | |
public class AppBus { | |
private Bus mBus = new Bus(); | |
private final Handler mainThread = new Handler(Looper.getMainLooper()); | |
/** | |
* Registers the instance on the bus | |
*/ | |
public void register(Object object) { | |
mBus.register(object); | |
} | |
/** | |
* Unregisters the instance | |
*/ | |
public void unregister(Object object) { | |
mBus.unregister(object); | |
} | |
/** | |
* Post the event on the bus. All events posted with this method are always delivered on the main thread. | |
*/ | |
public void post(final Object event) { | |
if (Looper.myLooper() == Looper.getMainLooper()) { | |
mBus.post(event); | |
} else { | |
mainThread.post(new Runnable() { | |
@Override | |
public void run() { | |
mBus.post(event); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment