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

fix: [ANDROAPP-6273] Moves dataset groups calls to background thread #3707

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<List<DataSetDetailModel>>()
Expand All @@ -38,36 +38,34 @@ class DataSetListViewModel(
val selectedSync = MutableLiveData<Action<DataSetDetailModel>>()

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()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class DataSetListViewModelFactory(
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return DataSetListViewModel(
dataSetDetailRepository,
schedulerProvider,
filterManager,
matomoAnalyticsController,
dispatchers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<FilterManager> = PublishProcessor.create()
Expand All @@ -56,7 +53,6 @@ class DataSetListViewModelTest {
whenever(repository.canWriteAny()) doReturn Flowable.just(true)
viewModel = DataSetListViewModel(
repository,
scheduler,
filterManager,
matomoAnalyticsController,
object : DispatcherProvider {
Expand All @@ -75,7 +71,6 @@ class DataSetListViewModelTest {
)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `Should get the list of dataSet`() {
val dataSets = listOf(dummyDataSet(), dummyDataSet(), dummyDataSet())
Expand All @@ -84,7 +79,6 @@ class DataSetListViewModelTest {
) doReturn Flowable.just(dataSets)
viewModel = DataSetListViewModel(
repository,
scheduler,
filterManager,
matomoAnalyticsController,
object : DispatcherProvider {
Expand All @@ -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)
Expand Down