com.bluelinelabs.conductor.Controller Java Examples

The following examples show how to use com.bluelinelabs.conductor.Controller. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvpViewStateConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Override public void preAttach(@NonNull Controller controller, @NonNull View view) {
  MvpViewStateConductorDelegateCallback<V, P, VS> vsCallback =
      ((MvpViewStateConductorDelegateCallback<V, P, VS>) callback);

  if (vsCallback.getViewState() == null) {
    // Creating view for the first time, so onRestoreViewState() never has been called
    // Create ViewState
    VS viewState = vsCallback.createViewState();
    if (viewState == null) {
      throw new NullPointerException("ViewState from createViewState() is null in " + vsCallback);
    }

    vsCallback.setViewState(viewState);
    vsCallback.onNewViewStateInstance();
  }

  super.preAttach(controller, view);
}
 
Example #2
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionOnSaveViewState() {
  Controller controller = PowerMockito.mock(Controller.class);

  MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

  MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

  Mockito.when(callback.getViewState()).thenReturn(null);
  Bundle bundle = new Bundle();

  try {
    lifecycleListener.onSaveViewState(controller, bundle);
    Assert.fail("NullPointerException expected");
  } catch (NullPointerException e) {
    Assert.assertEquals(e.getMessage(), "ViewState from getViewState() is null in " + callback);
  }
}
 
Example #3
Source File: MvpConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Override public void postCreateView(@NonNull Controller controller, @NonNull View view) {

    MvpConductorDelegateCallback<V, P> callback = getCallback();

    P presenter = callback.getPresenter();
    if (presenter == null) {
      presenter = callback.createPresenter();
      if (presenter == null) {
        throw new NullPointerException(
            "Presenter returned from createPresenter() is null in " + callback);
      }
      callback.setPresenter(presenter);
    }

    V mvpView = callback.getMvpView();
    if (mvpView == null) {
      throw new NullPointerException("MVP View returned from getMvpView() is null in " + callback);
    }
    presenter.attachView(mvpView);
  }
 
Example #4
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void dontRestoreViewStateBecauseViewStateInMemory() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = PowerMockito.mock(Controller.class);
  View view = Mockito.mock(View.class);
  MvpView mvpView = Mockito.mock(MvpView.class);
  ViewState<MvpView> viewState = Mockito.mock(ViewState.class);

  MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

  MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getMvpView()).thenReturn(mvpView);
  Mockito.when(callback.getViewState()).thenReturn(viewState);

  Bundle bundle = new Bundle();

  lifecycleListener.changingConfigurations = true; // Simulate screen orientation change
  lifecycleListener.postCreateView(controller, view);
  lifecycleListener.onRestoreViewState(controller, bundle);

  Mockito.verify(callback, Mockito.never()).createViewState();
  Mockito.verify(viewState, Mockito.times(1)).apply(Mockito.eq(mvpView), Mockito.eq(true));
}
 
Example #5
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void createAndSetNewViewState() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = PowerMockito.mock(Controller.class);
  View view = Mockito.mock(View.class);
  MvpView mvpView = Mockito.mock(MvpView.class);
  ViewState<MvpView> viewState = Mockito.mock(ViewState.class);

  MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getMvpView()).thenReturn(mvpView);
  Mockito.when(callback.getViewState()).thenReturn(null);
  Mockito.when(callback.createViewState()).thenReturn(viewState);

  MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

  lifecycleListener.postCreateView(controller, view);
  lifecycleListener.preAttach(controller, view);

  Mockito.verify(callback, Mockito.times(1)).getViewState();
  Mockito.verify(callback, Mockito.times(1)).createViewState();
  Mockito.verify(callback, Mockito.times(1)).setViewState(viewState);
  Mockito.verify(callback, Mockito.times(1)).onNewViewStateInstance();
}
 
Example #6
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerOnCreateViewState() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    MvpView mvpView = Mockito.mock(MvpView.class);

    MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
        callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.getViewState()).thenReturn(null);
    Mockito.when(callback.createViewState()).thenReturn(null);

    MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    try {
      lifecycleListener.preAttach(controller, view);
      Assert.fail("NullPointerException expected");
    } catch (NullPointerException e) {
      Assert.assertEquals(e.getMessage(),
          "ViewState from createViewState() is null in " + callback);
    }
  }
 
