Exercise: All users flow
You fetch users from an API that uses pagination. You want to expose a flow that emits all users. Implement a getAllUsers
function that returns a flow of all users. Use a fetchUsers
function to fetch users from the API. You can assume that the fetchUsers
function is already implemented. It takes a page number as a parameter and returns a list of users from that page. If there are no more pages, it returns an empty list. You should fetch pages on demand.
class AllUsers(private val repository: UserRepository) {
fun getAllUsers(): Flow<User> = TODO()
}
interface UserRepository {
fun fetchUsers(pageNumber: Int): List<User>
}
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in coroutines/flow/AllUsers.kt. You can find there starting code and unit tests.
Once you are done with the exercise, you can check your solution here.
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.test.assertEquals
class AllUsers(private val repository: UserRepository) {
fun getAllUsers(): Flow<User> = TODO()
}
interface UserRepository {
fun fetchUsers(pageNumber: Int): List<User>
}
data class User(val name: String)
internal class AllUsersTests {
@Test
fun test() = runTest {
val size = 10_000
val pageSize = 10
val repo = object : UserRepository {
val users = List(size) { User("User$it") }
var timesUsed = 0
override fun fetchUsers(pageNumber: Int): List<User> =
users.drop(pageSize * pageNumber)
.take(pageSize)
.also { timesUsed++ }
}
val service = AllUsers(repo)
val s = service.getAllUsers()
assertEquals(size, s.count())
assertEquals(size / pageSize + 1, repo.timesUsed)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.