Exemple #1
0
  /**
   * Finds or creates the scope for the given screen, honoring its optional {@link
   * WithModuleFactory} or {@link WithModule} annotation. Note that scopes are also created for
   * unannotated screens.
   */
  public MortarScope getScreenScope(
      Resources resources, MortarScope parentScope, final String name, final Object screen) {
    ModuleFactory moduleFactory = getModuleFactory(screen);
    Object[] childModule;
    if (moduleFactory != NO_FACTORY) {
      childModule = new Object[] {moduleFactory.createDaggerModule(resources, screen)};
    } else {
      // We need every screen to have a scope, so that anything it injects is scoped.  We need
      // this even if the screen doesn't declare a module, because Dagger allows injection of
      // objects that are annotated even if they don't appear in a module.
      childModule = new Object[0];
    }

    MortarScope childScope = parentScope.findChild(name);
    if (childScope == null) {
      childScope =
          parentScope
              .buildChild()
              .withService(
                  ObjectGraphService.SERVICE_NAME,
                  ObjectGraphService.create(parentScope, childModule))
              .build(name);
    }

    return childScope;
  }
  @Override
  public Object getSystemService(String name) {
    if (scope != null && scope.hasService(name)) {
      return scope.getService(name);
    }

    return super.getSystemService(name);
  }
 @Override
 public Object getSystemService(String name) {
   // Note we dont create the scope here since this is usually called
   // before onCreate and we need to be able to fetch our scope name
   // on configuration changes
   return (mActivityScope != null && mActivityScope.hasService(name))
       ? mActivityScope.getService(name)
       : super.getSystemService(name);
 }
  @Override
  public Object getSystemService(String name) {
    if (rootScope == null) {
      rootScope =
          MortarScope.buildRootScope()
              .withService(
                  ObjectGraphService.SERVICE_NAME, ObjectGraph.create(new ApplicationModule(this)))
              .build("Root");
    }

    if (rootScope.hasService(name)) return rootScope.getService(name);

    return super.getSystemService(name);
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   if (mActivityScope != null) {
     mActivityScope.destroy();
     mActivityScope = null;
   }
 }
  public static Navigator create(
      MortarScope containerScope, StackableParceler parceler, Config config) {
    if (config == null) {
      config = new Config();
    }

    Preconditions.checkNotNull(containerScope, "Mortar scope for Navigator cannot be null");
    Preconditions.checkArgument(
        config.dontRestoreStackAfterKill || parceler != null,
        "StackableParceler for Navigator cannot be null");

    Navigator navigator = new Navigator(parceler, config);

    MortarScope scope =
        containerScope.buildChild().withService(SERVICE_NAME, navigator).build(SCOPE_NAME);
    scope.register(navigator);

    return navigator;
  }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   AppComponent app = DaggerService.getDaggerComponent(getApplicationContext());
   mActivityScope =
       MortarScope.buildChild(getApplicationContext())
           .withService(
               DaggerService.DAGGER_SERVICE,
               DaggerSettingsActivityComponent.builder().appComponent(app).build())
           .build(UUID.randomUUID().toString());
   super.onCreate(savedInstanceState);
 }
  @Override
  protected void onDestroy() {
    activityPresenter.dropView(this);

    navigator.delegate().onDestroy();
    navigator = null;

    if (isFinishing() && scope != null) {
      scope.destroy();
      scope = null;
    }

    super.onDestroy();
  }
 public TearDownContext(Context context, MortarScope scope) {
   super(scope.createContext(context));
   this.parentScope = MortarScope.getScope(context);
 }
 static void destroyScope(Context context) {
   MortarScope.getScope(context).destroy();
 }
 /**
  * Retreive the navigator from the nearest child of the current context Use this method from the
  * host of a navigator container view to retrieve the associated navigator
  */
 public static Navigator find(Context context) {
   MortarScope scope = MortarScope.findChild(context, SCOPE_NAME);
   return scope != null ? scope.<Navigator>getService(SERVICE_NAME) : null;
 }
  @Override
  protected void performTraversal(
      final ViewGroup containerView,
      final TraversalState traversalState,
      final Flow.Direction direction,
      final Flow.TraversalCallback callback) {

    final PathContext context;
    final PathContext oldPath;
    if (containerView.getChildCount() > 0) {
      oldPath = PathContext.get(containerView.getChildAt(0).getContext());
    } else {
      oldPath = PathContext.root(containerView.getContext());
    }

    final Path to = traversalState.toPath();

    View newView;
    context = PathContext.create(oldPath, to, contextFactory);
    int layout = getLayout(to);

    MortarScope parentScope = Mortar.getScope(pathContainerView.getContext());

    Blueprint blueprint = getBlueprint(to);

    //noinspection unchecked
    Context childContext = parentScope.requireChild(blueprint).createContext(context);

    newView = LayoutInflater.from(childContext).inflate(layout, containerView, false);

    View fromView = null;
    if (traversalState.fromPath() != null) {
      fromView = containerView.getChildAt(0);
      traversalState.saveViewState(fromView);
    }
    traversalState.restoreViewState(newView);

    if (fromView == null || direction == REPLACE) {
      containerView.removeAllViews();
      containerView.addView(newView);
      oldPath.destroyNotIn(context, contextFactory);
      callback.onTraversalCompleted();
    } else {
      containerView.addView(newView);
      final View finalFromView = fromView;
      waitForMeasure(
          newView,
          new OnMeasuredCallback() {
            @Override
            public void onMeasured(View view, int width, int height) {
              runAnimation(
                  containerView,
                  finalFromView,
                  view,
                  direction,
                  new Flow.TraversalCallback() {
                    @Override
                    public void onTraversalCompleted() {
                      containerView.removeView(finalFromView);
                      oldPath.destroyNotIn(context, contextFactory);
                      callback.onTraversalCompleted();
                    }
                  });
            }
          });
    }
  }
Exemple #13
0
 public MortarScope getScreenScope(Context context, String name, Object screen) {
   MortarScope parentScope = MortarScope.getScope(context);
   return getScreenScope(context.getResources(), parentScope, name, screen);
 }