Search in sources :

Example 6 with ViewState

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

the class ActivityMvpViewStateDelegateImplTestNew method appStartWithViewStateFromMemoryAndBundleButPreferViewStateFromMemory.

@Test
public void appStartWithViewStateFromMemoryAndBundleButPreferViewStateFromMemory() {
    ActivityMvpViewStateDelegateImpl<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>> delegate = new ActivityMvpViewStateDelegateImpl<>(activity, callback, true);
    Mockito.doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            viewState = Mockito.spy(new SimpleRestorableViewState());
            return viewState;
        }
    }).when(callback).createViewState();
    startActivity(delegate, null, 1, 1, 1, 1, 1, 0, null, 0, 1, 0);
    Bundle bundle = BundleMocker.create();
    finishActivity(delegate, bundle, true, 1, true, false);
    startActivity(delegate, bundle, 1, 2, 2, 1, 2, 1, true, 1, 1, 1);
    finishActivity(delegate, bundle, false, 1, false, true);
}
Also used : Answer(org.mockito.stubbing.Answer) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Bundle(android.os.Bundle) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Test(org.junit.Test)

Example 7 with ViewState

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

the class ViewGroupMvpViewStateDelegateImplTest method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    viewState = Mockito.mock(ViewState.class);
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.mock(PartialViewGroupMvpViewStateDelegateCallbackImpl.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.doCallRealMethod().when(callback).setViewState(viewState);
    Mockito.doCallRealMethod().when(callback).getViewState();
    activity = Mockito.mock(FragmentActivity.class);
    application = Mockito.mock(Application.class);
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(callback.getContext()).thenReturn(activity);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
    Mockito.when(callback.createViewState()).thenReturn(viewState);
    delegate = new ViewGroupMvpViewStateDelegateImpl<>(callback, true);
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Application(android.app.Application) Before(org.junit.Before)

Example 8 with ViewState

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

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    unbinder = ButterKnife.bind(this);
    toolbar.setTitle("Mosby MVI");
    toolbar.inflateMenu(R.menu.activity_main_toolbar);
    toolbar.setOnMenuItemClickListener(item -> {
        getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out).add(R.id.drawerLayout, new SearchFragment()).addToBackStack("Search").commit();
        return true;
    });
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    if (savedInstanceState == null) {
        showCategoryItems(MainMenuItem.HOME);
    } else {
        title = savedInstanceState.getString(KEY_TOOLBAR_TITLE);
        toolbar.setTitle(title);
    }
    // TODO Create a Presenter & ViewState for this Activity
    DependencyInjection dependencyInjection = SampleApplication.getDependencyInjection(this);
    disposable = dependencyInjection.getMainMenuPresenter().getViewStateObservable().filter(state -> state instanceof MenuViewState.DataState).cast(MenuViewState.DataState.class).map(this::findSelectedMenuItem).subscribe(this::showCategoryItems);
    clearSelectionRelay = dependencyInjection.getClearSelectionRelay();
}
Also used : MenuViewState(com.hannesdorfmann.mosby3.sample.mvi.view.menu.MenuViewState) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) SearchFragment(com.hannesdorfmann.mosby3.sample.mvi.view.search.SearchFragment) DependencyInjection(com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection)

Example 9 with ViewState

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

the class AuthFragment method showAuthenticationRequired.

@Override
public void showAuthenticationRequired() {
    AuthViewState vs = (AuthViewState) viewState;
    vs.setShowingAuthenticationRequired();
    loadingView.setVisibility(View.GONE);
    errorView.setVisibility(View.GONE);
    contentView.setVisibility(View.GONE);
    authView.setVisibility(View.VISIBLE);
}
Also used : AuthViewState(com.hannesdorfmann.mosby3.sample.mail.base.view.viewstate.AuthViewState)

Example 10 with ViewState

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

the class FragmentMvpViewStateDelegateImpl method onViewCreated.

@Override
public void onViewCreated(View view, Bundle bundle) {
    super.onViewCreated(view, bundle);
    if (bundle != null && keepPresenterInstanceDuringScreenOrientationChanges) {
        mosbyViewId = bundle.getString(KEY_MOSBY_VIEW_ID);
        if (DEBUG) {
            Log.d(DEBUG_TAG, "MosbyView ID = " + mosbyViewId + " for MvpView: " + delegateCallback.getMvpView());
        }
    }
    if (mosbyViewId != null) {
        VS viewState = PresenterManager.getViewState(fragment.getActivity(), mosbyViewId);
        if (viewState != null) {
            //
            // ViewState restored from PresenterManager
            //
            delegateCallback.setViewState(viewState);
            applyViewState = true;
            applyViewStateFromMemory = 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;
            delegateCallback.setViewState(viewState);
            applyViewState = true;
            applyViewStateFromMemory = false;
            if (keepPresenterInstanceDuringScreenOrientationChanges) {
                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(fragment.getActivity(), mosbyViewId, viewState);
            }
            if (DEBUG) {
                Log.d(DEBUG_TAG, "Recreated ViewState from bundle for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    //
    if (keepPresenterInstanceDuringScreenOrientationChanges) {
        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(fragment.getActivity(), mosbyViewId, viewState);
    }
    delegateCallback.setViewState(viewState);
    applyViewState = false;
    applyViewStateFromMemory = false;
    if (DEBUG) {
        Log.d(DEBUG_TAG, "Created a new ViewState instance for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
    }
}
Also used : RestorableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)

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