23 lines
660 B
Kotlin
23 lines
660 B
Kotlin
package code.name.monkey.retromusic.fragments
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
import kotlinx.coroutines.*
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
open class CoroutineViewModel(
|
|
private val mainDispatcher: CoroutineDispatcher
|
|
) : ViewModel() {
|
|
private val job = Job()
|
|
protected val scope = CoroutineScope(job + mainDispatcher)
|
|
|
|
protected fun launch(
|
|
context: CoroutineContext = mainDispatcher,
|
|
start: CoroutineStart = CoroutineStart.DEFAULT,
|
|
block: suspend CoroutineScope.() -> Unit
|
|
) = scope.launch(context, start, block)
|
|
|
|
override fun onCleared() {
|
|
super.onCleared()
|
|
job.cancel()
|
|
}
|
|
} |