Example #7
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void createAndSetNewPresenterAndAttachView() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = Mockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    MvpView mvpView = Mockito.mock(MvpView.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(null);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    lifecycleListener.postCreateView(controller, view);

    Mockito.verify(callback, Mockito.times(1)).getPresenter();
    Mockito.verify(callback, Mockito.times(1)).createPresenter();
    Mockito.verify(callback, Mockito.times(1)).setPresenter(presenter);
    Mockito.verify(presenter, Mockito.times(1)).attachView(mvpView);
  }
 
Example #8
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionOnCreatePresenter() {

    Controller controller = Mockito.mock(Controller.class);
    View view = Mockito.mock(View.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(null);
    Mockito.when(callback.createPresenter()).thenReturn(null);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    try {
      lifecycleListener.postCreateView(controller, view);
      Assert.fail("NullpointerException expected");
    } catch (NullPointerException e) {
      Assert.assertEquals(e.getMessage(),
          "Presenter returned from createPresenter() is null in " + callback);
    }
  }
 
Example #9
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionOnGetMvpView() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = Mockito.mock(Controller.class);
    View view = Mockito.mock(View.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(null);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    try {
      lifecycleListener.postCreateView(controller, view);
      Assert.fail("NullpointerException expected");
    } catch (NullPointerException e) {
      Assert.assertEquals(e.getMessage(),
          "MVP View returned from getMvpView() is null in " + callback);
    }
  }
 
Example #10
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void dontCreatePresenterIfOneExists() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = Mockito.mock(Controller.class);
  View view = Mockito.mock(View.class);
  MvpView mvpView = Mockito.mock(MvpView.class);
  MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
      Mockito.mock(MvpConductorDelegateCallback.class);

  MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
      new MvpConductorLifecycleListener<>(callback);

  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getMvpView()).thenReturn(mvpView);

  lifecycleListener.postCreateView(controller, view);

  Mockito.verify(callback, Mockito.times(1)).getPresenter();
  Mockito.verify(callback, Mockito.never()).createPresenter();
  Mockito.verify(callback, Mockito.never()).setPresenter(presenter);
}
 
Example #11
Source File: ConductorInjection.java    From conductor-dagger with Apache License 2.0 6 votes vote down vote up
private static HasControllerInjector findHasControllerInjector(Controller controller) {
  Controller parentController = controller;

  do {
    if ((parentController = parentController.getParentController()) == null) {
      Activity activity = controller.getActivity();
      if (activity instanceof HasControllerInjector) {
        return (HasControllerInjector) activity;
      }

      if (activity.getApplication() instanceof HasControllerInjector) {
        return (HasControllerInjector) activity.getApplication();
      }

      throw new IllegalArgumentException(
          String.format("No injector was found for %s", new Object[] { controller.getClass().getCanonicalName() }));
    }
  } while (!(parentController instanceof HasControllerInjector));

  return (HasControllerInjector) parentController;
}
 
Example #12
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionGetPresenter() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = Mockito.mock(Controller.class);
  View view = Mockito.mock(View.class);

  MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
      Mockito.mock(MvpConductorDelegateCallback.class);

  MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
      new MvpConductorLifecycleListener<>(callback);

  Mockito.when(callback.getPresenter()).thenReturn(null);

  try {
    lifecycleListener.preDestroyView(controller, view);
    Assert.fail();
  } catch (NullPointerException e) {
    Assert.assertEquals(e.getMessage(),
        "Presenter returned from getPresenter() is null in " + callback);
  }
}
 
