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

[Mateusz Herych] The ultimate guide to MVP patt...

[Mateusz Herych] The ultimate guide to MVP pattern on Android

Presentation from GDG DevFest Ukraine 2016.
Learn more at: https://devfest.gdg.org.ua

Google Developers Group Lviv

September 10, 2016
Tweet

More Decks by Google Developers Group Lviv

Other Decks in Technology

Transcript

  1. #dfua The one random team’s guide to MVP on Android

    that may work for your project or not.
  2. #dfua Hey, I am Mateusz Mateusz Herych Android guy @

    IG Google Developer Expert Android Google Developers Group Kraków co-org
  3. #dfua Why - Many classes, interfaces - I have a

    small freelance project, won’t help me much
  4. #dfua Why - Many classes, interfaces - I have a

    small freelance project, won’t help me much - Won’t make my life any easier
  5. #dfua Why - Huge Activities and Fragments are hard to

    test - Huge Activities and Fragments are impossible to unit test
  6. #dfua Why - Huge Activities and Fragments are hard to

    test - Huge Activities and Fragments are impossible to unit test - People start to not test anything at all, because vast amount of logic belongs to Activities / Fragments anyways
  7. #dfua Why - Huge Activities and Fragments are hard to

    test - Huge Activities and Fragments are impossible to unit test - People start to not test anything at all, because vast amount of logic belongs to Activities / Fragments anyways - Multiple responsibilities - same class is doing complex animations and converting data from DB / REST format to the Adapter
  8. #dfua Why we don’t use it? - Fact (positive one):

    Mosby is a library, not a framework
  9. #dfua Why we don’t use it? - Fact (positive one):

    Mosby is a library, not a framework - Still, it influences your design a little bit, we wanted to avoid that
  10. #dfua Why we don’t use it? - Fact (positive one):

    Mosby is a library, not a framework - Still, it influences your design a little bit, we wanted to avoid that - Some problems that already have ready solutions in Mosby, we wanted to solve in a different way (like retaining presenters)
  11. #dfua Stuff we use RxJava Dagger2 Obviously, tons of other

    libraries as well, however they’re not influencing our MVP.
  12. #dfua View As dumb as possible View delegates user’s behavior

    to Presenter operating on raw data - like row ids in terms of clicking on a List
  13. #dfua View As dumb as possible View delegates user’s behavior

    to Presenter operating on raw data - like row ids in terms of clicking on a List View is responsible for performing Animations
  14. #dfua View As dumb as possible View delegates user’s behavior

    to Presenter operating on raw data - like row ids in terms of clicking on a List View is responsible for performing Animations Implements an interface
  15. #dfua View As dumb as possible View delegates user’s behavior

    to Presenter operating on raw data - like row ids in terms of clicking on a List View is responsible for performing Animations Implements an interface Activity / Fragment / Custom View
  16. #dfua Presenter - Glue code between View and the rest

    of the code. - Converts Model’s data to be easily displayed on a View.
  17. #dfua Presenter - Glue code between View and the rest

    of the code. - Converts Model’s data to be easily displayed on a View. - Is retained between configuration changes, like orientation changes (more about that few slides further!)
  18. #dfua Presenter - Glue code between View and the rest

    of the code. - Converts Model’s data to be easily displayed on a View. - Is retained between configuration changes, like orientation changes (more about that few slides further!) - In a nutshell, everything that stayed in a Fragment in our non-MVP architecture, but wasn’t strictly UI related.
  19. #dfua Presenter - Glue code between View and the rest

    of the code. - Converts Model’s data to be easily displayed on a View. - Is retained between configuration changes, like orientation changes (more about that few slides further!) - In a nutshell, everything that stayed in a Fragment in our non-MVP architecture, but wasn’t strictly UI related. - Doesn’t implement an interface
  20. #dfua Interfaces for presenters - We use Dagger. - Using

    interfaces will force you to explicitly map your Interfaces to Concrete implementation
  21. #dfua Interfaces for presenters - We use Dagger. - Using

    interfaces will force you to explicitly map your Interfaces to Concrete implementation - It doesn’t change anything from the unit tests perspective - you can still stub your concrete classes
  22. #dfua Interfaces for presenters - We use Dagger. - Using

    interfaces will force you to explicitly map your Interfaces to Concrete implementation - It doesn’t change anything from the unit tests perspective - you can still stub your concrete classes https://medium.com/@mherych/do-i-need-to-create-interfaces-for-my-pres enters-9abe0897f873#.896w7lyt7
  23. #dfua Presenters vs Android SDK - Some people claim that

    Presenters shouldn’t be using Android components.
  24. #dfua Presenters vs Android SDK - Some people claim that

    Presenters shouldn’t be using Android components. - We do not.
  25. #dfua Presenters vs Android SDK - Some people claim that

    Presenters shouldn’t be using Android components. - We do not. - Sometimes you need to delegate lifecycle methods (onPause(), onStart(), …)
  26. #dfua Presenters vs Android SDK - Some people claim that

    Presenters shouldn’t be using Android components. - We do not. - Sometimes you need to delegate lifecycle methods (onPause(), onStart(), …) - Presenter is in fact still a presentation layer
  27. #dfua Presenters vs Android SDK - Some people claim that

    Presenters shouldn’t be using Android components. - We do not. - Sometimes you need to delegate lifecycle methods (onPause(), onStart(), …) - Presenter is in fact still a presentation layer - No real benefits (?)
  28. #dfua Model - Business data gathered from an Application/data layer

    - The REAL stuff happens here - Not related to Android, no Android SDK is being used
  29. #dfua Model - Business data gathered from an Application/data layer

    - The REAL stuff happens here - Not related to Android, no Android SDK is being used - Basically it starts with some interface that reflects some business process...
  30. #dfua Model - Business data gathered from an Application/data layer

    - The REAL stuff happens here - Not related to Android, no Android SDK is being used - Basically it starts with some interface that reflects some business process… - … and then consists from various classes that are splitted by their responsibilities
  31. #dfua Model - Business data gathered from an Application/data layer

    - The REAL stuff happens here - Not related to Android, no Android SDK is being used - Basically it starts with some interface that reflects some business process… - … and then consists from various classes that are splitted by their responsibilities (db repositories, retrofit services, actual business logic, etc.)
  32. #dfua Dagger Scopes Scopes are local singletons (local in a

    single component/subcomponent) One default scope in Dagger2: @Singleton
  33. #dfua Dagger Scopes AppComponent - @AppScope SessionComponent - @SessionScope (logout/switch

    environment is basically recreation of this component) NonConfigurationComponent
  34. #dfua Dagger Scopes AppComponent - @AppScope SessionComponent - @SessionScope (logout/switch

    environment is basically recreation of this component) NonConfigurationComponent ActivityComponent
  35. #dfua getLastNonConfigurationScope() Deprecated for years - documentation tells you to

    use retained Fragments (meh) Deprected so you won’t break LoaderManager and FragmentManager, not because it’ll be removed anytime soon
  36. #dfua getLastNonConfigurationScope() Deprecated for years - documentation tells you to

    use retained Fragments (meh) Deprected so you won’t break LoaderManager and FragmentManager, not because it’ll be removed anytime soon Yep, we use the exact same mechanism Android is using for retaining FragmentManager and LoaderManager
  37. #dfua getLastNonConfigurationScope() Deprecated for years - documentation tells you to

    use retained Fragments (meh) Deprected so you won’t break LoaderManager and FragmentManager, not because it’ll be removed anytime soon Yep, we use the exact same mechanism Android is using for retaining FragmentManager and LoaderManager Finally, getLastCustomNonConfiguration(), not deprecated, in AppCompatActivity.