Example #1
0
  public MainView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (isInEditMode()) {
      return;
    }

    Mortar.inject(context, this);

    screenConductor = new ScreenConductor<>(context, this);
  }
 @Override
 protected MortarScope extractScope(View view) {
   return Mortar.getScope(view.getMortarContext());
 }
  @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();
                    }
                  });
            }
          });
    }
  }