Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Kotlin Coroutines #270

Merged
merged 9 commits into from
Mar 24, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple
import com.chuckerteam.chucker.internal.data.entity.RecordedThrowableTuple
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import com.chuckerteam.chucker.internal.support.NotificationHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus

internal class MainViewModel : ViewModel() {

private val currentFilter = MutableLiveData<String>("")
private val backgroundScope = CoroutineScope(Dispatchers.IO) + Job()

val transactions: LiveData<List<HttpTransactionTuple>> = currentFilter.switchMap { searchQuery ->
with(RepositoryProvider.transaction()) {
Expand All @@ -44,14 +40,14 @@ internal class MainViewModel : ViewModel() {
}

fun clearTransactions() {
backgroundScope.launch {
viewModelScope.launch {
RepositoryProvider.transaction().deleteAllTransactions()
}
NotificationHelper.clearBuffer()
}

fun clearThrowables() {
backgroundScope.launch {
viewModelScope.launch {
RepositoryProvider.throwable().deleteAllThrowables()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import kotlinx.android.synthetic.main.chucker_fragment_transaction_payload.*
CuriousNikhil marked this conversation as resolved.
Show resolved Hide resolved
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
Expand All @@ -51,7 +51,7 @@ internal class TransactionPayloadFragment :

private lateinit var viewModel: TransactionViewModel

private val uiScope = CoroutineScope(Dispatchers.Main) + Job()
private val uiScope = MainScope() + Job()
CuriousNikhil marked this conversation as resolved.
Show resolved Hide resolved

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down