Explains when you should use ThirtyInch and when you should use MVP at all. It also covers the different kinds of MVP, statless and stateful.
The code sample focuses on MVVM, or as I call it VPVM (view presenter viewmodel).
to a Presenter makes the code testable ◦ Only self written code ◦ Easy lifecycle, view attached/detached ◦ Unit tests on JVM Activities and Fragments are hard to test ◦ Have final methods and classes ◦ Constructor dependency injection not possible ◦ Requires emulator to run androidTest tests (slow) ◦ The lifecycle is hard to mock and too complex
in Activity#onCreate Doesn’t solve the problem or network requests when the phone is changing orientation Hold state (model) in the Activity and save is in onSaveInstanceState() Stateful Presenter survives orientation changes Network requests keep running when the phone is changing orientation Hold state objects (model) in the Presenter
saved in the Presenter (in memory) instead of using Parcelables Ignores all lifecycle events except for the availability of the View Network requests can run during a orientation change and update the model, not the view Implementing a stateful Presenter works best with a ViewModel and a passive view (VPVM) Living next to the Activity
a reference to the class under test final EditTaskPresenter presenter = new EditTaskPresenter("1", mTasksRepository); final TiPresenterInstructor<EditTaskView> instructor = new TiPresenterInstructor<>(presenter); // When task is requested and view is attached final AddEditTaskView view = mock(AddEditTaskView.class); instructor.attachView(view);
ArgumentCaptor.forClass(TasksDataSource.GetTaskCallback.class); verify(mTasksRepository).getTask(eq("1"), captor.capture()); // Simulate callback final Task testTask = new Task("TITLE", "DESCRIPTION"); captor.getValue().onTaskLoaded(testTask); // Then the task repository is queried and the view updated verify(view).showLoadingIndicator(false); verify(view, atLeastOnce()).setTitle(testTask.getTitle()); }
than your homemade one; Stateless or stateful • Grown and stabilized over the last 1.5 years in multiple projects • Gives you a stateful Presenter for Activities and Fragments which survives configuration changes Try it now!