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 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 -> |
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 readFileSuspend(path: String): File = | |
| suspendCoroutine { | |
| readFile(path) { file -> | |
| it.resume(file) | |
| } | |
| } | |
| fun readFile(path: String, onReady: (File) -> Unit) { | |
| Thread.sleep(1000) |
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
| 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") |
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 CustomScope : CoroutineScope { | |
| private var parentJob = Job() | |
| override val coroutineContext: CoroutineContext | |
| get() = Dispatchers.Main + parentJob | |
| fun onStart() { | |
| parentJob = Job() | |
| } |
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 CoroutineContextProvider { | |
| fun context(): CoroutineContext | |
| } |
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
| 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() |
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
| 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() |
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
| 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 | |
| } |
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.Default) { | |
| println(Thread.currentThread().name) | |
| } | |
| Thread.sleep(50) | |
| } | |
| //DefaultDispatcher-worker-1 |
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() { | |
| val executorDispatcher = Executors | |
| .newWorkStealingPool().asCoroutineDispatcher() | |
| GlobalScope.launch(context = executorDispatcher) { | |
| println(Thread.currentThread().name) | |
| } | |
| Thread.sleep(50) | |
| } |