From c4a58ca0d62def075687b825481fcc0028a5b26b Mon Sep 17 00:00:00 2001 From: Pablo Date: Thu, 19 Sep 2024 09:59:43 +0200 Subject: [PATCH] Make LocationItem public Signed-off-by: Pablo --- .../component/LocationSearchBar.kt | 89 +++++++++++++------ 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/LocationSearchBar.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/LocationSearchBar.kt index 39f584547..c12ad1f9a 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/LocationSearchBar.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/LocationSearchBar.kt @@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.lazy.LazyColumn -import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.icons.Icons @@ -41,6 +40,8 @@ import androidx.compose.ui.Alignment.Companion.Top import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.style.TextOverflow @@ -216,9 +217,9 @@ private fun LocationSearchBar( when { currentResults.isNotEmpty() -> itemsIndexed(items = currentResults) { index, locationItemModel -> - LocationItem( + SearchResultLocationItem( modifier = Modifier.testTag("LOCATION_ITEM_$index"), - locationItemModel = locationItemModel, + locationItemModel, ) { onLocationSelected(locationItemModel) } @@ -276,22 +277,9 @@ private fun LocationSearchBar( fun LocationItem( modifier: Modifier = Modifier, locationItemModel: LocationItemModel, + icon: @Composable () -> Unit, onClick: () -> Unit, ) { - val icon = when (locationItemModel) { - is LocationItemModel.StoredResult -> Icons.Outlined.WatchLater - is LocationItemModel.SearchResult -> Icons.Outlined.Place - } - val tintedColor = when (locationItemModel) { - is LocationItemModel.StoredResult -> SurfaceColor.Warning - is LocationItemModel.SearchResult -> SurfaceColor.Primary - } - - val bgColor = when (locationItemModel) { - is LocationItemModel.StoredResult -> SurfaceColor.WarningContainer - is LocationItemModel.SearchResult -> SurfaceColor.PrimaryContainer - } - Row( modifier = modifier .fillMaxWidth() @@ -301,18 +289,7 @@ fun LocationItem( horizontalArrangement = spacedBy(Spacing.Spacing16), verticalAlignment = Top, ) { - Box( - modifier = Modifier.size(Spacing.Spacing40) - .clip(Shape.Full) - .background(color = bgColor, shape = Shape.Full), - contentAlignment = Center, - ) { - Icon( - imageVector = icon, - tint = tintedColor, - contentDescription = "location icon", - ) - } + icon() Column(modifier = Modifier.fillMaxWidth()) { Text( @@ -331,6 +308,60 @@ fun LocationItem( } } +@Composable +private fun SearchResultLocationItem( + modifier: Modifier = Modifier, + locationItemModel: LocationItemModel, + onClick: () -> Unit, +) { + val icon = when (locationItemModel) { + is LocationItemModel.StoredResult -> Icons.Outlined.WatchLater + is LocationItemModel.SearchResult -> Icons.Outlined.Place + } + val tintedColor = when (locationItemModel) { + is LocationItemModel.StoredResult -> SurfaceColor.Warning + is LocationItemModel.SearchResult -> SurfaceColor.Primary + } + + val bgColor = when (locationItemModel) { + is LocationItemModel.StoredResult -> SurfaceColor.WarningContainer + is LocationItemModel.SearchResult -> SurfaceColor.PrimaryContainer + } + + LocationItem( + modifier = modifier, + locationItemModel = locationItemModel, + icon = { + LocationItemIcon( + icon = icon, + tintedColor = tintedColor, + bgColor = bgColor, + ) + }, + onClick = onClick, + ) +} + +@Composable +fun LocationItemIcon( + icon: ImageVector, + tintedColor: Color, + bgColor: Color, +) { + Box( + modifier = Modifier.size(Spacing.Spacing40) + .clip(Shape.Full) + .background(color = bgColor, shape = Shape.Full), + contentAlignment = Center, + ) { + Icon( + imageVector = icon, + tint = tintedColor, + contentDescription = "location icon", + ) + } +} + @Composable private fun NoResultsMessage(isSearching: Boolean) { val message = if (!isSearching) {