dagger2-mortar-flow-experiment
dagger2-mortar-flow-experiment copied to clipboard
Blueprint is gone
With Blueprint gone now I ended up working around it with lines like these:
String childName = getChildNameSomehow();
MortarScope parent = getParentScope();
MortarScope scope = parent.findChild(childName);
if (scope == null) {
scope = parent.createChild(childName, Dagger2.buildComponent(My.Component.class, parent.getObjectGraph()));
}
scope.<My.Component>getObjectGraph().inject(this);
Is this how we should handle building components now?
ok so I solved this by passing a custom context factory (modified version of https://github.com/square/mortar/blob/master/mortar-sample/src/main/java/com/example/mortar/mortarflow/MortarContextFactory.java) to the SimplePathContainer.
It reads the value of WithComponent annotation of the given screen and passes it to Dagger2.buildComponent. Then the result can be accessed via wrapped context so the dependencies injection can be done just by:
Mortar.getScope(getContext() /* wrapped context */ ).<My.Component>getObjectGraph().inject(this /* view instance */ );
Yep. Note that a util method is available in https://github.com/square/mortar/blob/master/dagger2support
=> Dagger2.<My.Component>get(getContext()).inject(this);