From d1ed0e423ba0ecc9cef00e00bb4bb3138d2627fa Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jul 2020 16:43:43 -0500 Subject: [PATCH] Use coroutines in TagPickerViewModel --- .../java/org/tasks/data/TagDataDaoBlocking.kt | 8 ---- .../java/org/tasks/tags/TagPickerViewModel.kt | 41 ++++++++----------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt b/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt index 9f52f26eb..865c795cc 100644 --- a/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt @@ -9,15 +9,7 @@ class TagDataDaoBlocking @Inject constructor(private val dao: TagDataDao) { return dao.subscribeToTags() } - fun searchTags(query: String): List = runBlocking { - dao.searchTags(query) - } - fun tagDataOrderedByName(): List = runBlocking { dao.tagDataOrderedByName() } - - fun createNew(tag: TagData) = runBlocking { - dao.createNew(tag) - } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/tags/TagPickerViewModel.kt b/app/src/main/java/org/tasks/tags/TagPickerViewModel.kt index d80f42eed..f4848be40 100644 --- a/app/src/main/java/org/tasks/tags/TagPickerViewModel.kt +++ b/app/src/main/java/org/tasks/tags/TagPickerViewModel.kt @@ -1,23 +1,20 @@ package org.tasks.tags import androidx.hilt.lifecycle.ViewModelInject -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.* import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModel -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.disposables.CompositeDisposable -import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.launch import org.tasks.Strings.isNullOrEmpty import org.tasks.data.TagData -import org.tasks.data.TagDataDaoBlocking +import org.tasks.data.TagDataDao import org.tasks.tags.CheckBoxTriStates.State import java.util.* -class TagPickerViewModel @ViewModelInject constructor(private val tagDataDao: TagDataDaoBlocking) : ViewModel() { +class TagPickerViewModel @ViewModelInject constructor( + private val tagDataDao: TagDataDao +) : ViewModel() { + private val tags = MutableLiveData>() - private val disposables = CompositeDisposable() private val selected: MutableSet = HashSet() private val partiallySelected: MutableSet = HashSet() var text: String? = null @@ -37,15 +34,14 @@ class TagPickerViewModel @ViewModelInject constructor(private val tagDataDao: Ta fun getPartiallySelected() = ArrayList(partiallySelected) - fun search(text: String) { - if (!text.equals(this.text, ignoreCase = true)) { - disposables.add( - Single.fromCallable { tagDataDao.searchTags(text) } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { results: List -> onUpdate(results.toMutableList()) }) + fun search(newText: String) { + if (!newText.equals(text, ignoreCase = true)) { + viewModelScope.launch { + val results = tagDataDao.searchTags(newText) + onUpdate(results.toMutableList()) + } } - this.text = text + text = newText } private fun onUpdate(results: MutableList) { @@ -55,11 +51,6 @@ class TagPickerViewModel @ViewModelInject constructor(private val tagDataDao: Ta tags.value = results } - override fun onCleared() { - super.onCleared() - disposables.dispose() - } - fun getState(tagData: TagData): State { if (partiallySelected.contains(tagData)) { return State.PARTIALLY_CHECKED @@ -71,7 +62,9 @@ class TagPickerViewModel @ViewModelInject constructor(private val tagDataDao: Ta var tagData = tagData if (tagData.id == null) { tagData = TagData(tagData.name) - tagDataDao.createNew(tagData) + viewModelScope.launch { + tagDataDao.createNew(tagData) + } } partiallySelected.remove(tagData) return if (checked) {