Example #13
Source File: MainActivity.java    From service-tree with Apache License 2.0 6 votes vote down vote up
@Override
public void onChangeCompleted(@Nullable Controller to, @Nullable Controller from, boolean isPush, @NonNull ViewGroup container, @NonNull ControllerChangeHandler handler) {
    List<RouterTransaction> newState = router.getBackstack();
    Log.d("MainActivity",
            Arrays.toString(previousState.toArray()) + " :: " + Arrays.toString(newState.toArray()));
    for(RouterTransaction previousKey : previousState) {
        if(!newState.contains(previousKey)) {
            serviceTree.removeNodeAndChildren(serviceTree.getNode(previousKey));
            Log.d("MainActivity", "Destroying [" + previousKey + "]");
        }
    }
    for(RouterTransaction newKey : newState) {
        if(!serviceTree.hasNodeWithKey(newKey)) {
            ((BaseController) newKey.controller()).bindServices(serviceTree.createChildNode(serviceTree.getNode(
                    TAG), newKey));
            Log.d("MainActivity", "Bind service to [" + newKey + "]");
        }
    }
    previousState = router.getBackstack();
}
 
Example #14
Source File: MviConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Override public void postDestroy(@NonNull Controller controller) {
  if (DEBUG) {
    Log.d(DEBUG_TAG, "Presenter destroyed because View destroyed. MvpView "
            + callback.getMvpView()
            + "   Presenter: "
            + presenter);
  }
  presenter.destroy();
  presenter = null;
  callback = null;
}
 
Example #15
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void attachDetachViewFromPresenterNotRetaining() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    MvpView mvpView = Mockito.mock(MvpView.class);
    Activity activity = Mockito.mock(Activity.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    PowerMockito.when(controller.getActivity()).thenReturn(activity);
    Mockito.when(activity.isChangingConfigurations()).thenReturn(false);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    lifecycleListener.postCreateView(controller, view);

    // Attach
    Mockito.verify(presenter, Mockito.times(1)).attachView(mvpView);

    // Detach
    lifecycleListener.preDestroyView(controller, view);
    Mockito.verify(presenter, Mockito.times(1)).detachView();
  }
 
Example #16
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void attachDetachReattachViewFromPresenterRetaining() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    MvpView mvpView = Mockito.mock(MvpView.class);
    Activity activity = Mockito.mock(Activity.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    PowerMockito.when(controller.getActivity()).thenReturn(activity);
    Mockito.when(activity.isChangingConfigurations()).thenReturn(true);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    lifecycleListener.postCreateView(controller, view);

    // Attach
    Mockito.verify(presenter, Mockito.times(1)).attachView(mvpView);

    // Detach
    lifecycleListener.preDestroyView(controller, view);
    Mockito.verify(presenter, Mockito.times(1)).detachView();

    // Reattach
    lifecycleListener.postCreateView(controller, view);
    Mockito.verify(presenter, Mockito.times(2)).attachView(mvpView);
  }
 
Example #17
Source File: MvpConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Override
public void postDestroy(@NonNull Controller controller) {
  super.postDestroy(controller);
  P presenter = getCallback().getPresenter();
  if (presenter == null) {
    throw new NullPointerException(
            "Presenter returned from getPresenter() is null in " + callback);
  }
  presenter.destroy();
}
 
Example #18
Source File: MvpConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Override public void preDestroyView(@NonNull Controller controller, @NonNull View view) {
  P presenter = getCallback().getPresenter();
  if (presenter == null) {
    throw new NullPointerException(
        "Presenter returned from getPresenter() is null in " + callback);
  }
  presenter.detachView();
}
 
Example #19
Source File: MviConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Override public void preDestroyView(@NonNull Controller controller, @NonNull View view) {
  presenter.detachView();

  if (DEBUG) {
    Log.d(DEBUG_TAG, "detached MvpView from Presenter. MvpView "
        + callback.getMvpView()
        + "   Presenter: "
        + presenter);
  }
}
 
Example #20
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void dontSaveNotRestoreableViewState() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);
    Activity activity = Mockito.mock(Activity.class);

    RetainingLceViewState<Model, ModelView> viewState =
        Mockito.spy(new RetainingLceViewState<Model, ModelView>());

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.spy(new SemiFunctionalCallback(viewState));

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    PowerMockito.when(controller.getActivity()).thenReturn(activity);
    Mockito.when(activity.isChangingConfigurations()).thenReturn(false);

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);

    lifecycleListener.onSaveViewState(controller, bundle);
    lifecycleListener.preDestroyView(controller, view);

    Mockito.verifyZeroInteractions(bundle);
  }
 
