This version of the app is called todo-mvp-kotlin-coroutines
and is based on todo-mvp-kotlin. The sample aims to:
- Replace all asynchronous operations with coroutines, which simplify asynchronous programming by providing possibility to write code in direct style (sequentially).
Before exploring this sample, you should familiarize yourself with the following topics:
- The project README
- The todo-mvp sample
- The todo-mvp-kotlin sample
- Kotlin coroutine documentation
- kotlin stdlib
- kotlin-android plugin
- kotlinx-coroutines-core
- kotlinx-coroutines-android
All functions in TasksDataSource
replaces with suspend
functions.
interface TasksDataSource {
suspend fun getTasks(): Result<List<Task>>
suspend fun getTask(taskId: String): Result<Task>
suspend fun saveTask(task: Task)
suspend fun completeTask(task: Task)
suspend fun completeTask(taskId: String)
suspend fun activateTask(task: Task)
suspend fun activateTask(taskId: String)
suspend fun clearCompletedTasks()
suspend fun refreshTasks()
suspend fun deleteAllTasks()
suspend fun deleteTask(taskId: String)
}
In todo-mvp-kotlin
sample functions getTasks
and getTask
had callbacks with onTasksLoaded
and onDataNotAvailable
functions. In todo-mvp-kotlin-coroutines
sample this functions now return Result
which can be ether a Success
or Error
.
sealed class Result<out T : Any> {
class Success<out T : Any>(val data: T) : Result<T>()
class Error(val exception: Throwable) : Result<Nothing>()
}
To launch coroutines you need to specify a CoroutineContext
. In tests all coroutines are launched via runBlocking
or with EmptyCoroutineContext
.
@Test
fun loadStatisticsWhenTasksAreUnavailable_CallErrorToDisplay() = runBlocking {
// Tasks data isn't available
setTasksNotAvailable(tasksRepository)
// When statistics are loaded
statisticsPresenter.start()
// Then an error message is shown
verify(statisticsView).showLoadingStatisticsError()
}
Files were converted mostly 1:1 from TODO-MVP's Java code.
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Kotlin 54 944 1604 3107 (3060 in MVP-kotlin)
XML 34 95 338 816
-------------------------------------------------------------------------------
SUM: 89 1039 1942 3923
-------------------------------------------------------------------------------