diff --git a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenter.kt b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenter.kt index e953a7820e..434b519908 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenter.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenter.kt @@ -25,6 +25,7 @@ import org.dhis2.data.forms.dataentry.tablefields.spinner.SpinnerViewModel import org.dhis2.form.model.ValueStoreResult.ERROR_UPDATING_VALUE import org.dhis2.form.model.ValueStoreResult.VALUE_CHANGED import org.dhis2.form.model.ValueStoreResult.VALUE_HAS_NOT_CHANGED +import org.dhis2.usescases.datasets.dataSetTable.dataSetSection.TableDataToTableModelMapper.Companion.INDICATORS_TABLE_ID import org.hisp.dhis.android.core.arch.helpers.Result import org.hisp.dhis.android.core.common.ValueType import org.hisp.dhis.android.core.dataelement.DataElement @@ -109,10 +110,12 @@ class DataValuePresenter( val updatedTableModel = mapper(tableData) val updatedTables = screenState.value.tables.map { tableModel -> - if (tableModel.id == catComboUid) { - updatedTableModel.copy(overwrittenValues = tableModel.overwrittenValues) - } else { - indicatorTables() ?: tableModel + when (tableModel.id) { + catComboUid -> updatedTableModel.copy( + overwrittenValues = tableModel.overwrittenValues, + ) + INDICATORS_TABLE_ID -> indicatorTables() ?: tableModel + else -> tableModel } } diff --git a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/TableDataToTableModelMapper.kt b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/TableDataToTableModelMapper.kt index e361f5fba6..6ab3f171d1 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/TableDataToTableModelMapper.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/TableDataToTableModelMapper.kt @@ -97,10 +97,14 @@ class TableDataToTableModelMapper(val mapFieldValueToUser: MapFieldValueToUser) } return TableModel( - id = "indicators", + id = INDICATORS_TABLE_ID, title = mapFieldValueToUser.resources.getString(R.string.dashboard_indicators), tableHeaderModel = tableHeader, tableRows = tableRows, ) } + + companion object { + const val INDICATORS_TABLE_ID = "indicators" + } } diff --git a/app/src/test/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenterTest.kt b/app/src/test/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenterTest.kt index f396e5c2fd..ee876e27ec 100644 --- a/app/src/test/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenterTest.kt +++ b/app/src/test/java/org/dhis2/usescases/datasets/dataSetTable/dataSetSection/DataValuePresenterTest.kt @@ -18,6 +18,7 @@ import org.dhis2.data.forms.dataentry.tablefields.spinner.SpinnerViewModel import org.dhis2.data.schedulers.TrampolineSchedulerProvider import org.dhis2.form.model.StoreResult import org.dhis2.form.model.ValueStoreResult +import org.dhis2.usescases.datasets.dataSetTable.dataSetSection.TableDataToTableModelMapper.Companion.INDICATORS_TABLE_ID import org.hisp.dhis.android.core.category.CategoryCombo import org.hisp.dhis.android.core.common.ValueType import org.hisp.dhis.android.core.dataelement.DataElement @@ -381,7 +382,7 @@ class DataValuePresenterTest { } val mockedIndicatorTableModel = mock { - on { id } doReturn null + on { id } doReturn INDICATORS_TABLE_ID } val mockedUpdatedTableModel = mock { @@ -426,6 +427,72 @@ class DataValuePresenterTest { verify(view).onValueProcessed() } + @Test + fun shouldUpdateValueWhenSavedForTwoTablesAndIndicators() { + val mockedTableCell = mock { + on { id } doReturn "mocked_id" + on { column } doReturn 1 + on { row } doReturn 0 + on { value } doReturn "valueToSave" + } + + val mockedTableModelA = mock { + on { id } doReturn "tableIdA" + on { hasCellWithId(any()) } doReturn true + } + + val mockedTableModelB = mock { + on { id } doReturn "tableIdB" + on { hasCellWithId(any()) } doReturn true + } + + val mockedIndicatorTableModel = mock { + on { id } doReturn INDICATORS_TABLE_ID + } + + val mockedUpdatedTableModel = mock { + on { id } doReturn "tableIdA_updated" + on { hasCellWithId(any()) } doReturn true + } + + val mockedUpdatedIndicatorTableModel = mock { + on { id } doReturn "updated_indicator" + } + + val tableStateValue = presenter.mutableTableData() + tableStateValue.value = TableScreenState( + listOf(mockedTableModelA, mockedTableModelB, mockedIndicatorTableModel), + ) + + whenever(valueStore.save(any(), any(), any(), any(), any(), any())) doReturn Flowable.just( + StoreResult( + uid = "id", + valueStoreResult = ValueStoreResult.VALUE_CHANGED, + valueStoreResultMessage = null, + ), + ) + + whenever(dataValueRepository.getDataTableModel(any())) doReturn Observable.just( + mockedDataTableModel, + ) + whenever(dataValueRepository.setTableData(any(), any())) doReturn mockedTableData + whenever(mapper.invoke(any())) doReturn mockedUpdatedTableModel + whenever( + mockedUpdatedTableModel.copy(overwrittenValues = mockedTableModel.overwrittenValues), + ) doReturn mockedUpdatedTableModel + + whenever(dataValueRepository.getDataSetIndicators()) doReturn Single.just(mockedIndicators) + whenever(mapper.map(any())) doReturn mockedUpdatedIndicatorTableModel + + presenter.onSaveValueChange(mockedTableCell) + + assertTrue(presenter.currentState().value.tables.size == 3) + assertTrue(presenter.currentState().value.tables[0].id == "tableIdA_updated") + assertTrue(presenter.currentState().value.tables[1].id == "tableIdB") + assertTrue(presenter.currentState().value.tables.last().id == "updated_indicator") + verify(view).onValueProcessed() + } + @Test fun shouldSetErrorValue() { val mockedTableCell = mock {