Skip to content

Commit

Permalink
Use viewModelScope
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Feb 18, 2020
1 parent 44e9932 commit 8cf8597
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8cf8597

Please sign in to comment.