Android Storage by Dmitry Melnikov
+ - 0:00:00
Notes for current slide
Notes for next slide

Android UI Architecture Patterns

Dmitry Melnikov

1 / 34

Logical layers

UI

  • render
  • ui interactions (no handling)

Application

  • control flow
  • handles ui interactions

Domain

  • business logic

Infrastructure

  • rest from domain layer (network, storage)
3 / 34

MVC/MVP/MVVM/MVI…

Model: state and/or business logic and/or data structures

View: user interface

X: Business logic and/or flow control logic and/or state and/or data structures

4 / 34

Android Architecture Blueprints

aab.png

MVP official sample code review

git clone git@github.com:android/architecture-samples.git
git checkout todo-mvp
5 / 34

Android Architecture Blueprints

ಠ_ಠ

Too verbose View interface

View is a fragment

View calls Presenter! (from README.md view should be passive)

Presenter void result(int requestCode, int resultCode);

View is difficult to replace with alternative implementation

View is not reusable

View and Presenter tightly coupled

View has non-UI logic

=> MVP Architecture Blueprint is not MVP

6 / 34

History

img

7 / 34

History

arch-03.jpg

8 / 34

Thought 💡

Activity not a View but more a Presenter/Controller

11 / 34

MVC/MVP/MVVM/MVI… benefits

12 / 34

MVC/MVP/MVVM/MVI… benefits

Testability... ?

12 / 34

MVC/MVP/MVVM/MVI… benefits

Testability... ?

No need for it if you don't unit test?

Better testability is just a byproduct.

12 / 34

MVC/MVP/MVVM/MVI… benefits

Testability... ?

No need for it if you don't unit test?

Better testability is just a byproduct.

The main benefit of MVx is a decoupled UI logic.

All MVx implementations have one common thing: UI logic in views.

12 / 34

UI logic

Responsibilities

  • render system output
  • capture user interactions with the UI and route them into the system.

Characteristics

  • detailed and accurate requirements (UI mockups)
  • much higher rate of change in most cases
  • unreadable (verbose, messy, hacky, etc)
  • easiest to test manually
  • hardest to test automatically
13 / 34

MVC/MVP/MVVM/MVI… benefits

Decoupled UI logic is the main benefit

14 / 34

Activity/Fragment & UI logic

Example: outsourcing

UI specs + decoupled logic => Outsourcing company => MyActivity implements MyView

What about

  • life-cycle
  • screen navigation
  • runtime permissions
  • loaders
  • fragments
  • dialogs
  • di
  • more...

Activity is a God Object with lots of responsibilities.

15 / 34

Activity/Fragment & UI logic

It's difficult to integrate outsourced activity to an app. Activity is a God Object with many responsibilities.

It's almost impossible to decouple UI logic inside Activity.

Fragments are the same.

Extract UI logic to standalone class.

MyViewMvpImpl implements MyViewMvp. Obviously Activity is P.

Composition of several MyViewImpl.

16 / 34

Activity/Fragment & UI logic

+---------------+
| Interface |
| MyViewMvp | <----------------
+-------|-------+ |
| |
+---------------+ +---------------+
| | | MyViewMvpImpl |
| | | +-----------+ |
| Activity | | | | |
| | -------> | | UI logic | |
| | | | | |
| | | +-----------+ |
+---------------+ +---------------+
17 / 34

MVx approach

Letters don't matter

MVC wiki

mvc

18 / 34

MVx approach

mvp

19 / 34

MVx approach

MVVM wiki

mvvm

20 / 34

My approach

2010 -> 2012 -> 2014 -> 2018

My approach is a variation of MVP

Droidcon NYC 2018 - Netflix's componentization architecture with RxJava + Kotlin

21 / 34

MVP Sample

git clone git@github.com:melnikovdv/android-arch-2.git

App shows blog posts fetched from local cache or from remote server if not cached yet.

Better way is to implement everything the "bad way" and extract UI-logic in a real-time manner, but we have no time for it.

  • blog item screen
  • blog items screen
22 / 34

MVP Sample ideas

Single Activity

UI logic and business logic are separated

Separated MvpView helps to understand what view does

ListView + performance workaround with ViewHolder => RecyclerView


Application layer: Fragment

Decoupling layer: MvpView & Listener interfaces

UI Layer: MvpViewImpl

All the interactions in activity and fragments are high level and don't depend on implementation details.

It's a good abstraction when you can refactor without affecting dependent classes.

23 / 34

Why not Android View as MvpView

Why not use Android View class as MvpView?

Android views are not the best candidates for MvpView implementation.

Imagine inheritance level:

BlogItemMvpView -> MvpViewObservable -> MvpView -> FrameLayout -> ViewGroup -> View

Cons:

  • inheritance of thousands of loc
  • inheritance of 100+ methods
  • standalone hierarchy for each ViewGroup

Effective Java, item 16: "Favour composition over inheritance".

24 / 34

MVP Sample summary

MVP views implement the MvpView interface with single rootView.

UI-actions calls corresponding presenters via listeners (observable).

MVP views can be nested.

Activities and fragments are not views, but presenters/controllers.

Android views aren't the best choice for MvpView.

25 / 34

MVVM

MVVM official sample code review

git clone git@github.com:android/architecture-samples.git

Fragments have a mix of ui logic and non-ui logic (e.g., navigation)

Sunflower app

26 / 34

MVVM sample

git clone git@github.com:melnikovdv/android-arch-2.git
git checkout 101d2e7

With Data Binding

git clone git@github.com:melnikovdv/android-arch-2.git
git checkout 433670f
27 / 34

MVVM summary

Pros

  • less code, especially with data bindings
  • quick start (no need in custom code)

Cons

  • heavily dependant on the framework (MPP)
  • emphasizes usage of viewModels/liveData/fragments
  • usually Activity/Fragment as a View
  • views are difficult to reuse
  • nested views are difficult
28 / 34

MVVM summary

They: tell me your wildest sexual fantasy!

Android Dev: 👇

img

29 / 34

MVI

MVVM + immutable state

MVP/MVVM FSM vs MVI state

30 / 34

MVC/MVP/MVVM/MVI… summary

Decoupled UI logic

Reuse of UI and application layer logic

Testability

No additional dependencies

Easy integration with dependency injection

Standardization of the code

31 / 34

MVC/MVP/MVVM/MVI… summary

Cons

Initial learning curve

Unfamiliar

A bit more code

img

32 / 34

MVC/MVP/MVVM/MVI… further reading

Android vision guide

Sunflower app

Vasily Zukanov blog

Hannes Dorfmann's blog

Martin Fowler website

Robert Martin blog + «Clean Code»

33 / 34

The end

Dmitry Melnikov

melnikovdv@gmail.com

t.me/melnikovdv

34 / 34
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow