dagger2-mortar-flow-experiment icon indicating copy to clipboard operation
dagger2-mortar-flow-experiment copied to clipboard

Blueprint is gone

Open vishna opened this issue 10 years ago • 2 comments

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?

vishna avatar Jan 31 '15 16:01 vishna

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 */ );

vishna avatar Feb 02 '15 16:02 vishna

Yep. Note that a util method is available in https://github.com/square/mortar/blob/master/dagger2support

=> Dagger2.<My.Component>get(getContext()).inject(this);

pyricau avatar Feb 02 '15 17:02 pyricau