Skip to content

Instantly share code, notes, and snippets.

View juliuscanute's full-sized avatar
💭
I may be slow to respond.

juliuscanute

💭
I may be slow to respond.
View GitHub Profile
@juliuscanute
juliuscanute / Api.kt
Last active November 30, 2019 21:04
[Creating your own suspendable API] #kotlin #coroutine
fun executeBackground(action: suspend () -> Unit) {
GlobalScope.launch { action() }
}
fun executeMain(action: suspend () -> Unit) {
GlobalScope.launch(context = Dispatchers.Main) { action() }
}
suspend fun <T : Any> getValue(provider: () -> T): T =
suspendCoroutine { continuation ->
@juliuscanute
juliuscanute / Suspendable.kt
Created November 30, 2019 21:07
[Change code to suspendable] #kotlin #coroutine
suspend fun readFileSuspend(path: String): File =
suspendCoroutine {
readFile(path) { file ->
it.resume(file)
}
}
fun readFile(path: String, onReady: (File) -> Unit) {
Thread.sleep(1000)
@juliuscanute
juliuscanute / AsyncCoroutine.kt
Created December 2, 2019 19:51
[Using async] #kotlin #coroutine
private fun getUserByIdFromNetwork(
userId: Int,
scope: CoroutineScope) =
scope.async {
if (!scope.isActive) {
return@async User(0, "", "")
}
println("Retrieving user from network")
delay(3000)
println("Still in the coroutine")
@juliuscanute
juliuscanute / CustomScope.kt
Created December 2, 2019 19:53
[Custom Coroutine Scope] #kotlin #coroutine
class CustomScope : CoroutineScope {
private var parentJob = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + parentJob
fun onStart() {
parentJob = Job()
}
@juliuscanute
juliuscanute / CoroutineContextProvider.kt
Created December 3, 2019 20:08
[Providing contexts] #kotlin #coroutine
interface CoroutineContextProvider {
fun context(): CoroutineContext
}
@juliuscanute
juliuscanute / BrowseCreaturesViewModel.kt
Last active December 8, 2019 10:13
[UI Layer] #clean #architecture #android
package com.raywenderlich.android.creatures.presentation.browse
open class BrowseCreaturesViewModel @Inject internal constructor(
private val getCreatures: GetCreatures,
private val creatureMapper: CreatureMapper) : ViewModel() {
private val creaturesLiveData: MutableLiveData<Resource<List<CreatureView>>> = MutableLiveData()
init {
fetchCreatures()
@juliuscanute
juliuscanute / FlowableUseCase.kt
Last active December 8, 2019 10:18
[Domain Layer] #clean #architecture #android
package com.raywenderlich.android.creatures.domain.interactor
/**
* Abstract class for a UseCase that returns an instance of a [Single].
*/
abstract class FlowableUseCase<T, in Params> constructor(
private val threadExecutor: ThreadExecutor,
private val postExecutionThread: PostExecutionThread) {
private val disposables = CompositeDisposable()
@juliuscanute
juliuscanute / CreatureCacheImpl.kt
Created December 8, 2019 10:10
[Data Layer] #clean #architecture #android
package com.raywenderlich.android.creatures.cache
class CreatureCacheImpl @Inject constructor(private val creaturesDatabase: CreaturesDatabase,
private val entityMapper: CreatureEntityMapper,
private val preferencesHelper: PreferencesHelper) :
CreatureCache {
companion object {
private const val EXPIRATION_TIME = (60 * 10 * 1000).toLong() // 10 minutes
}
@juliuscanute
juliuscanute / Confined.kt
Created December 8, 2019 20:26
[Run coroutine on launch thread] #coroutine #kotlin #android
fun main() {
GlobalScope.launch(context = Dispatchers.Default) {
println(Thread.currentThread().name)
}
Thread.sleep(50)
}
//DefaultDispatcher-worker-1
@juliuscanute
juliuscanute / Main.kt
Created December 8, 2019 20:29
[Creating a work stealing Executor] #kotlin #coroutine #android
fun main() {
val executorDispatcher = Executors
.newWorkStealingPool().asCoroutineDispatcher()
GlobalScope.launch(context = executorDispatcher) {
println(Thread.currentThread().name)
}
Thread.sleep(50)
}