Concurrency with Kotlin Flow

Jun 5 2024 · Kotlin 1.9.20, Android 14, Android Studio Iguana

Lesson 03: Leverage Flow Operators

Filtering Operators Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll see how to use the filter operator you covered in the previous lesson and also how to use the flatMapLatest operator from the transforming operators’ lesson.

private fun category(categoryId: String): Flow<MovieCategoryViewState> =
  movieRepository.categories()
    .filter { category -> category.id == categoryId }
    .map { category -> MovieCategoryViewState(category.id, category.name) }
import kotlinx.coroutines.flow.*
@OptIn(ExperimentalCoroutinesApi::class)
private fun moviesForCategory(categoryId: String): Flow<List<MovieViewState>> =
  movieRepository.categories()
    .filter { it.id == categoryId }
    .flatMapLatest { movieCategory -> movieRepository.fetchMoviesForCategory(movieCategory.name) }
    .map { movies ->
      movies.map { movie ->
        val imageRes = movieRepository.fetchMovieImage(movieId = movie.id)
          MovieViewState(movie.id, movie.title, "", imageRes)
      }
    }
import kotlinx.coroutines.ExperimentalCoroutinesApi
fun fetchMoviesForCategory(categoryName: String): Flow<List<Movie>> = flow {
  val movies = movieService.fetchMoviesForCategory(categoryName)
  emit(movies)
}
See forum comments
Cinema mode Download course materials from Github
Previous: Filtering Operators Next: Combining Operators