Skip to content

Instantly share code, notes, and snippets.

View vvsevolodovich's full-sized avatar

Vladimir Ivanov vvsevolodovich

View GitHub Profile
public class EventBus {
private final SubscriberRegistry subscribers = new SubscriberRegistry(this);
public void register(Object object) {
subscribers.register(object);
}
public void unregister(Object object) {
subscribers.unregister(object);
private static final class PerThreadQueuedDispatcher extends Dispatcher {
private final ThreadLocal<Queue<Event>> queue =
new ThreadLocal<Queue<Event>>() {
@Override
protected Queue<Event> initialValue() {
return Queues.newArrayDeque();
}
};
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 08:57
Background class first attempt
public class Background {
private final ExecutorService mService = new ScheduledThreadPoolExecutor(5);
public Future<?> execute(Runnable runnable) {
return mService.submit(runnable);
}
public <T> Future<T> submit(Callable<T> runnable) {
return mService.submit(runnable);
}
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 08:58
Background with Handler
public class Background {
...
private final Handler mUiHandler;
public void postOnUiThread(final Runnable runnable) {
mUiHandler.post(runnable);
}
}
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 09:00
Background with Bus
public class Background {
private final Bus mEventBus;
public void postEvent(final Object event) {
mEventBus.post(event);
}
}
@vvsevolodovich
vvsevolodovich / ApplicationInitializer.java
Last active March 12, 2018 09:02
App db init with posting an event
mBackground.execute(new Runnable() {
@Override
public void run() {
try {
initDatabaseInternal();
mBackground.post(new DatabaseLoadedEvent());
} catch (Exception e) {
Log.e("Failed to init db", e);
@vvsevolodovich
vvsevolodovich / SplashActivity.java
Created March 12, 2018 09:04
SplashActivity with event listening
public class SplashActivity extends Activity {
@Override
protected void onStart() {
super.onStart();
eventBus.register(this);
}
@Override
protected void onStop() {
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 09:05
Background with posting event on UI
public void postEventOnUiThread(final Object event) {
mUiHandler.post(new Runnable() {
@Override
public void run() {
mEventBus.post(event);
}
});
}
@vvsevolodovich
vvsevolodovich / SplashActivity.java
Created March 12, 2018 09:05
Splash with rerunning the code on UI
public class SplashActivity extends Activity {
@Subscribe
public void on(DatabaseLoadedEvent event) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
showMainActivity();
}
public abstract class Scheduler {
@NonNull
public Disposable scheduleDirect(@NonNull Runnable run) { ... }
@NonNull
public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) { ... }
@NonNull
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) { ... } {