Slide 1

Slide 1 text

Kotlin’S Flow on Android App Development

Slide 2

Slide 2 text

Kotlin’s Corutine Flow on Android Development Su Myat Tun GDE for Android,WTM Ambassador Senior Android Dev,Lomotif(SG)

Slide 3

Slide 3 text

1. Reactive streams : common practical use case on Android Development 2. Backpressure : every software dev will have to deal with this at some point(reading & writing) 3. Kotlin’s Flow & Action : let’s get our hands dirty ;) What we’ll talk about today

Slide 4

Slide 4 text

What’s Backpressure?

Slide 5

Slide 5 text

“Resistance or force opposing the desired flow of data through software.”

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Reactive streams on Android

Slide 8

Slide 8 text

“The purpose of Reactive Streams is to provide a standard for asynchronous stream processing with non-blocking backpressure.”

Slide 9

Slide 9 text

1. Rxjava : very powerful, many features ,steep learning curve 2. Flow : very powerful & very easy to use (Livedata, Rxjava & Flow implement the observer pattern in your code) What’re powerful tool for reactive stream?

Slide 10

Slide 10 text

Kotlin’s Flow

Slide 11

Slide 11 text

1. based on suspending functions 2. is an additional to kotlin coroutines 3. simplify asynchronous work by getting rid of callbacks 4. emits the value sequentially What’s Flow

Slide 12

Slide 12 text

1. make a request to network 2. accessing the internal storage Most particle common use case

Slide 13

Slide 13 text

1. Flow Builder 2. Flow Operator In Kotlin’s Flow

Slide 14

Slide 14 text

Flow architecture UI ViewModel Repository DataSource flow flow livedata

Slide 15

Slide 15 text

The most basic Flow Builder val colorsFlow = flow { val names = listOf("White", "Black", "Pink", "Blue","Red") for (name in names) { delay(100) emit(name) } } val colorsFlowOf = flowOf("White", "Black", "Pink", "Blue","Red") val colorsAsFlow = listOf("White", "Black", "Pink", "Blue","Red").asFlow()

Slide 16

Slide 16 text

Flow Operator - intermediate fun main() = runBlocking { colorsFlowOf .map { name -> name.length } .filter { length -> length < 5 } println() }

Slide 17

Slide 17 text

How’s map & filter work White 5 Black 5 Pink 4 4 Blue 4 4 Red 3 3 map filter

Slide 18

Slide 18 text

Flow Operator - terminal fun main() = runBlocking { colorsFlowOf .map { name -> name.length } .filter { length -> length < 5 } .collect { println(it) } }

Slide 19

Slide 19 text

How flow on development

Slide 20

Slide 20 text

Going to the following link, and access the sample project I created in git https://github.com/suhtun/Dagger4U.git What you’ll need:

Slide 21

Slide 21 text

Application architecture UI ViewModel Repository DataSource flow flow livedata

Slide 22

Slide 22 text

DataSource: Remote API calls with Retrofit @GET("now_playing?page=1") suspend fun getPlayingMovie(): PlayingMoviewsResponse

Slide 23

Slide 23 text

DataSource: Local data calls with Room @Query("SELECT * FROM movie_table") suspend fun getMovies(): List

Slide 24

Slide 24 text

Repository override fun getMovieList(): Flow>> { return flow { emit(ViewState.loading>()) emit(ViewState.success(movieDao.getMovies())) val response =service.getPlayingMovie() response.movieResults.let { movieDao.insertMovies(it.map {Movie(...)}) } emit(ViewState.success(movieDao.getMovies())) }.catch {emit(ViewState.error(it.message.orEmpty())) }.flowOn(Dispatchers.IO) }

Slide 25

Slide 25 text

ViewModel val results : LiveData>> = Transformations .switchMap(callRepo) { search -> if (search.isNullOrBlank()) { AbsentLiveData.create() } else { repository.getMovieList().asLiveData() } }

Slide 26

Slide 26 text

UI viewmodel.results.observe(viewLifecycleOwner, Observer { result -> loading = true if (result.data != null) adapter.submitList(result?.data) })

Slide 27

Slide 27 text

https://codelabs.developers.google.com/c odelabs/advanced-kotlin-coroutines/#7 Keep learning:

Slide 28

Slide 28 text

Question???