Search in sources :

Example 1 with ViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.ViewState in project mosby by sockeqwe.

the class ActivityMvpViewStateDelegateImpl method onPostCreate.

@Override
public void onPostCreate(Bundle bundle) {
    super.onPostCreate(bundle);
    if (mosbyViewId != null) {
        VS viewState = PresenterManager.getViewState(activity, mosbyViewId);
        if (viewState != null) {
            //
            // ViewState restored from PresenterManager
            //
            setViewState(viewState, true, true);
            if (DEBUG) {
                Log.d(DEBUG_TAG, "ViewState reused from Mosby internal cache for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    VS viewState = delegateCallback.createViewState();
    if (viewState == null) {
        throw new NullPointerException("ViewState returned from createViewState() is null! MvpView that has returned null as ViewState is: " + delegateCallback.getMvpView());
    }
    if (bundle != null && viewState instanceof RestorableViewState) {
        // A little bit hacky that we need an instance of the viewstate to restore a view state
        // (may creates another view state object) but I don't know any better way :)
        RestorableViewState restoredViewState = ((RestorableViewState) viewState).restoreInstanceState(bundle);
        if (restoredViewState != null) {
            //
            // ViewState restored from bundle
            //
            viewState = (VS) restoredViewState;
            setViewState(viewState, true, false);
            if (keepPresenterInstance) {
                if (mosbyViewId == null) {
                    throw new IllegalStateException("The (internal) Mosby View id is null although bundle is not null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
                }
                PresenterManager.putViewState(activity, mosbyViewId, viewState);
            }
            if (DEBUG) {
                Log.d(DEBUG_TAG, "Recreated ViewState from bundle for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    //
    if (keepPresenterInstance) {
        if (mosbyViewId == null) {
            throw new IllegalStateException("The (internal) Mosby View id is null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
        }
        PresenterManager.putViewState(activity, mosbyViewId, viewState);
    }
    setViewState(viewState, false, false);
    if (DEBUG) {
        Log.d(DEBUG_TAG, "Created a new ViewState instance for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
    }
    delegateCallback.onNewViewStateInstance();
}
Also used : RestorableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)

Example 2 with ViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.ViewState in project mosby by sockeqwe.

the class ViewGroupMvpViewStateDelegateImpl method onSaveInstanceState.

/**
   * Must be called from {@link View#onSaveInstanceState()}
   */
@Override
public Parcelable onSaveInstanceState() {
    VS viewState = delegateCallback.getViewState();
    if (viewState == null) {
        throw new NullPointerException("ViewState returned from getViewState() is null for MvpView " + delegateCallback.getMvpView());
    }
    Parcelable superState = delegateCallback.superOnSaveInstanceState();
    if (keepPresenter) {
        if (viewState instanceof RestorableParcelableViewState) {
            return new MosbyViewStateSavedState(superState, mosbyViewId, (RestorableParcelableViewState) viewState);
        }
        return new MosbyViewStateSavedState(superState, mosbyViewId, null);
    } else {
        return superState;
    }
}
Also used : RestorableParcelableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableParcelableViewState) Parcelable(android.os.Parcelable)

Example 3 with ViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.ViewState in project mosby by sockeqwe.

the class ActivityMvpViewStateDelegateImplTestNew method dontKeepPresenterAndViewState.

@Test
public void dontKeepPresenterAndViewState() {
    ActivityMvpViewStateDelegateImpl<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>> delegate = new ActivityMvpViewStateDelegateImpl<>(activity, callback, false);
    startActivity(delegate, null, 1, 1, 1, 1, 1, 0, null, 0, 1, 0);
    Bundle bundle = BundleMocker.create();
    finishActivity(delegate, bundle, false, 1, true, false);
    startActivity(delegate, bundle, 2, 2, 2, 2, 2, 0, null, 0, 2, 0);
    finishActivity(delegate, bundle, false, 2, false, true);
}
Also used : MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) Bundle(android.os.Bundle) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Test(org.junit.Test)

Example 4 with ViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.ViewState in project mosby by sockeqwe.

the class ActivityMvpViewStateDelegateImplTestNew method appStartFinishing.

@Test
public void appStartFinishing() {
    ActivityMvpViewStateDelegateImpl<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>> delegate = new ActivityMvpViewStateDelegateImpl<>(activity, callback, true);
    startActivity(delegate, null, 1, 1, 1, 1, 1, 0, null, 0, 1, 0);
    Bundle bundle = BundleMocker.create();
    finishActivity(delegate, bundle, false, 1, false, true);
}
Also used : MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) Bundle(android.os.Bundle) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Test(org.junit.Test)

Example 5 with ViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.ViewState in project mosby by sockeqwe.

the class ActivityMvpViewStateDelegateImplTestNew method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    viewState = Mockito.mock(ViewState.class);
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.spy(PartialMvpViewStateDelegateCallbackImpl.class);
    activity = Mockito.mock(Activity.class);
    application = Mockito.mock(Application.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.doCallRealMethod().when(callback).setViewState(viewState);
    Mockito.doCallRealMethod().when(callback).getViewState();
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
    Mockito.when(callback.createViewState()).thenReturn(viewState);
}
Also used : MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) Activity(android.app.Activity) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Application(android.app.Application) Before(org.junit.Before)

Aggregations

MvpPresenter (com.hannesdorfmann.mosby3.mvp.MvpPresenter)8 MvpView (com.hannesdorfmann.mosby3.mvp.MvpView)8 ViewState (com.hannesdorfmann.mosby3.mvp.viewstate.ViewState)8 Bundle (android.os.Bundle)5 Test (org.junit.Test)5 Application (android.app.Application)3 Before (org.junit.Before)3 FragmentActivity (android.support.v4.app.FragmentActivity)2 RestorableViewState (com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 Activity (android.app.Activity)1 Parcelable (android.os.Parcelable)1 Fragment (android.support.v4.app.Fragment)1 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 RestorableParcelableViewState (com.hannesdorfmann.mosby3.mvp.viewstate.RestorableParcelableViewState)1 AuthViewState (com.hannesdorfmann.mosby3.sample.mail.base.view.viewstate.AuthViewState)1 DependencyInjection (com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection)1 MenuViewState (com.hannesdorfmann.mosby3.sample.mvi.view.menu.MenuViewState)1 SearchFragment (com.hannesdorfmann.mosby3.sample.mvi.view.search.SearchFragment)1