Skip to content

Commit

Permalink
Merge pull request dhis2#3504 from dhis2/ANDROAPP-5919
Browse files Browse the repository at this point in the history
feat: [ANDROAPP-5919] multiselection on dataset
  • Loading branch information
andresmr authored Mar 6, 2024
2 parents b45565d + dc7f2f4 commit 2bfc9bc
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,7 @@ class DataValueRepository(
isNumber = dataElement.valueType()!!.isNumeric
}

val options = dataElement.optionSetUid()?.let {
d2.optionModule().options()
.byOptionSetUid().eq(it)
.orderBySortOrder(RepositoryScope.OrderByDirection.ASC)
.blockingGet()
.map { option -> "${option.code()}_${option.displayName()}" }
} ?: emptyList()
val options = getOptionsForOptionSet(dataElement.optionSetUid())

for (
categoryOptionCombo in categorOptionCombos
Expand Down Expand Up @@ -1011,7 +1005,7 @@ class DataValueRepository(
null,
dataElement.displayDescription(),
dataElement.uid(),
emptyList(),
getOptionsForOptionSet(dataElement.optionSetUid()),
"android",
0,
0,
Expand All @@ -1035,4 +1029,12 @@ class DataValueRepository(
fun getDataSetInfo(): Triple<String, String, String> {
return Triple(periodId, orgUnitUid, attributeOptionComboUid)
}

private fun getOptionsForOptionSet(optionSetUid: String?) = optionSetUid?.let {
d2.optionModule().options()
.byOptionSetUid().eq(it)
.orderBySortOrder(RepositoryScope.OrderByDirection.ASC)
.blockingGet()
.map { option -> "${option.code()}_${option.displayName()}" }
} ?: emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class MapFieldValueToUser(
field.value()
}
}
ValueType.MULTI_TEXT -> field.value()?.split(", ")?.map { code ->
field.options().find { it.contains(code) }?.split("_")?.get(1)
}?.joinToString(", ")
else -> field.value()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.dhis2.composetable.model.TableHeaderCell
import org.dhis2.composetable.model.TableHeaderRow
import org.dhis2.composetable.model.TableModel
import org.dhis2.composetable.model.TableRowModel
import org.hisp.dhis.android.core.common.ValueType
import java.util.SortedMap

class TableDataToTableModelMapper(val mapFieldValueToUser: MapFieldValueToUser) {
Expand Down Expand Up @@ -50,6 +51,7 @@ class TableDataToTableModelMapper(val mapFieldValueToUser: MapFieldValueToUser)
mandatory = field.mandatory(),
error = field.error(),
warning = field.warning(),
isMultiText = dataElement.valueType() == ValueType.MULTI_TEXT,
)
}.toMap(),
isLastRow = rowIndex == (tableData.rows()!!.size - 1),
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions compose-table/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ dependencies {
debugImplementation(libs.bundles.table.debugImplementation)
testImplementation(libs.bundles.table.test)
androidTestImplementation(libs.bundles.table.androidTest)
implementation(libs.dhis2.mobile.designsystem)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data class TableCell(
val error: String? = null,
val warning: String? = null,
val legendColor: Int? = null,
val isMultiText: Boolean = false,
) {

fun hasErrorOrWarning() = errorOrWarningMessage() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun ItemValues(
overridenValues: Map<Int, TableCell>,
headerExtraSize: Int,
options: List<String>,
headerLabel: String,
) {
Row(
modifier = Modifier
Expand All @@ -42,6 +43,7 @@ fun ItemValues(
maxLines = maxLines,
headerExtraSize = headerExtraSize,
options = options,
headerLabel = headerLabel,
)
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.dhis2.composetable.ui

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import org.dhis2.composetable.R
import org.dhis2.composetable.model.TableCell
import org.hisp.dhis.mobile.ui.designsystem.component.CheckBoxData
import org.hisp.dhis.mobile.ui.designsystem.component.MultiSelectBottomSheet

@Composable
fun MultiOptionSelector(
options: List<String>,
cell: TableCell,
title: String,
onSave: (String, String) -> Unit,
onDismiss: () -> Unit,
) {
MultiSelectBottomSheet(
items = options.map { option ->
val code = option.split("_")[0]
val label = option.split("_")[1]
CheckBoxData(
uid = code,
checked = cell.value?.contains(label) == true,
enabled = cell.editable,
textInput = label,
)
},
title = title,
noResultsFoundString = stringResource(R.string.no_results_found),
doneButtonText = stringResource(id = R.string.done),
onItemsSelected = { checkBoxes ->
val checkedCodes = checkBoxes
.filter { item -> item.checked }
.joinToString(", ") { it.uid }
val checkedValues = checkBoxes
.filter { item -> item.checked }
.joinToString(", ") { it.textInput?.text.orEmpty() }
onSave(checkedCodes, checkedValues)
},
onDismiss = onDismiss,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ fun TableCell(
maxLines: Int,
headerExtraSize: Int,
options: List<String>,
headerLabel: String,
) {
val localInteraction = LocalInteraction.current
val (dropDownExpanded, setExpanded) = remember { mutableStateOf(false) }
val (showMultiSelector, setShowMultiSelector) = remember { mutableStateOf(false) }

var cellValue by remember {
mutableStateOf<String?>(null)
Expand Down Expand Up @@ -138,7 +140,10 @@ fun TableCell(
.fillMaxHeight()
.clickable(cell.editable) {
when {
options.isNotEmpty() -> setExpanded(true)
options.isNotEmpty() -> when {
cell.isMultiText -> setShowMultiSelector(true)
else -> setExpanded(true)
}
else -> {
localInteraction.onSelectionChange(
TableSelection.CellSelection(
Expand Down Expand Up @@ -195,6 +200,29 @@ fun TableCell(
cellValue = label
},
)
if (showMultiSelector) {
MultiOptionSelector(
options = options,
cell = cell,
title = headerLabel,
onSave = { codes, values ->
localInteraction.onSelectionChange(
TableSelection.CellSelection(
tableId = tableId,
columnIndex = cell.column,
rowIndex = cell.row ?: -1,
globalIndex = 0,
),
)
cellValue = values
localInteraction.onOptionSelected(cell, codes, values)
setShowMultiSelector(false)
},
onDismiss = {
setShowMultiSelector(false)
},
)
}
}

if (cell.mandatory == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fun TableItemRow(
tableModel.tableHeaderModel.hasTotals,
),
options = rowModel.dropDownOptions ?: emptyList(),
headerLabel = rowModel.rowHeader.title,
)
}
if (!rowModel.isLastRow) {
Expand Down

0 comments on commit 2bfc9bc

Please sign in to comment.