Slide 1

Slide 1 text

Android Architecture Components

Slide 2

Slide 2 text

Akshay Chordiya ● Co-founder of BitFurther ● Android Developer by heart ● Active community speaker ● Co-author of upcoming book “Kotlin Blueprints” ● Graduated in 2016 @Aky @Akshay_Chordiya AkshayChordiya

Slide 3

Slide 3 text

Agenda ● Life before ● Why Architecture Components? ● What are these components? ● Exploring the arch components ● Guide to App Architecture ● Pro-Tips ● Conclusion ● Q&A

Slide 4

Slide 4 text

Life before ● Android Development is difficult!* ● No official opinionated guide ● Confusion deciding architecture ● Under and over architecturing problem ● The architecture war (MVP, MVVM, Clean, etc)

Slide 5

Slide 5 text

Why Architecture Components?

Slide 6

Slide 6 text

Benefits of Architecture Components Persist Data Manage Lifecycle Prevents from common issues Modular Less boilerplate

Slide 7

Slide 7 text

Overview of Components

Slide 8

Slide 8 text

Overview of Architecture Components Lifecycle LiveData Room Image credits - developer.android.com ViewModel

Slide 9

Slide 9 text

Lifecycle

Slide 10

Slide 10 text

The problem override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Network call api.get().onResponse { data -> textView.text = data } } It is WRONG!

Slide 11

Slide 11 text

The workaround override fun onCreate(savedInstanceState: Bundle?) { /// .... // Network call request = api.get() request.onResponse { data -> textView.text = data request = null } } override fun onDestory() { if (request != null) request.cancel() } BORING Boilerplate

Slide 12

Slide 12 text

“Lifecycle provides classes and interfaces that let you build lifecycle-aware components.”

Slide 13

Slide 13 text

With lifecycle override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Network call api.get().onResponse(getLifecycle()) { data -> textView.text = data } }

Slide 14

Slide 14 text

What is Lifecycle aware? “The components which are aware about the state of the activity

Slide 15

Slide 15 text

Anatomy of Lifecycle LifecycleOwners are objects with Lifecycle like Activity and Fragment. LifecycleObservers observe LifecycleOwners, and are notified about lifecycle changes LifecycleOwner (Activity/Fragment) LifecycleObserver (Ex. LiveData)

Slide 16

Slide 16 text

LiveData

Slide 17

Slide 17 text

“Instantly notify the changes in the data to the UI”

Slide 18

Slide 18 text

What is LiveData? ● An observable data holder ● It notifies the observers when data changes so that you can update the UI ● Respects lifecycle aka it is lifecycle aware ● Pretty similar to RxJava

Slide 19

Slide 19 text

Overall Flow LifecycleOwners (Activity/Fragment) LiveData UI components observe which observes

Slide 20

Slide 20 text

How it works? Observes for updates LiveData value updated Is STARTED or RESUMED? Notify the observer Update the UI

Slide 21

Slide 21 text

Advantages of LiveData ● Always up to date data ● It is lifecycle aware ● Handles proper configuration change ● No memory leaks

Slide 22

Slide 22 text

ViewModel

Slide 23

Slide 23 text

Image credits: google-developers/viewmodels-a-simple-example-ed5ac416317e

Slide 24

Slide 24 text

“They contain data for UI that survives configuration change.”

Slide 25

Slide 25 text

What is ViewModel? ● Designed to hold and manage UI related data ● Survives configuration changes ● It is lifecycle aware ● It will often store LiveData objects ● Helps to communicate between Activity and Fragment ● Eliminates “God Activities”

Slide 26

Slide 26 text

Sample UI

Slide 27

Slide 27 text

Code: ViewModel class NewsViewModel() : ViewModel() { private var newsArticles: List init { // Load the data over here newsArticles = .... } fun getNewsArticles(): List { return newsArticles } }

Slide 28

Slide 28 text

Code: ViewModel class NewsViewModel() : ViewModel() { private var newsArticles: LiveData> init { // Load the data over here newsArticles = .... } fun getNewsArticles(): LiveData> { return newsArticles } }

Slide 29

Slide 29 text

Usage: ViewModel // Get ViewModel val newsViewModel = ViewModelProviders.of(this).get(NewsViewModel::class.java) // Observing for data change newsViewModel.getNewsArticles().observe(this, Observer> { // Update the UI })

Slide 30

Slide 30 text

Lifecycle of ViewModel

Slide 31

Slide 31 text

“Avoid references to context or views in the ViewModel”

Slide 32

Slide 32 text

Room

Slide 33

Slide 33 text

“Room is a robust SQL mapping library”

Slide 34

Slide 34 text

What is Room? ● It provides local data persistence ● Abstraction layer over existing SQLite database ● Checks SQL query at compile time ● Ability to observe for changes in the database using LiveData ● Eliminates the boilerplate code ● Plays well with RxJava

Slide 35

Slide 35 text

Persistence Spectrum SQL Objects Android SQLite APIs SQL Delight Room Object Box Realm

Slide 36

Slide 36 text

Entity @Entity(tableName = "news_article") data class NewsArticles( @PrimaryKey(autoGenerate = true) var id: Int = 0, var author: String? = null, var title: String = "", var description: String = "")

Slide 37

Slide 37 text

DAO @Dao interface NewsArticlesDao { @Insert fun insertArticle(article: NewsArticles) @Query("SELECT * FROM news_article") fun getNewsArticles(): LiveData> @Update fun updateArticle(article: Article) @Delete fun deleteArticle(article: Article) } CRUD operations Compile time verification

Slide 38

Slide 38 text

Database @Database(entities = arrayOf(NewsArticles::class), version = 1) abstract class NewsDatabase : RoomDatabase() { /** * Get news article DAO */ abstract fun newsArticlesDao(): NewsArticlesDao }

Slide 39

Slide 39 text

Anatomy of Room ● Defines database holder ● Main access point to the database connection ● Main component of Room ● It defines the methods for interaction with the database ● Implementation is auto-generated ● For each entity a database table is created ● It represents a class which holds a database row

Slide 40

Slide 40 text

Guide to App Architecture

Slide 41

Slide 41 text

Architecture

Slide 42

Slide 42 text

Testing

Slide 43

Slide 43 text

Testing Component Tests Mock UI Espresso ViewModel ViewModel JUnit Repository Repository JUnit DAO and WebService DAO Instrumented - WebService Instrumented MockWebServer

Slide 44

Slide 44 text

Pro-Tips ● You can use them in production! ● Mix match them to solve your problem ● Don’t shift if you're already happy with existing architecture ● Use in-memory database mode for Room when developing or testing ● Use LifecycleService to get life-cycle in Service

Slide 45

Slide 45 text

Conclusion

Slide 46

Slide 46 text

“They are opinionated, scalable and modular”

Slide 47

Slide 47 text

“Architecture components make Android development easy again”

Slide 48

Slide 48 text

Questions

Slide 49

Slide 49 text

Further Resources ● All the images and logos used are trademarks of respective companies and/or author ● Android Architecture ● Guide to App Architecture ● Introduction to Architecture Components - Medium ● Exploring ViewModel Architecture component ● Exploring LiveData Architecture component ● Exploring Room Architecture component ● Sample news app with Architecture Components ● Architecture Components: Improve Your App's Design ● Google I/O Architecture Components Talk ● Architecture Components - GDD ● Getting started with Kotlin - YouTube

Slide 50

Slide 50 text

Thank you! Slides: https://goo.gl/UF9ewh