Skip to content

Instantly share code, notes, and snippets.

@ronshapiro
Created November 8, 2017 02:15
Show Gist options
  • Save ronshapiro/7b92eb2ea111a90550a57a79f2908325 to your computer and use it in GitHub Desktop.
Save ronshapiro/7b92eb2ea111a90550a57a79f2908325 to your computer and use it in GitHub Desktop.
dagger.android without O(N) subcomponents
@Subcomponent
public abstract class MonolithicActivityInjector implements AndroidInjector<Activity> {
@Override
public final void inject(Activity activity) {
if (activity instanceof RedActivity) {
inject((RedActivity) activity);
} else if (activity instanceof BlueActivity) {
inject((BlueActivity) activity);
} else if (activity instanceof YellowActivity) {
inject((YellowActivity) activity);
} else {
throw new IllegalArgumentException();
}
/* btw, you should totally codegen this:
@MonolithicActivitySubcomponent({
RedActivity.class,
BlueActivity.class,
YellowActivity.class,
})
interface Foo {}
*/
// or, if you want something that's not linear, but not proguardable:
switch (activity.getClass().getName()) {
case "com.example.RedActivity": inject((RedActivity) activity); break;
case "com.example.BlueActivity": inject((BlueActivity) activity); break;
case "com.example.YellowActivity": inject((YellowActivity) activity); break;
default: throw new IllegalArgumentException();
}
// or some other clever technique
}
public abstract void inject(RedActivity red);
public abstract void inject(BlueActivity blue);
public abstract void inject(YellowActivity yellow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment