diff --git a/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt b/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt index 8bda812..b2ed8e2 100644 --- a/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt +++ b/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt @@ -3,29 +3,17 @@ package com.dp.logcatapp.util import android.app.Application import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.Job import kotlin.coroutines.CoroutineContext open class ScopedViewModel : ViewModel(), CoroutineScope { - private val job = Job() - override val coroutineContext: CoroutineContext - get() = job + Main - - override fun onCleared() { - job.cancel() - } + get() = viewModelScope.coroutineContext } open class ScopedAndroidViewModel(application: Application) : AndroidViewModel(application), CoroutineScope { - private val job = Job() override val coroutineContext: CoroutineContext - get() = job + Main - - override fun onCleared() { - job.cancel() - } + get() = viewModelScope.coroutineContext }