Skip to content

Commit

Permalink
feat: [ANDROAPP-6693] multiplatform resources code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmr committed Jan 13, 2025
1 parent 7720c93 commit 549e4e3
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color

/**
* Composable function to display a cell with a legend color.
*
* @param modifier The modifier to be applied to the layout.
* @param legendColor The color of the legend to be displayed.
* @param content The content to be displayed inside the box.
*/
@Composable
internal fun CellLegendBox(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,55 @@ package org.hisp.dhis.mobile.ui.designsystem.component.table.ui
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

sealed class CellStyle {
internal sealed class CellStyle {

/**
* Style for header cells.
*
* @property backgroundColor The background color of the header cell.
* @property textColor The text color of the header cell.
*/
data class HeaderStyle(val backgroundColor: Color, val textColor: Color) : CellStyle()

/**
* Style for cells with borders.
*
* @property backgroundColor The background color of the cell.
* @property borderColor The border color of the cell.
*/
data class CellBorderStyle(val backgroundColor: Color, val borderColor: Color) : CellStyle()

/**
* Returns the background color of the cell.
*
* @return The background color.
*/
fun backgroundColor() = when (this) {
is CellBorderStyle -> backgroundColor
is HeaderStyle -> backgroundColor
}

/**
* Returns the main color of the cell (text or border color).
*
* @return The main color.
*/
fun mainColor() = when (this) {
is CellBorderStyle -> borderColor
is HeaderStyle -> textColor
}
}

/**
* Returns the style for column header cells based on their selection state and index.
*
* @param isSelected Indicates if the column header is selected.
* @param isParentSelected Indicates if the parent column header is selected.
* @param columnIndex The index of the column.
* @return The style for the column header cell.
*/
@Composable
fun styleForColumnHeader(
internal fun styleForColumnHeader(
isSelected: Boolean,
isParentSelected: Boolean,
columnIndex: Int,
Expand All @@ -43,8 +75,16 @@ fun styleForColumnHeader(
)
}

/**
* Returns the style for column header cells based on their selection state and index.
*
* @param isSelected Indicates if the column header is selected.
* @param isParentSelected Indicates if the parent column header is selected.
* @param columnIndex The index of the column.
* @return The style for the column header cell.
*/
@Composable
fun styleForRowHeader(isSelected: Boolean, isOtherRowSelected: Boolean): CellStyle = when {
internal fun styleForRowHeader(isSelected: Boolean, isOtherRowSelected: Boolean): CellStyle = when {
isSelected -> CellStyle.HeaderStyle(
TableTheme.colors.primary,
TableTheme.colors.onPrimary,
Expand All @@ -59,7 +99,19 @@ fun styleForRowHeader(isSelected: Boolean, isOtherRowSelected: Boolean): CellSty
)
}

fun styleForCell(
/**
* Returns the style for table cells based on various states and properties.
*
* @param tableColorProvider A function providing the table colors.
* @param isSelected Indicates if the cell is selected.
* @param isParentSelected Indicates if the parent cell is selected.
* @param hasError Indicates if the cell has an error.
* @param hasWarning Indicates if the cell has a warning.
* @param isEditable Indicates if the cell is editable.
* @param legendColor The color of the legend, if any.
* @return The style for the table cell.
*/
internal fun styleForCell(
tableColorProvider: () -> TableColors,
isSelected: Boolean,
isParentSelected: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.TableTheme.tableS
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.compositions.LocalInteraction
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.compositions.LocalTableResizeActions

/**
* Composable function to display a data table.
*
* @param tableList The list of table models to be displayed.
* @param bottomContent Optional composable content to be displayed at the bottom of the table.
*/
@Composable
fun DataTable(tableList: List<TableModel>, bottomContent: @Composable (() -> Unit)? = null) {
val tableResizeActions = LocalTableResizeActions.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import org.hisp.dhis.mobile.ui.designsystem.component.table.model.DropdownOption

/**
* Composable function to display a dropdown menu with options.
*
* @param expanded Indicates whether the dropdown menu is expanded.
* @param options The list of options to be displayed in the dropdown menu.
* @param onDismiss The callback to be invoked when the dropdown menu is dismissed.
* @param onSelected The callback to be invoked when an option is selected, with the option's code and label.
*/
@Composable
fun DropDownOptions(
internal fun DropDownOptions(
expanded: Boolean,
options: List<DropdownOption>,
onDismiss: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
// todo review dividers

// TODO review dividers
/**
* Composable function to display an extended divider for table rows.
*
* @param tableId The ID of the table.
* @param selected Indicates if the divider is selected.
*/
@Composable
fun ExtendDivider(tableId: String, selected: Boolean) {
internal fun ExtendDivider(tableId: String, selected: Boolean) {
val background = TableTheme.colors.primary
Row(modifier = Modifier.fillMaxWidth()) {
Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.columnI
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.rowIndexHeader
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.tableIdColumnHeader

/**
* Composable function to display a header cell.
*
* @param itemHeaderUiState The state of the header cell.
* @param modifier The modifier to be applied to the cell.
*/
@Composable
internal fun HeaderCell(itemHeaderUiState: ItemColumnHeaderUiState, modifier: Modifier = Modifier) {
internal fun HeaderCell(
itemHeaderUiState: ItemColumnHeaderUiState,
modifier: Modifier = Modifier,
) {
Box(
modifier = modifier
.width(with(LocalDensity.current) { itemHeaderUiState.headerMeasures.width.toDp() })
Expand Down Expand Up @@ -57,7 +66,7 @@ internal fun HeaderCell(itemHeaderUiState: ItemColumnHeaderUiState, modifier: Mo
maxLines = 3,
softWrap = true,
)
// todo ensure new dividers are implemented correctly
// TODO ensure new dividers are implemented correctly
HorizontalDivider(
color = TableTheme.colors.primary,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ import org.hisp.dhis.mobile.ui.designsystem.component.table.model.TableCell
import org.hisp.dhis.mobile.ui.designsystem.component.table.model.TableHeader
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.CELL_TEST_TAG

/**
* Composable function to display item values in a table row.
*
* @param tableId The ID of the table.
* @param horizontalScrollState The state of the horizontal scroll.
* @param maxLines The maximum number of lines to display in each cell.
* @param cellValues A map of column indices to table cells representing the cell values.
* @param overridenValues A map of column indices to table cells representing the overridden cell values.
* @param tableHeaderModel The model representing the table header.
* @param options The list of dropdown options available for the cells.
* @param headerLabel The label for the header.
*/
@Composable
fun ItemValues(
internal fun ItemValues(
tableId: String,
horizontalScrollState: ScrollState,
maxLines: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ import org.hisp.dhis.mobile.ui.designsystem.component.table.model.DropdownOption
import org.hisp.dhis.mobile.ui.designsystem.component.table.model.TableCell
import org.hisp.dhis.mobile.ui.designsystem.resource.provideStringResource

/**
* Composable function to display a multi-option selector.
*
* @param options The list of dropdown options available for selection.
* @param cell The table cell containing the current value and editability state.
* @param title The title of the multi-option selector.
* @param onSave The callback to be invoked when the selected options are saved, with the selected codes and values.
* @param onDismiss The callback to be invoked when the selector is dismissed.
*/
@Composable
fun MultiOptionSelector(
internal fun MultiOptionSelector(
options: List<DropdownOption>,
cell: TableCell,
title: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.TableTheme.tableS
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.compositions.LocalTableResizeActions
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.extensions.fixedStickyHeader

/**
* Composable function to display a table.
*
* @param tableList The list of table models to be displayed.
* @param tableHeaderRow Optional composable function to display the header row of the table.
* @param tableItemRow Optional composable function to display the item row of the table.
* @param verticalResizingView Optional composable function to display the vertical resizing view.
* @param bottomContent Optional composable content to be displayed at the bottom of the table.
*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Table(
internal fun Table(
tableList: List<TableModel>,
tableHeaderRow: @Composable ((index: Int, tableModel: TableModel) -> Unit)? = null,
tableItemRow: @Composable (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
* Composable function to display table actions with a title and action icons.
*
* @param modifier The modifier to be applied to the layout.
* @param title The title of the table actions.
* @param actionIcons A composable function to display the action icons.
*/
@Composable
fun TableActions(modifier: Modifier, title: String, actionIcons: @Composable () -> Unit) {
internal fun TableActions(modifier: Modifier, title: String, actionIcons: @Composable () -> Unit) {
Row(
modifier = modifier,
horizontalArrangement = Arrangement.Absolute.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
// todo verify icon is correct
// TODO verify icon is correct
Icon(
imageVector = Icons.Outlined.TableView,
contentDescription = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.hasErro
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.isBlocked
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.semantics.rowBackground

/**
* Composable function to display a table cell.
*
* @param tableId The ID of the table.
* @param cell The cell to be displayed.
* @param maxLines The maximum number of lines to be displayed in the cell.
* @param headerExtraSize The extra size to be added to the header.
* @param options The list of dropdown options.
* @param headerLabel The label of the header.
*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun TableCell(
internal fun TableCell(
tableId: String,
cell: TableCell,
maxLines: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color

/**
* Data class representing the colors used in the table component.
*
* @property primary The primary color.
* @property primaryLight The light variant of the primary color.
* @property headerText The color of the header text.
* @property headerBackground1 The first background color for the header.
* @property headerBackground2 The second background color for the header.
* @property cellText The color of the cell text.
* @property disabledCellText The color of the text in disabled cells.
* @property disabledCellBackground The background color of disabled cells.
* @property disabledSelectedBackground The background color of selected disabled cells.
* @property errorColor The color used to indicate errors.
* @property warningColor The color used to indicate warnings.
* @property tableBackground The background color of the table.
* @property iconColor The color of icons.
* @property onPrimary The color used for text/icons on primary color.
*/
@Immutable
data class TableColors(
val primary: Color = Color(0xFF2C98F0),
Expand All @@ -21,17 +39,35 @@ data class TableColors(
val iconColor: Color = Color.LightGray,
val onPrimary: Color = Color.White,
) {

/**
* Returns the appropriate cell text color based on error, warning, and editability states.
*
* @param hasError Indicates if the cell has an error.
* @param hasWarning Indicates if the cell has a warning.
* @param isEditable Indicates if the cell is editable.
* @return The color to be used for the cell text.
*/
fun cellTextColor(hasError: Boolean, hasWarning: Boolean, isEditable: Boolean) = when {
hasError -> errorColor
hasWarning -> warningColor
!isEditable -> disabledCellText
else -> cellText
}

/**
* Returns the appropriate color for the mandatory icon based on the cell value state.
*
* @param hasValue Indicates if the cell has a value.
* @return The color to be used for the mandatory icon.
*/
fun cellMandatoryIconColor(hasValue: Boolean) = when (hasValue) {
true -> iconColor
false -> errorColor
}
}

/**
* CompositionLocal to provide [TableColors] throughout the Compose hierarchy.
*/
val LocalTableColors = staticCompositionLocalOf { TableColors() }
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ package org.hisp.dhis.mobile.ui.designsystem.component.table.ui
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf

/**
* Data class representing the configuration settings for the table component.
*
* @property headerActionsEnabled Indicates if header actions are enabled.
* @property editable Indicates if the table cells are editable.
* @property textInputViewMode Indicates if the text input view mode is enabled.
*/
@Immutable
data class TableConfiguration(
val headerActionsEnabled: Boolean = true,
val editable: Boolean = true,
val textInputViewMode: Boolean = true,
)

/**
* CompositionLocal to provide [TableConfiguration] throughout the Compose hierarchy.
*/
val LocalTableConfiguration = staticCompositionLocalOf { TableConfiguration() }
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import androidx.compose.ui.zIndex
import org.hisp.dhis.mobile.ui.designsystem.component.table.model.TableCornerUiState
import org.hisp.dhis.mobile.ui.designsystem.component.table.ui.modifiers.cornerBackground

/**
* Composable function to display the table corner.
*
* @param modifier The modifier to be applied to the layout.
* @param tableCornerUiState The state of the table corner.
* @param tableId The ID of the table.
* @param onClick The action to be executed
*/
@Composable
internal fun TableCorner(
modifier: Modifier = Modifier,
Expand Down
Loading

0 comments on commit 549e4e3

Please sign in to comment.