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);
}
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);
}
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();
}
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);
}
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);
}
}
Aggregations