2025-04-14 13:35:26 +10:00
|
|
|
package moe.lava.banksia.ui
|
|
|
|
|
|
|
|
|
|
import androidx.compose.animation.core.animateDpAsState
|
|
|
|
|
import androidx.compose.foundation.clickable
|
|
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
|
|
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.material.icons.Icons
|
|
|
|
|
import androidx.compose.material.icons.filled.Clear
|
|
|
|
|
import androidx.compose.material.icons.filled.Search
|
|
|
|
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
|
|
|
import androidx.compose.material3.Icon
|
2025-04-14 21:07:05 +10:00
|
|
|
import androidx.compose.material3.ListItem
|
|
|
|
|
import androidx.compose.material3.ListItemDefaults
|
2025-04-14 13:35:26 +10:00
|
|
|
import androidx.compose.material3.MaterialTheme
|
|
|
|
|
import androidx.compose.material3.SearchBar
|
|
|
|
|
import androidx.compose.material3.SearchBarDefaults
|
2025-04-14 21:07:05 +10:00
|
|
|
import androidx.compose.material3.Text
|
2025-04-14 13:35:26 +10:00
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
|
|
|
import androidx.compose.runtime.getValue
|
2025-04-14 21:07:05 +10:00
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
|
import androidx.compose.runtime.remember
|
|
|
|
|
import androidx.compose.runtime.setValue
|
2025-04-14 13:35:26 +10:00
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
|
import androidx.compose.ui.Modifier
|
2025-04-14 21:07:05 +10:00
|
|
|
import androidx.compose.ui.graphics.Color
|
2025-04-14 13:35:26 +10:00
|
|
|
import androidx.compose.ui.unit.dp
|
2025-04-14 21:07:05 +10:00
|
|
|
import moe.lava.banksia.api.ptv.PtvService
|
2025-04-15 14:19:17 +10:00
|
|
|
import moe.lava.banksia.api.ptv.structures.ComposableIcon
|
2025-04-29 20:40:45 +10:00
|
|
|
import moe.lava.banksia.api.ptv.structures.PtvRoute
|
2025-04-14 13:35:26 +10:00
|
|
|
|
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
|
|
|
@Composable
|
|
|
|
|
fun Searcher(
|
2025-04-14 21:07:05 +10:00
|
|
|
ptvService: PtvService,
|
2025-04-14 13:35:26 +10:00
|
|
|
expanded: Boolean,
|
|
|
|
|
onExpandedChange: (Boolean) -> Unit,
|
|
|
|
|
text: String,
|
|
|
|
|
onTextChange: (String) -> Unit,
|
2025-04-29 20:40:45 +10:00
|
|
|
onRouteChange: (PtvRoute) -> Unit,
|
2025-04-14 13:35:26 +10:00
|
|
|
) {
|
|
|
|
|
val animatedPadding by animateDpAsState(
|
|
|
|
|
if (expanded) {
|
|
|
|
|
0.dp
|
|
|
|
|
} else {
|
|
|
|
|
20.dp
|
|
|
|
|
},
|
|
|
|
|
label = "padding"
|
|
|
|
|
)
|
2025-04-29 20:40:45 +10:00
|
|
|
var routes by remember { mutableStateOf(listOf<PtvRoute>()) }
|
2025-04-14 13:35:26 +10:00
|
|
|
Box(modifier = Modifier.fillMaxSize()) {
|
|
|
|
|
LaunchedEffect(Unit) {
|
2025-04-14 21:07:05 +10:00
|
|
|
val localRoutes = ptvService.routes()
|
|
|
|
|
routes = localRoutes.sortedWith(
|
|
|
|
|
compareBy(
|
2025-04-15 14:09:46 +10:00
|
|
|
{ it.gtfsSubType()?.ordinal },
|
2025-04-14 21:07:05 +10:00
|
|
|
{ it.routeNumber.toIntOrNull() },
|
|
|
|
|
{ it.routeName }
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-04-14 13:35:26 +10:00
|
|
|
}
|
|
|
|
|
SearchBar(
|
|
|
|
|
colors = SearchBarDefaults.colors(containerColor = MaterialTheme.colorScheme.surfaceContainer),
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.align(Alignment.TopCenter)
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
|
.padding(horizontal = animatedPadding),
|
|
|
|
|
inputField = {
|
|
|
|
|
SearchBarDefaults.InputField(
|
|
|
|
|
query = text,
|
|
|
|
|
onQueryChange = onTextChange,
|
|
|
|
|
onSearch = {},
|
|
|
|
|
expanded = expanded,
|
|
|
|
|
onExpandedChange = onExpandedChange,
|
|
|
|
|
leadingIcon = { Icon(Icons.Default.Search, null) },
|
|
|
|
|
trailingIcon = {
|
|
|
|
|
if (expanded && text.isNotEmpty())
|
|
|
|
|
Icon(
|
|
|
|
|
imageVector = Icons.Default.Clear,
|
|
|
|
|
contentDescription = null,
|
|
|
|
|
modifier = Modifier.clickable { onTextChange("") }
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
expanded = expanded,
|
|
|
|
|
onExpandedChange = onExpandedChange,
|
|
|
|
|
) {
|
|
|
|
|
LazyColumn(modifier = Modifier.fillMaxWidth()) {
|
2025-04-14 21:07:05 +10:00
|
|
|
for (route in routes) {
|
|
|
|
|
if (!route.routeNumber.contains(text) &&
|
|
|
|
|
!route.routeName.lowercase().contains(text.lowercase()))
|
2025-04-14 13:35:26 +10:00
|
|
|
continue
|
|
|
|
|
item {
|
|
|
|
|
ListItem(
|
2025-04-14 21:07:05 +10:00
|
|
|
headlineContent = { Text(route.routeNumber.ifEmpty { route.routeName }) },
|
2025-04-14 13:35:26 +10:00
|
|
|
supportingContent = {
|
2025-04-14 21:07:05 +10:00
|
|
|
if (route.routeNumber.isNotEmpty()) {
|
|
|
|
|
Text(route.routeName)
|
2025-04-14 13:35:26 +10:00
|
|
|
}
|
|
|
|
|
},
|
2025-04-15 14:09:46 +10:00
|
|
|
leadingContent = { route.routeType.ComposableIcon() },
|
2025-04-14 13:35:26 +10:00
|
|
|
colors = ListItemDefaults.colors(containerColor = Color.Transparent),
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
|
.padding(horizontal = 16.dp, vertical = 4.dp)
|
|
|
|
|
.clickable {
|
2025-04-15 17:25:47 +10:00
|
|
|
onTextChange(route.getShortFullName())
|
2025-04-14 21:07:05 +10:00
|
|
|
onExpandedChange(false)
|
|
|
|
|
onRouteChange(route)
|
2025-04-14 13:35:26 +10:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-04-14 21:07:05 +10:00
|
|
|
}
|
2025-04-14 13:35:26 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-14 21:53:07 +10:00
|
|
|
}
|