1 of 72

2 of 72

Eduardo Medina

@eduardomedina | emedinaa@gmail.com | github/emedinaa

Android SME Belatrix / Globant

Mapsalud�Instructor en Academia Móviles

Android Dev Perú

Kotlin User Group Perú

IOS Dev Perú�GDG Lima

3 of 72

MVVM Pattern

Model View ViewModel

4 of 72

MVC Model View Controller

MVP Model View Presenter

Clean Architecture

MVVM Model View ViewModel

Architecture Patterns & Architectures

5 of 72

https://github.com/emedinaa/kotlin-mvvm

6 of 72

  • mvvm + tests �https://github.com/emedinaa/kotlin-mvvm/tree/master
  • mvvm + koin + tests

https://github.com/emedinaa/kotlin-mvvm/tree/koin

  • mvvm + coroutines + sealed class + tests

https://github.com/emedinaa/kotlin-mvvm/tree/coroutines

  • mvvm + view binding�https://github.com/emedinaa/kotlin-mvvm/tree/viewbinding
  • mvvm + coroutines + room �https://github.com/emedinaa/kotlin-mvvm/tree/room

Kotlin mvvm samples

7 of 72

  • mvvm + flow + tests �https://github.com/emedinaa/kotlin-mvvm/tree/flow
  • mvvm + hilt + tests

https://github.com/emedinaa/kotlin-mvvm/tree/hilt

  • mvvm + clean

https://github.com/emedinaa/kotlin-mvvm/tree/clean

  • mvvm + rx (in progress)
  • mvvm + navigation component (in progress)

Kotlin mvvm samples

8 of 72

https://github.com/emedinaa/kotlin-mvvm

9 of 72

UI

10 of 72

Retrofit

11 of 72

VIEW - DATA

channel

12 of 72

DataSource

13 of 72

DataSource

14 of 72

View - DataSource

15 of 72

UI

16 of 72

View - Repository - DataSource

Callback

Callback

17 of 72

Repository

18 of 72

UI

19 of 72

View - ?? - Repository - DataSource

Callback

Callback

20 of 72

MVVM - Model View ViewModel

Callbacks

Property changed events

UI events

21 of 72

MVVM - Model View ViewModel

Callbacks

Property changed events

UI events

22 of 72

View

Observers

UI events

LiveData

Property changed events

23 of 72

LiveData & Lifecycle

24 of 72

LiveData & Lifecycle

25 of 72

UI

26 of 72

ViewModel

ViewModel provides data for UI components and survive configuration changes.

27 of 72

View actions

ViewModel

showLoading()

isViewLoading�<Boolean>

hideLoading()

isViewLoading

<Boolean>

errorMessage()

onErrorMessage�<Any>

emptyData()

isEmptyList

<Boolean>

renderMuseums()

museums List<Museum>>

28 of 72

View - ViewModel - Model (Repository | DataSource)

Callback

Callback

Callbacks

Callbacks

LiveData

29 of 72

ViewModel

30 of 72

ViewModel

31 of 72

UI

32 of 72

Architecture

https://developer.android.com/jetpack/docs/guide

33 of 72

Unit Tests

34 of 72

  • Local tests
  • Instrumentation tests

Unit tests

JUnit + Mockito

35 of 72

Test

36 of 72

Test

37 of 72

Test

38 of 72

Test

39 of 72

Test

40 of 72

Test

41 of 72

Layer

communication

42 of 72

MVVM - Model View ViewModel

Callbacks

Property changed events

UI events

43 of 72

Sealed

classes

Operation Result

Success

Error

https://kotlinlang.org/docs/reference/sealed-classes.html

44 of 72

Sealed classes

OperationResult

Success

Error

45 of 72

46 of 72

47 of 72

Coroutines

https://kotlinlang.org/docs/reference/coroutines-overview.html

48 of 72

Coroutines

fun fetchCoupons() {

viewModelScope.launch {

val result = withContext(Dispatchers.IO) {

repository.retrieveCoupons()

}

}

...

}

fetchCoupons()

onDraw()

onDraw()

renderCoupons()

onDraw()

Other

thread

49 of 72

DataSource

50 of 72

ViewModel

51 of 72

UI

52 of 72

Unit Tests

53 of 72

Test

54 of 72

Test

55 of 72

Test

56 of 72

Test

57 of 72

Test

58 of 72

Test

59 of 72

Checklist

60 of 72

  • Delegate responsibilities to other components. (BL, Data layer, View layer)
  • Consider the lifecycle of android components.
  • Convert your views to Passive Views.
  • Define a rule of dependencies.
  • Refactor and support changes.
  • Easy to create Unit tests.

You should use some architecture pattern...

61 of 72

MVVM - Model View ViewModel

Property changed events

UI events

Sealed class

62 of 72

Model

LiveData

request data

Sealed class

Repository

Data source

Entity

63 of 72

Use Case

64 of 72

ViewModel

65 of 72

Property changed events

UI events

Request data

Sealed class

Observers

LiveData

MVVM

66 of 72

LiveData

MVVM - Clean

Sealed class

Sealed class

DTO

Entity

67 of 72

Clean Architecture

68 of 72

DI

69 of 72

DI

70 of 72

https://github.com/emedinaa/kotlin-mvvm

71 of 72

Eduardo Medina

Thank you

@eduardomedina | emedinaa@gmail.com | github/emedinaa

72 of 72

Android

Dev�Perú

https://www.meetup.com/es-ES/Android-Dev-Peru/