Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Model View Intent - An Introduction

Model View Intent - An Introduction

Jossi Wolf

August 16, 2017
Tweet

More Decks by Jossi Wolf

Other Decks in Programming

Transcript

  1. @Override public void render(SchoolsViewState state) { if (state.getError() != null)

    { showError(state.getError().getMessage()); } if (state.isLoading()) { showLoader(); clearAdapter(); } else if (!state.isLoading()) { hideLoader(); } if (state.getSchools() != null) { showSchools(state.getSchools()); } }
  2. public class ExampleStateModel { private boolean loading; private Throwable error;

    private List<SchoolsModel> data; ExampleStateModel(boolean loading, Throwable error, List<SchoolsModel> data) { // set everything } }
  3. public class ExampleStateModel { private boolean loading; private Throwable error;

    private List<SchoolsModel> data; private ExampleStateModel( boolean loading, Throwable error, List<SchoolsModel> data) { // set everything } }
  4. public static SchoolsViewState LoadingState() { return new SchoolsViewState(true, null, null);

    } public static SchoolsViewState ErrorState(Throwable error) { return new SchoolsViewState(false, error, null); } public static SchoolsViewState DataLoadedState(List<SchoolsModel> schools) { return new SchoolsViewState(false, null, schools); }
  5. HomeViewState { loading = false error = null data =

    [a, b, c] } HomeViewState { loading = false error = null data = [x, y, z] } [a, b, c, x, y, z]
  6. @Test public void loadSchoolsNotSuccessful() { Throwable throwable = new Throwable();

    when(api.getHamburgSchools()) .thenReturn(Observable.error(throwable)); schoolsPresenter.loadSchools(); verify(schoolsView).render(SchoolsViewState.LoadingState()); verify(schoolsView).render(SchoolsViewState.ErrorState(throwable)); }
  7. public MainViewState reduce(MainViewState previous, MainViewState next) { MainViewState computedViewState =

    MainViewState.Companion.ErrorState(new Throwable("No State")); if (next.getLoading()) { computedViewState = MainViewState.Companion.LoadingState(); } else if (next.getError() != null) { computedViewState = MainViewState.Companion.ErrorState(next.getError()); } else if (next.getData() != null) { List<String> data = new ArrayList<>(); if (previous.getData() != null) { data.addAll(previous.getData()); } data.addAll(next.getData()); computedViewState = MainViewState.Companion.DataLoadedState(data); } return computedViewState; }