Step #3: Consuming a FilterMode

Now, we can update ToDoRepository to help us get at a filtered edition of the to-do items.

First, we need to map from the FilterMode enum to the functions and parameters that we need for ToDoEntity.Store. To handle that, add this function to ToDoRepository:

  private fun filteredEntities(filterMode: FilterMode) = when (filterMode) {
    FilterMode.ALL -> store.all()
    FilterMode.OUTSTANDING -> store.filtered(isCompleted = false)
    FilterMode.COMPLETED -> store.filtered(isCompleted = true)
  }

Here, we just use Kotlin’s “exhaustive when” to handle the three FilterMode cases, calling the appropriate function on ToDoEntity.Store for each.

Then, replace the items function in ToDoRepository with this implementation:

  fun items(filterMode: FilterMode = FilterMode.ALL): Flow<List<ToDoModel>> =
    filteredEntities(filterMode).map { all -> all.map { it.toModel() } }

Here, we use the new filteredEntities() function to get the Flow of entities. We have items() use a default value for its filterMode parameter, so a call to items() with no parameters will retrieve the unfiltered list (FilterMode.ALL).


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.