Skip to content

Commit

Permalink
changes calls to datasets to background thread and now uses kotlin's …
Browse files Browse the repository at this point in the history
…flows rather than rxjava's subscribeOn to prevent ANR
  • Loading branch information
ferdyrod committed Jul 1, 2024
1 parent 9d81916 commit 8c0adc6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
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

0 comments on commit 8c0adc6

Please sign in to comment.