This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -eu | |
| # Docker | |
| sudo apt remove --yes docker docker-engine docker.io \ | |
| && sudo apt update \ | |
| && sudo apt --yes --no-install-recommends install \ | |
| apt-transport-https \ | |
| ca-certificates \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| GlobalScope.launch { | |
| // do some heavy work | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ListUsersViewModel( | |
| private val repository: UserRepository | |
| ) : CoroutineViewModel() { | |
| private val users: MutableLiveData<List<User>> = MutableLiveData() | |
| private val loading: MutableLiveData<Boolean> = MutableLiveData() | |
| private val error: MutableLiveData<Throwable> = MutableLiveData() | |
| fun users() = users as LiveData<List<User>> | |
| fun loading() = loading as LiveData<Boolean> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open class CoroutineViewModel : ViewModel(), CoroutineScope { | |
| override val coroutineContext = Main | |
| protected val jobs = ArrayList<Job>() | |
| infix fun ArrayList<Job>.add(job: Job) { this.add(job) } | |
| override fun onCleared() { | |
| super.onCleared() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserDataRepository( | |
| private val api: GithubApi | |
| ) : UserRepository { | |
| override suspend fun getAll() = withContext(IO) { | |
| async { api.getAll().await().map { it.toModel() } } | |
| } | |
| override suspend fun getByUsername(username: String) = withContext(IO) { | |
| async { api.getByUsername(username).await().toModel() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface UserRepository { | |
| suspend fun getAll(): Deferred<List<User>> | |
| suspend fun getByUsername(username: String): Deferred<User> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface GithubApi { | |
| @GET("users") | |
| fun getAll(): Deferred<List<UserResponse>> | |
| @GET("users/{username}") | |
| fun getByUsername(@Path("username") username: String): Deferred<UserResponse> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| suspend fun loadUserData(username: String): User { | |
| return coroutineScope { | |
| val user = async { loadUser(username) } | |
| val repos = async { loadRepos(username) } | |
| buildUserData(user.await(), repos.await()) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| GlobalScope.launch(context = Dispatchers.Main) { | |
| val orders = withContext(context = Dispatchers.IO) { | |
| fetchOrders().await() | |
| } | |
| printOrders(orders) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| GlobalScope.launch { | |
| val orders = fetchOrders().await() | |
| println(orders) | |
| } | |
| } | |
| suspend fun fetchOrders() = GlobalScope.async { | |
| delay(2000) // simulates a external data fetch | |
| listOf( |
NewerOlder