Created
November 22, 2013 16:09
-
-
Save imminent/7602365 to your computer and use it in GitHub Desktop.
Example of setup that registers/unregisters non-singleton POJOs with an Event Bus.
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 com.example; | |
import android.annotation.TargetApi; | |
import android.app.Application; | |
import android.app.ActivityLifecycleCallbacks; | |
import com.squareup.otto.Bus; | |
import javax.inject.Inject; | |
import javax.inject.Provider; | |
import dagger.ObjectGraph; | |
import java.util.Map; | |
import java.util.HashMap; | |
public class MyApplication extends Application { | |
@Inject /* package */Provider<HomeController> home_controller; | |
@Inject /* package */Provider<SettingsController> settings_controller; | |
public void onCreate() { | |
super.onCreate(); | |
registerActivityLifecycleCallbacks(activityLifecycleCallbacks()); | |
controller_bus = new Bus("ControllerBus"); | |
_presentation_controllers = bindPresentationsToControllers(); | |
/* Dependency injection object graph */ | |
final ObjectGraph object_graph = createObjectGraph(); | |
object_graph.inject(this); | |
_presentation_controllers = new HashMap<Class<?>, Provider>(2); | |
_presentation_controllers.put(HomeActivity.class, home_controller); | |
_presentation_controllers.put(SettingsActivity.class, settings_controller); | |
_controllers = new HashMap<Class<?>, Object>(); | |
} | |
public void sendMessage(Object message) { | |
controller_bus.post(message); | |
} | |
/** | |
* <p>Retrieves the {@link ActivityLifecycleCallbacks} that binds Prestige to the Activity | |
* lifecycles so that it can conjure Controllers behind the curtains.</p> | |
*/ | |
@TargetApi(ICE_CREAM_SANDWICH) | |
public static ActivityLifecycleCallbacks activityLifecycleCallbacks() { | |
return new ActivityLifecycleCallbacks() { | |
/** | |
* Destroys the Controller associated with the given Activity. | |
*/ | |
@Override public void onActivityDestroyed(Activity activity) { | |
final Class<?> activity_class = activity.getClass(); | |
if (!_presentation_controllers.containsKey(activity_class)) return; | |
_controllers.remove(activity_class); | |
} | |
/** | |
* Creates the Controller associated with the given Activity. | |
*/ | |
@Override public void onActivityCreated(Activity activity, Bundle __) { | |
final Class<?> activity_class = activity.getClass(); | |
if (!_presentation_controllers.containsKey(activity_class)) return; | |
final Object controller = _presentation_controllers.get(activity_class).get(); | |
_controllers.put(activity_class, controller); | |
} | |
/** | |
* Registers the Controller that is associated with the given Activity to the Controller Bus. | |
*/ | |
@Override public void onActivityResumed(Activity activity) { | |
final Class<?> activity_class = activity.getClass(); | |
if (!_controllers.containsKey(activity_class)) return; | |
controller_bus.register(_controllers.get(activity_class)); | |
} | |
/** | |
* Unregisters the Controller that is associated with the given Activity from the Controller Bus. | |
*/ | |
@Override public void onActivityPaused(Activity activity) { | |
final Class<?> activity_class = activity.getClass(); | |
if (!_controllers.containsKey(activity_class)) return; | |
controller_bus.unregister(_controllers.get(activity_class)); | |
} | |
@Override public void onActivityStopped(Activity activity) { } | |
@Override public void onActivityStarted(Activity _) { } | |
@Override public void onActivitySaveInstanceState(Activity _, Bundle __) { } | |
}; | |
} | |
/** Bus over which Presentations communicate to their Controllers */ | |
private Bus controller_bus; | |
/** Provides the Controller implementation for the given Presentation Implementation */ | |
private Map<Class<?>, Provider> _presentation_controllers; | |
/** Maintains the Controller references as they are being used */ | |
private Map<Class<?>, Object> _controllers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment