Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin's Flow on Android App Development

Su Myat
October 17, 2020
190

Kotlin's Flow on Android App Development

Asynchronous programming is complex. In this talk, you will learn about Kotlin's Flow where to use, and how Flow's help write reliable apps. I'll present using a sample application that will dive straight in and have a look at the basics, getting from API calls to our UI only using Kotlin's Flow with LiveData, ViewModel, and the repository pattern. After this talk, you'll see how Flows enable writing safe, reliable, and how it can strengthen your app development.

Su Myat

October 17, 2020
Tweet

Transcript

  1. Kotlin’s Corutine Flow on Android Development Su Myat Tun GDE

    for Android,WTM Ambassador Senior Android Dev,Lomotif(SG)
  2. 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
  3. “The purpose of Reactive Streams is to provide a standard

    for asynchronous stream processing with non-blocking backpressure.”
  4. 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?
  5. 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
  6. 1. make a request to network 2. accessing the internal

    storage Most particle common use case
  7. 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()
  8. Flow Operator - intermediate fun main() = runBlocking { colorsFlowOf

    .map { name -> name.length } .filter { length -> length < 5 } println() }
  9. How’s map & filter work White 5 Black 5 Pink

    4 4 Blue 4 4 Red 3 3 map filter
  10. Flow Operator - terminal fun main() = runBlocking { colorsFlowOf

    .map { name -> name.length } .filter { length -> length < 5 } .collect { println(it) } }
  11. Going to the following link, and access the sample project

    I created in git https://github.com/suhtun/Dagger4U.git What you’ll need:
  12. Repository override fun getMovieList(): Flow<ViewState<List<Movie>>> { return flow { emit(ViewState.loading<List<Movie>>())

    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) }
  13. ViewModel val results : LiveData<ViewState<List<Movie>>> = Transformations .switchMap(callRepo) { search

    -> if (search.isNullOrBlank()) { AbsentLiveData.create() } else { repository.getMovieList().asLiveData() } }
  14. UI viewmodel.results.observe(viewLifecycleOwner, Observer { result -> loading = true if

    (result.data != null) adapter.submitList(result?.data) })