Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaspili/fc8d41d0dcd36e149695 to your computer and use it in GitHub Desktop.
Save lukaspili/fc8d41d0dcd36e149695 to your computer and use it in GitHub Desktop.
Dagger 2 / Mortar / Flow
@Layout(R.layout.view_login)
@WithComponent(LoginScreen.Component.class)
public class LoginScreen extends Path {
@dagger.Component(dependencies = RootActivity.Component.class)
@PerScreenScope(Component.class)
public static interface Component extends RootActivity.Component {
void inject(LoginView view);
}
@PerScreenScope(Component.class)
public static class Presenter extends ViewPresenter<LoginView> {
@Inject
public Presenter() { }
}
}
public class LoginView extends LinearLayout {
@Inject
LoginScreen.Presenter presenter;
public LoginView(Context context, AttributeSet attrs) {
super(context, attrs);
DaggerService.<LoginScreen.Component>getDaggerComponent(context).inject(this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.inject(this);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
presenter.takeView(this);
}
@Override
protected void onDetachedFromWindow() {
presenter.dropView(this);
super.onDetachedFromWindow();
}
}
public final class MortarContextFactory implements PathContextFactory {
@Override
public Context setUpContext(Path path, Context parentContext) {
MortarScope scope = MortarScope.findChild(parentContext, path.getClass().getName());
if (scope == null) {
final WithComponent withComponent = path.getClass().getAnnotation(WithComponent.class);
if (withComponent == null) {
throw new IllegalStateException(String.format("Missing WithComponent annotation on %s", path.getClass().getName()));
}
MortarScope parentScope = MortarScope.getScope(parentContext);
Object component = parentScope.getService(DaggerService.SERVICE_NAME);
scope = MortarScope.buildChild(parentContext, path.getClass().getName())
.withService(DaggerService.SERVICE_NAME, DaggerService.createComponent(withComponent.value(), component))
.build();
}
return new TearDownContext(parentContext, scope);
}
@Override
public void tearDownContext(Context context) {
TearDownContext.destroyScope(context);
}
static class TearDownContext extends ContextWrapper {
private static final String SERVICE = "SNEAKY_MORTAR_PARENT_HOOK";
private final MortarScope parentScope;
private LayoutInflater inflater;
static void destroyScope(Context context) {
MortarScope scope = MortarScope.getScope(context);
scope.destroy();
}
public TearDownContext(Context context, MortarScope scope) {
super(scope.createContext(context));
this.parentScope = MortarScope.getScope(context);
}
@Override
public Object getSystemService(String name) {
if (LAYOUT_INFLATER_SERVICE.equals(name)) {
if (inflater == null) {
inflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
}
return inflater;
}
if (SERVICE.equals(name)) {
return parentScope;
}
return super.getSystemService(name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment