Giter Club home page Giter Club logo

android-architecture's Introduction

todo-mvp-kotlin-coroutines

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).

What you need

Before exploring this sample, you should familiarize yourself with the following topics:

Dependencies

  • kotlin stdlib
  • kotlin-android plugin
  • kotlinx-coroutines-core
  • kotlinx-coroutines-android

Implementing the app

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>()
}

Testability

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()
}

Code metrics

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
-------------------------------------------------------------------------------

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.