Slide 1

Slide 1 text

Android Architecture Components Room, LiveData & ViewModel Carlos Daniel Munoz - Android Dev @cdmunozi [email protected] August 9th - 2017 @MedellinAndroid

Slide 2

Slide 2 text

New architecture guidelines for Android Development

Slide 3

Slide 3 text

3 The journey ...

Slide 4

Slide 4 text

4 Android Development Process

Slide 5

Slide 5 text

5 Android Development Process ● Put all your code into Activities files

Slide 6

Slide 6 text

6 Android Development Process ● Put all your code into Activities files ● Occasionally, move some stuff into AsyncTasks if error NetworkOnMainThreadException

Slide 7

Slide 7 text

7 Android Development Process ● Put all your code into Activities files ● Occasionally, move some stuff into AsyncTasks if error NetworkOnMainThreadException ● Unit tests and instrumentation tests → hard to do, read and maintain

Slide 8

Slide 8 text

8 Evolution ● Patterns: ○ MVC ○ MVP ○ MVVM ○ . ○ . ○ . ● 3rd party Libraries

Slide 9

Slide 9 text

9 Had Google and Android Dev team shared some guidance?

Slide 10

Slide 10 text

10 No :(

Slide 11

Slide 11 text

Google I/O 2017 Android Architecture Components - AAC

Slide 12

Slide 12 text

12 What are AAC framework?

Slide 13

Slide 13 text

13 What are AAC framework? ● Libraries

Slide 14

Slide 14 text

14 What are AAC framework? ● Libraries ● Guidelines

Slide 15

Slide 15 text

15 What are AAC framework? ● Libraries ● Guidelines ● Common scenarios in apps dev

Slide 16

Slide 16 text

16 What are AAC framework? ● Libraries ● Guidelines ● Common scenarios in apps dev ● Aims:

Slide 17

Slide 17 text

17 What are AAC framework? ● Libraries ● Guidelines ● Common scenarios in apps dev ● Aims: ○ Reduce boilerplate

Slide 18

Slide 18 text

18 What are AAC framework? ● Libraries ● Guidelines ● Common scenarios in apps dev ● Aims: ○ Reduce boilerplate ○ Reduce repetitive code

Slide 19

Slide 19 text

19 What are AAC framework? ● Libraries ● Guidelines ● Common scenarios in apps dev ● Aims: ○ Reduce boilerplate ○ Reduce repetitive code ○ Focus on core features

Slide 20

Slide 20 text

20 Basic blocks of AAC

Slide 21

Slide 21 text

21 Basic blocks of AAC ● Room

Slide 22

Slide 22 text

22 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao)

Slide 23

Slide 23 text

23 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData

Slide 24

Slide 24 text

24 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component.

Slide 25

Slide 25 text

25 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel

Slide 26

Slide 26 text

26 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment.

Slide 27

Slide 27 text

27 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. ● Lifecycle

Slide 28

Slide 28 text

28 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. ● Lifecycle → contains information about the lifecycle state of a component (for instance an Activity).

Slide 29

Slide 29 text

29 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. ● Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). ● Lifecycle owner

Slide 30

Slide 30 text

30 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. ● Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). ● Lifecycle owner → Interface - Annotations @OnLifecycleEvent(Lifecycle.Event.ON_START)

Slide 31

Slide 31 text

31 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. ● Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). ● Lifecycle owner → Interface - Annotations @OnLifecycleEvent(Lifecycle.Event.ON_START) ● Lifecycle observer

Slide 32

Slide 32 text

32 Basic blocks of AAC ● Room → A SQLite object mapper (ORMLite - GreenDao) ● LiveData → A Lifecycle aware observable core component. ● ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. ● Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). ● Lifecycle owner → Interface - Annotations @OnLifecycleEvent(Lifecycle.Event.ON_START) ● Lifecycle observer → Interface - getLifecycle(): LifeCycleRegistry

Slide 33

Slide 33 text

Android Architecture Components - AAC Can be used in isolation. Work really well when used together, though.

Slide 34

Slide 34 text

34 Using Architecture Components

Slide 35

Slide 35 text

35 Using Architecture Components ● App → Events Scheduler

Slide 36

Slide 36 text

36 Using Architecture Components ● App → Events Scheduler ○ Event addition

Slide 37

Slide 37 text

37 Using Architecture Components ● App → Events Scheduler ○ Event addition ○ Event list

Slide 38

Slide 38 text

38 Using Architecture Components ● App → Events Scheduler ○ Event addition ○ Event list ● Initial Version → Offline

Slide 39

Slide 39 text

39 Using Architecture Components ● App → Events Scheduler ○ Event addition ○ Event list ● Initial Version → Offline ● Architecture Components: ○ Lifecycle ○ ViewModel ○ Room

Slide 40

Slide 40 text

40 MVVM using Architecture Components EventRepository EventDatabase EventDao Events Event List EventListActivity EventListViewModel Add Event AddEventActivity AddEventViewModel extends ViewModel @Database (Room) @Dao (Room) extends Lifecycle

Slide 41

Slide 41 text

41 What is Room? ● New way to create databases in Android ● ORM between Java and SQLite ● Less verbose ● No more Cursors ● No more Loaders ● Room isn’t a fully-fledged ORM (no complex nesting objects)

Slide 42

Slide 42 text

42 How to query data?

Slide 43

Slide 43 text

43 How to query data? ● Using LiveData → exposes a stream of events that we can subscribe to receive updates for asynchronously

Slide 44

Slide 44 text

44 How to query data? ● Using LiveData → exposes a stream of events that we can subscribe to receive updates for asynchronously ● Using RxJava2 (Flowable)

Slide 45

Slide 45 text

45 How to query data? ● Using LiveData → exposes a stream of events that we can subscribe to receive updates for asynchronously ● Using RxJava2 (Flowable) ● Place synchronous calls in a background thread such as an AsyncTask. (Room doesn’t allow to issue database queries on the main thread → ANRs)

Slide 46

Slide 46 text

46 Looking at ViewModels ● ViewModel name comes from MVVM pattern ● Responsible for preparing data for the view ● Expose data to any view listening for changes ● Retain state across activity changes → No onSaveInstanceState() ● Share data across activities/fragments → No actions ● Stay in memory until Lifecycle is scoped ○ Activity finishes / Fragment detached ● Should not reference any View → Memory leak ● Extend AndroidViewModel to access app context

Slide 47

Slide 47 text

47 ViewModel’s Lifecycle

Slide 48

Slide 48 text

48 How to work with AAC? ● Empty Activity project ● Add Google Maven Repo en project’s build.gradle ● Add AAC’s components to app/build.gradle ● Let’s code! :)

Slide 49

Slide 49 text

49 References ● Android Architecture Components - https://developer.android.com/topic/libraries/architecture/index.html ● Android Lifecycles - https://developer.android.com/topic/libraries/architecture/lifecycle.html ● Room Reference - https://developer.android.com/topic/libraries/architecture/room.html ● LiveData Reference - https://developer.android.com/topic/libraries/architecture/livedata.html ● ViewModel Reference - https://developer.android.com/topic/libraries/architecture/viewmodel.html

Slide 50

Slide 50 text

50 References ● Android Persistence Codelab - https://codelabs.developers.google.com/codelabs/android-persistence/#0 ● Google’s Android Architecture Github - https://github.com/googlesamples/android-architecture ● Project shown in this MeetUp session - cdmunoz - https://github.com/cdmunoz/EventSchedulerArchComp ● Simple GoogleBooks MVVM project - cdmunoz - https://github.com/cdmunoz/GBooksViewModel

Slide 51

Slide 51 text

51 QUESTIONS?