Giter VIP home page Giter VIP logo

mosbymvp's Introduction

Getting Started with Mosby MVP

Build Status

My Medium article to accompany this repo is here: Get your MVP right with Mosby

This repo explores Mosby and Mosby Conductor plugin implementing MVP pattern.

Both Mosby and Conductor have excellent documentation on GitHub and at Hannes Dorfmann's blog.

Why use a library to implement MVP?

Model-View-Presenter (MVP) is hard to implement correctly (bug-free). Handling view state is another big challenge. There are many different implementations of MVP out there.

If there is a library that manages MVP wiring and view state for you so you can concentrate on your app logic and UI, why not check it out?

Definitions

  • ViewState is used to preserve current view data such as loading in progress, data displayed, error displayed, etc. after configuration changes such as device rotation. More details at ViewState
  • LCE describes Loading-Content-Error flow typical of many Android apps where loading indicator (such as ProgressBar) is followed by displaying content or error.
  • Delegate Callbacks is a set of interfaces that Mosby uses internally which you are welcome to implement yourself instead of inheriting from base MVP classes. For instance, instead of extending MvpActivity, implement MvpDelegateCallback or instead of extending MvpViewStateController implement MvpViewStateConductorDelegateCallback and so forth.
  • MvpNullObjectBasePresenter is a special kind of Presenter which implements the Null Object Pattern. It makes it unnecessary for you to check if the View is attached prior to calling methods on it. Any calls on the View that is not attached will do nothing (no op); no NullPointerExceptions will be thrown.
  • MvpQueuingBasePresenter is a special kind of Presenter which lets you queue the calls on the View and replay them in same order once the View becomes available.

Modules in this repo

All examples implement MVP pattern

  • Hello World
    • Simple Hello World sample. [link]
  • Hello World with ViewState
    • Hello World with ViewState preserved across configuration changes. [link]
  • LCE pull-to-refresh with ViewState
    • Todo-style notes screen using RecyclerView with ViewState preserved across configuration changes. [link]
  • Mosby Conductor Hello World
    • Mosby with Conductor plugin sample. [link]
  • Mosby Conductor Hello World with ViewState
    • Mosby with Conductor plugin with ViewState preserved across configuration changes. [link]
  • Mosby Conductor LCE pull-to-refresh with ViewState
    • Mosby with Conductor plugin LCE pull-to-refresh with ViewState preserved across configuration changes. [link]
  • Mosby Conductor Dagger Sample
    • Mosby with Conductor plugin used in a small Clean Architecture sample app with Dagger and Retrofit. Uses custom Dagger scopes. [link]

License

Copyright 2017 James Shvarts

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

mosbymvp's People

Contributors

jshvarts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mosbymvp's Issues

Not restoring view state correctly

In GreetingViewController you should implement onViewStateInstanceRestored(boolean instanceStateRetained).

Following scenario:
Lets say a user clicks on the greeting button, then presenter. loadGreeting() is invoked. Presenter calls view. showLoading() (I think this method should contain a call like viewState.setShowLoading()). So your app is in LOADING state.
Let's assume that loading the greeting takes quite some time and your app is killed (process death). Since you use a RestorableViewState the state will be saved into Bundle and restored from it afterwards.

Once the user restarts your app, ViewState will be restored (from Bundle) and ProgressBar is shown again on screen.

BUT you actually never invoke presenter. loadGreeting() again, so LOADING state will last forever.

Possible fix:
Don't use a RestoreableViewState
OR
implement onViewStateInstanceRestored() properly

override fun onViewStateInstanceRestored(instanceStateRetained : boolean) {
    if (!instanceStateRetained) {  // is false on process death --> state restored from bundle
        if (viewState.isLoading())
           presenter.loadGreeting() // restart loading process after process death
   }
}

I think in 95% of the cases it is ok to no use a RestoreableViewState but rather reload the entire view on process death as it would be the first time the view is displayed. To do that just don't use a RestoreableViewState instance but rather any other ViewState (will only be kept im memory i.e. for screen orientation changes, but not persisted in bundle).

If that is not suitable for you, you should implement onViewStateInstanceResored() properly to bring your presenter back in the correct state i.e. by calling presenter.loadGreeting()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.