Skip to content

Instantly share code, notes, and snippets.

View RBusarow's full-sized avatar
🕶️

Rick Busarow RBusarow

🕶️
View GitHub Profile
@RBusarow
RBusarow / MyViewModel.kt
Last active April 20, 2019 12:45
Basic boilerplate version of a CoroutineScope implementation in a ViewModel
class MyViewModel : ViewModel(), CoroutineScope {
private val job = SupervisorJob()
override val coroutineContext: CoroutineContext
get() = SupervisorJob() + Dispatchers.Default
override fun onCleared() {
job.cancel()
super.onCleared()
@RBusarow
RBusarow / MyViewModel.kt
Last active April 20, 2019 12:45
Basic boilerplate version of a CoroutineScope implementation in a ViewModel, with an abstract BaseViewModel
class MyViewModel : BaseViewModel() // nothing at all to implement!
abstract class BaseViewModel : ViewModel(), CoroutineScope {
private val job = SupervisorJob()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Default
override fun onCleared() {
@RBusarow
RBusarow / SupervisorCoroutineScope.kt
Created March 17, 2019 23:43
Simple factory for creating an implementation of a CoroutineScope using a SupervisorJob
fun SupervisorCoroutineScope(
dispatcher: CoroutineDispatcher
): CoroutineScope = object : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = SupervisorJob() + dispatcher
}
@RBusarow
RBusarow / CoroutineScopeClassDelegation.kt
Last active April 20, 2019 13:14
CoroutineScope class delegation
abstract class BaseViewModel :
ViewModel(),
CoroutineScope by SupervisorCoroutineScope(Dispatchers.Default) {
override fun onCleared() {
cancel()
super.onCleared()
}
}
@RBusarow
RBusarow / MyRepositoryImpl.kt
Last active April 20, 2019 12:49
repository CoroutineScope injection with class delegation
class MyRepositoryImpl(coroutineScope: CoroutineScope) :
MyRepository, CoroutineScope by coroutineScope
@RBusarow
RBusarow / RepositoryCoroutineScopeDelegation.kt
Created March 17, 2019 23:49
Initialization of a repository using the CoroutineScope of the dependant class
class MyViewModel : BaseViewModel() {
val myRepository = MyRepositoryImpl(
coroutineScope = this + Dispatchers.IO
)
}
@RBusarow
RBusarow / ViewModelCoroutineScopeInjection.kt
Created March 17, 2019 23:51
Injecting CoroutineScope into the ViewModel
class MyViewModel(
private val myRepository: MyRepository,
coroutineScope: CoroutineScope
) : BaseViewModel(coroutineScope)
abstract class BaseViewModel(
coroutineScope: CoroutineScope
) : ViewModel(), CoroutineScope by coroutineScope {
override fun onCleared() {
@RBusarow
RBusarow / MyActivity.kt
Last active April 20, 2019 13:26
Injecting a CoroutineScope into a ViewModel using a common viewModelFactory lambda
viewModelFactory {
val scope = SupervisorCoroutineScope(Dispatchers.Default)
MyViewModel(MyRepository(scope), scope)
}
@RBusarow
RBusarow / TestCoroutineExtension.kt
Last active November 20, 2023 19:03
A JUnit 4 Rule and JUnit 5 Extension for utilizing TestCoroutineDispatcher and TestCoroutineScope from kotlinx.coroutines-test
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.jupiter.api.extension.AfterAllCallback
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.ExtensionContext
@RBusarow
RBusarow / MyViewModel.kt
Created June 21, 2019 23:17
Basic addition of CoroutineScope to a ViewModel
class MyViewModel : ViewModel() {
val scope : CoroutineScope = MainScope()
override fun onCleared() {
scope.cancel()
super.onCleared()
}
}