Skip to content

Commit

Permalink
Search feature (#293)
Browse files Browse the repository at this point in the history
* search feature

* search feature - LocationDescription refactor

---------

Signed-off-by: Dave Craig <[email protected]>
Co-authored-by: Amira <[email protected]>
Co-authored-by: Dave Craig <[email protected]>
  • Loading branch information
3 people authored Dec 31, 2024
1 parent 2ca4d96 commit 111baf7
Show file tree
Hide file tree
Showing 18 changed files with 1,737 additions and 1,152 deletions.
355 changes: 187 additions & 168 deletions app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeIntents.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,129 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SearchBar
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.CollectionInfo
import androidx.compose.ui.semantics.CollectionItemInfo
import androidx.compose.ui.semantics.collectionInfo
import androidx.compose.ui.semantics.collectionItemInfo
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import org.scottishtecharmy.soundscape.R
import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainSearchBar(
searchText: String,
isSearching: Boolean,
itemList: List<SearchItem>,
itemList: List<LocationDescription>,
onSearchTextChange: (String) -> Unit,
onToggleSearch: () -> Unit,
onItemClick: (SearchItem) -> Unit
onItemClick: (LocationDescription) -> Unit,
) {
val containerColor = Color.White
SearchBar(
query = searchText,
onQueryChange = onSearchTextChange,
onSearch = onSearchTextChange,
active = isSearching,
modifier =
Modifier
.fillMaxWidth()
.then(
if (!isSearching) {
Modifier.padding(horizontal = 16.dp)
} else {
Modifier.padding(horizontal = 0.dp)
},
),
shape = RoundedCornerShape(8.dp),
onActiveChange = { onToggleSearch() },
colors = SearchBarDefaults.colors(
containerColor = containerColor,
inputFieldColors = TextFieldDefaults.colors(
focusedTextColor = Color.Black,
focusedContainerColor = containerColor,
unfocusedContainerColor = containerColor,
disabledContainerColor = containerColor,
)
),
leadingIcon = {
if (!isSearching) {
Icon(
Icons.Rounded.Search,
null,
tint = Color.Gray
)
} else {
IconButton(
onClick = { onToggleSearch() },
) {
Icon(
Icons.AutoMirrored.Rounded.ArrowBack,
contentDescription = "Cancel search",
tint = Color.Gray
)
}
}
},
placeholder = {
Text(
text = "Search destination",
color = Color.Gray
colors = SearchBarDefaults.colors(containerColor = Color.White),
inputField = {
SearchBarDefaults.InputField(
query = searchText,
onQueryChange = onSearchTextChange,
onSearch = onSearchTextChange,
expanded = isSearching,
onExpandedChange = { onToggleSearch() },
placeholder = { Text(stringResource(id = R.string.search_hint_input)) },
leadingIcon = {
when {
!isSearching -> {
Icon(
imageVector = Icons.Rounded.Search,
contentDescription = null,
tint = Color.Gray,
)
}

else -> {
IconButton(
onClick = { onToggleSearch() },
) {
Icon(
Icons.AutoMirrored.Rounded.ArrowBack,
contentDescription =
stringResource(R.string.cancel_search_contentDescription),
tint = Color.Gray,
)
}
}
}
},
colors = SearchBarDefaults.inputFieldColors(focusedTextColor = Color.Black),
)
},
modifier = Modifier
.fillMaxWidth()
.then(
if (!isSearching) {
Modifier.padding(horizontal = 16.dp)
} else {
Modifier.padding(horizontal = 0.dp)
}
)
expanded = isSearching,
onExpandedChange = { onToggleSearch() },
) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
modifier =
Modifier
.semantics {
this.collectionInfo =
CollectionInfo(
rowCount = itemList.size, // Total number of items
columnCount = 1, // Single-column list
)
}.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
) {
LazyColumn(modifier = Modifier.padding(top = 16.dp)) {
items(itemList) { item ->
SearchItemButton(
item = item,
onClick = {
onItemClick(item)
},
modifier = Modifier
)
itemsIndexed(itemList) { index, item ->
Column {
SearchItemButton(
item = item,
onClick = {
onItemClick(item)
onToggleSearch()
},
modifier =
Modifier.semantics {
this.collectionItemInfo =
CollectionItemInfo(
rowSpan = 1,
columnSpan = 1,
rowIndex = index,
columnIndex = 0,
)
},
)
HorizontalDivider(color = Color.White)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,66 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.scottishtecharmy.soundscape.R
import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription
import org.scottishtecharmy.soundscape.ui.theme.Foreground2
import org.scottishtecharmy.soundscape.ui.theme.IntroductionTheme


data class SearchItem(
val text: String,
val label: String,
)
import org.scottishtecharmy.soundscape.ui.theme.PaleBlue
import org.scottishtecharmy.soundscape.utils.buildAddressFormat

@Composable
fun SearchItemButton(item: SearchItem, onClick: () -> Unit, modifier: Modifier,) {
fun SearchItemButton(
item: LocationDescription,
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
Button(
onClick = onClick,
shape = RoundedCornerShape(0),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.tertiary,
),
colors =
ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.tertiary,
),
) {
Row(
modifier = Modifier
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
modifier = modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
Icons.Rounded.LocationOn,
contentDescription = "Choose destination",
tint = Color.White
contentDescription = null,
tint = Color.White,
)
Column(modifier = Modifier.padding(start = 18.dp)) {
Text(
item.text,
fontWeight = FontWeight(400),
fontSize = 18.sp,
color = Color.White,
)
Text(
item.label,
color = Foreground2,
fontWeight = FontWeight(350),
)
Column(
modifier = Modifier.padding(start = 18.dp),
verticalArrangement = Arrangement.spacedBy(5.dp),
) {
item.adressName?.let {
Text(
text = it,
fontWeight = FontWeight(700),
fontSize = 22.sp,
color = Color.White,
)
}
item.distance?.let {
Text(
text = it,
color = Foreground2,
fontWeight = FontWeight(450),
)
}
item.buildAddressFormat()?.let {
Text(
text = it,
fontWeight = FontWeight(400),
fontSize = 18.sp,
color = PaleBlue,
)
}
}
}
}
Expand All @@ -74,15 +88,25 @@ fun SearchItemButton(item: SearchItem, onClick: () -> Unit, modifier: Modifier,)
@Composable
fun PreviewSearchItemButton() {
IntroductionTheme {
Column(modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
Column(
modifier =
Modifier
.fillMaxWidth()
.fillMaxHeight(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
)
{
val test = SearchItem("Bristol", "1")
verticalArrangement = Arrangement.Center,
) {
val test =
LocationDescription(
adressName = "Bristol",
streetNumberAndName = "18 Street",
postcodeAndLocality = "59000 Lille",
distance = "17 Km",
country = "France",
latitude = 9.55,
longitude = 8.00,
)
SearchItemButton(test, onClick = {}, Modifier.width(200.dp))
}
}
}
}
Loading

0 comments on commit 111baf7

Please sign in to comment.