Example #21
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void saveViewState() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);
    Activity activity = Mockito.mock(Activity.class);

    ParcelableDataLceViewState<Model, ModelView> viewState =
        Mockito.spy(new ParcelableDataLceViewState<Model, ModelView>());

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.spy(new SemiFunctionalCallback(viewState));

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    PowerMockito.when(controller.getActivity()).thenReturn(activity);
    Mockito.when(activity.isChangingConfigurations()).thenReturn(false);

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);

    lifecycleListener.onSaveViewState(controller, bundle);
    lifecycleListener.preDestroyView(controller, view);

    Mockito.verify(bundle, Mockito.times(1))
        .putParcelable(Mockito.eq(ParcelableDataLceViewState.KEY_BUNDLE_VIEW_STATE),
            Mockito.eq(viewState));
  }
 
Example #22
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void dontRestoreViewStateBecauseFirstTimeControllerStarts() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

    ParcelableDataLceViewState<Model, MvpLceView<Model>> viewState =
        Mockito.spy(new ParcelableDataLceViewState<Model, MvpLceView<Model>>());

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);

    lifecycleListener.postCreateView(controller, view);
    // lifecycleListener.onRestoreViewState(controller, bundle); Not called because first time starts
    lifecycleListener.preAttach(controller, view);

    Mockito.verify(viewState, Mockito.never()).apply(Mockito.eq(mvpView), Mockito.anyBoolean());
    Mockito.verify(callback, Mockito.times(1)).createViewState();
    Mockito.verify(callback, Mockito.times(1)).setViewState(Mockito.any(ViewState.class));
  }
 
Example #23
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void restoreViewStateFromBundle() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.spy(SemiFunctionalCallback.class);

    ParcelableDataLceViewState<Model, MvpLceView<Model>> viewState =
        Mockito.spy(new ParcelableDataLceViewState<Model, MvpLceView<Model>>());

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(bundle.getParcelable(AbsParcelableLceViewState.KEY_BUNDLE_VIEW_STATE))
        .thenReturn(viewState);
    viewState.saveInstanceState(bundle);

    lifecycleListener.postCreateView(controller, view);
    lifecycleListener.onRestoreViewState(controller, bundle);
    lifecycleListener.preAttach(controller, view);

    Mockito.verify(callback, Mockito.times(1)).setRestoringViewState(true);
    Mockito.verify(viewState, Mockito.times(1)).apply(Mockito.eq(mvpView), Mockito.eq(false));
    Mockito.verify(callback, Mockito.times(1)).setRestoringViewState(false);
    Mockito.verify(callback, Mockito.times(1)).onViewStateInstanceRestored(false);
  }
 
Example #24
Source File: MvpViewStateConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Override public void onSaveViewState(@NonNull Controller controller, @NonNull Bundle outState) {

    MvpViewStateConductorDelegateCallback<V, P, VS> vsCallback =
        ((MvpViewStateConductorDelegateCallback<V, P, VS>) callback);

    VS viewState = vsCallback.getViewState();
    if (viewState == null) {
      throw new NullPointerException("ViewState from getViewState() is null in " + callback);
    }

    if (viewState instanceof RestorableViewState) {
      ((RestorableViewState) viewState).saveInstanceState(outState);
    }
  }
 
Example #25
Source File: ConductorInjection.java    From conductor-dagger with Apache License 2.0 5 votes vote down vote up
public static void inject(Controller controller) {
  Preconditions.checkNotNull(controller, "controller");
  HasControllerInjector hasDispatchingControllerInjector = findHasControllerInjector(controller);
  Log.d("dagger.android", String.format("An injector for %s was found in %s", controller.getClass().getCanonicalName(),
      hasDispatchingControllerInjector.getClass().getCanonicalName()));
  DispatchingAndroidInjector<Controller> controllerInjector = hasDispatchingControllerInjector.controllerInjector();
  Preconditions.checkNotNull(controllerInjector, "%s.controllerInjector() returned null",
      hasDispatchingControllerInjector.getClass().getCanonicalName());
  controllerInjector.inject(controller);
}
 
