diff --git a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModel.kt b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModel.kt index 9eb943de26..e9a611cfec 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModel.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModel.kt @@ -4,14 +4,16 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import kotlinx.coroutines.async +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch +import kotlinx.coroutines.reactive.asFlow +import kotlinx.coroutines.withContext import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.matomo.Actions import org.dhis2.commons.matomo.Categories import org.dhis2.commons.matomo.Labels import org.dhis2.commons.matomo.MatomoAnalyticsController -import org.dhis2.commons.schedulers.SchedulerProvider import org.dhis2.commons.viewmodel.DispatcherProvider import org.dhis2.usescases.datasets.datasetDetail.DataSetDetailModel import org.dhis2.usescases.datasets.datasetDetail.DataSetDetailRepository @@ -20,11 +22,9 @@ import timber.log.Timber class DataSetListViewModel( private val dataSetDetailRepository: DataSetDetailRepository, - schedulerProvider: SchedulerProvider, val filterManager: FilterManager, val matomoAnalyticsController: MatomoAnalyticsController, dispatcher: DispatcherProvider, - ) : ViewModel() { private val _datasets = MutableLiveData>() @@ -38,36 +38,34 @@ class DataSetListViewModel( val selectedSync = MutableLiveData>() init { - viewModelScope.launch(dispatcher.io()) { - val datasets = async { - filterManager.asFlowable() - .startWith(filterManager) - .flatMap { filterManager: FilterManager -> - dataSetDetailRepository.dataSetGroups( - filterManager.orgUnitUidsFilters, - filterManager.periodFilters, - filterManager.stateFilters, - filterManager.catOptComboFilters, - ).subscribeOn(schedulerProvider.io()) - } - .subscribeOn(schedulerProvider.io()) - .observeOn(schedulerProvider.ui()) - .subscribe({ + filterManager.asFlowable() + .startWith(filterManager) + .flatMap { filterManager: FilterManager -> + dataSetDetailRepository.dataSetGroups( + filterManager.orgUnitUidsFilters, + filterManager.periodFilters, + filterManager.stateFilters, + filterManager.catOptComboFilters, + ) + } + .asFlow() + .catch { Timber.d(it) } + .collectLatest { + withContext(dispatcher.ui()) { _datasets.value = it - }) { t: Throwable? -> Timber.d(t) } - } - val permissions = async { - dataSetDetailRepository.canWriteAny() - .subscribeOn(schedulerProvider.io()) - .observeOn(schedulerProvider.ui()) - .subscribe({ + } + } + + dataSetDetailRepository.canWriteAny() + .asFlow() + .catch { Timber.d(it) } + .collectLatest { + withContext(dispatcher.ui()) { _canWrite.value = it - }) { t: Throwable? -> Timber.e(t) } - } - datasets.await() - permissions.await() + } + } } } diff --git a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModelFactory.kt b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModelFactory.kt index 7bce936c93..cd7e07ef33 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModelFactory.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListViewModelFactory.kt @@ -21,7 +21,6 @@ class DataSetListViewModelFactory( override fun create(modelClass: Class): T { return DataSetListViewModel( dataSetDetailRepository, - schedulerProvider, filterManager, matomoAnalyticsController, dispatchers, diff --git a/app/src/test/java/org/dhis2/usescases/datasets/datasetDetail/datasetlist/DataSetListViewModelTest.kt b/app/src/test/java/org/dhis2/usescases/datasets/datasetDetail/datasetlist/DataSetListViewModelTest.kt index 916d15ce09..f331c17039 100644 --- a/app/src/test/java/org/dhis2/usescases/datasets/datasetDetail/datasetlist/DataSetListViewModelTest.kt +++ b/app/src/test/java/org/dhis2/usescases/datasets/datasetDetail/datasetlist/DataSetListViewModelTest.kt @@ -15,7 +15,6 @@ import org.dhis2.commons.matomo.Categories import org.dhis2.commons.matomo.Labels import org.dhis2.commons.matomo.MatomoAnalyticsController import org.dhis2.commons.viewmodel.DispatcherProvider -import org.dhis2.data.schedulers.TrampolineSchedulerProvider import org.dhis2.usescases.datasets.datasetDetail.DataSetDetailModel import org.dhis2.usescases.datasets.datasetDetail.DataSetDetailRepository import org.dhis2.usescases.datasets.datasetDetail.datasetList.DataSetListViewModel @@ -37,11 +36,9 @@ class DataSetListViewModelTest { private lateinit var viewModel: DataSetListViewModel private val repository: DataSetDetailRepository = mock() - private val scheduler = TrampolineSchedulerProvider() private val filterManager: FilterManager = mock() private val matomoAnalyticsController: MatomoAnalyticsController = mock() - @OptIn(ExperimentalCoroutinesApi::class) private val testingDispatcher = StandardTestDispatcher() private val filterProcessor: FlowableProcessor = PublishProcessor.create() @@ -56,7 +53,6 @@ class DataSetListViewModelTest { whenever(repository.canWriteAny()) doReturn Flowable.just(true) viewModel = DataSetListViewModel( repository, - scheduler, filterManager, matomoAnalyticsController, object : DispatcherProvider { @@ -75,7 +71,6 @@ class DataSetListViewModelTest { ) } - @OptIn(ExperimentalCoroutinesApi::class) @Test fun `Should get the list of dataSet`() { val dataSets = listOf(dummyDataSet(), dummyDataSet(), dummyDataSet()) @@ -84,7 +79,6 @@ class DataSetListViewModelTest { ) doReturn Flowable.just(dataSets) viewModel = DataSetListViewModel( repository, - scheduler, filterManager, matomoAnalyticsController, object : DispatcherProvider { @@ -105,7 +99,6 @@ class DataSetListViewModelTest { assert(viewModel.datasets.value == dataSets) } - @OptIn(ExperimentalCoroutinesApi::class) @Test fun `Should get write permissions`() { whenever(repository.canWriteAny()) doReturn Flowable.just(true)