Example #26
Source File: MvpViewStateConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 4 votes vote down vote up
@Override
public void onRestoreViewState(@NonNull Controller controller, @NonNull Bundle savedViewState) {

  MvpViewStateConductorDelegateCallback<V, P, VS> vsCallback =
      ((MvpViewStateConductorDelegateCallback<V, P, VS>) callback);

  VS viewState = vsCallback.getViewState();

  if (viewState == null) {

    viewState = vsCallback.createViewState();
    if (viewState == null) {
      throw new NullPointerException("ViewState from createViewState() is null in " + vsCallback);
    }

    if (viewState instanceof RestorableViewState) {

      // Try to restore from bundle.
      VS restoredViewState =
          (VS) ((RestorableViewState) viewState).restoreInstanceState(savedViewState);

      if (restoredViewState != null) {
        // Restored from bundle
        vsCallback.setViewState(restoredViewState);

        vsCallback.setRestoringViewState(true);
        restoredViewState.apply(callback.getMvpView(), false);
        vsCallback.setRestoringViewState(false);

        vsCallback.onViewStateInstanceRestored(false);
      }
    }
  } else {
    // ViewState in memory retained
    // TODO Conductor ViewRetainType
    vsCallback.setRestoringViewState(true);
    viewState.apply(callback.getMvpView(), changingConfigurations);
    vsCallback.setRestoringViewState(false);
    vsCallback.onViewStateInstanceRestored(changingConfigurations);
  }
}
 
Example #27
Source File: MvpViewStateConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 4 votes vote down vote up
@Override public void preDestroyView(@NonNull Controller controller, @NonNull View view) {
  super.preDestroyView(controller, view);
  changingConfigurations = controller.getActivity().isChangingConfigurations();
}
 
Example #28
Source File: CustomApplication.java    From conductor-dagger with Apache License 2.0 4 votes vote down vote up
@Override public DispatchingAndroidInjector<Controller> controllerInjector() {
  return dispatchingControllerInjector;
}
 
Example #29
Source File: MviConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 4 votes vote down vote up
@Override public void postCreateView(@NonNull Controller controller, @NonNull View view) {

    boolean viewStateWillBeRestored = false;

    if (presenter == null) {
      // Process death,
      // hence no presenter with the given viewState id stored, although we have a viewState id
      presenter = callback.createPresenter();
      if (DEBUG) {
        Log.d(DEBUG_TAG,
            "New Presenter instance created for " + controller + ". Presenter: " + presenter);
      }
    } else {
      viewStateWillBeRestored = true;
      if (DEBUG) {
        Log.d(DEBUG_TAG, "Reusing Presenter instance for controller "
            + controller
            + ". Presenter: "
            + presenter);
      }
    }

    V viewMpv = callback.getMvpView();
    if (viewMpv == null) {
      throw new NullPointerException(
          "MvpView returned from getMvpView() is null. Returned by " + controller.getActivity());
    }

    if (viewStateWillBeRestored) {
      callback.setRestoringViewState(true);
    }

    presenter.attachView(viewMpv);

    if (viewStateWillBeRestored) {
      callback.setRestoringViewState(false);
    }

    if (DEBUG) {
      Log.d(DEBUG_TAG,
          "MvpView attached to Presenter. MvpView: " + view + "   Presenter: " + presenter);
    }
  }
 
Example #30
Source File: MviConductorLifecycleListener.java    From mosby-conductor with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether or not a Presenter Instance should be kept
 *
 * @param keepPresenterInstance true, if the delegate has enabled keep
 */
static boolean retainPresenterInstance(boolean keepPresenterInstance, Controller controller) {

  return keepPresenterInstance && (controller.getActivity().isChangingConfigurations()
      || !controller.getActivity().isFinishing()) && !controller.isBeingDestroyed();
}