Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home Screen adjustments and improvements #101

Merged
merged 6 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.cornellappdev.transit.models.Place
import com.cornellappdev.transit.networking.ApiResponse
import com.cornellappdev.transit.ui.theme.DividerGray
import com.cornellappdev.transit.ui.theme.IconGray
Expand All @@ -51,25 +52,22 @@ import com.cornellappdev.transit.ui.viewmodels.HomeViewModel

/**
* Contents of AddFavorites BottomSheet
* @param homeViewModel the homeViewModel used in the app
* @param cancelOnClick The function to run when the cancel button is clicked
* @param onItemClick The function to run when a menu item from a search is clicked
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddFavoritesSearchSheet(
homeViewModel: HomeViewModel,
favoritesViewModel: FavoritesViewModel = hiltViewModel(),
addSearchBarValue: String,
placeQueryResponse: ApiResponse<List<Place>>,
onQueryChange: (String) -> Unit,
onClearChange: () -> Unit,
cancelOnClick: () -> Unit,
onItemClick: (Place) -> Unit,
) {

val addSearchBarValue = homeViewModel.addSearchQuery.collectAsState().value

val placeQueryResponse = homeViewModel.placeQueryFlow.collectAsState().value

var addSearchActive by remember { mutableStateOf(false) }

val favorites = favoritesViewModel.favoritesStops.collectAsState().value

val keyboardController = LocalSoftwareKeyboardController.current


Expand Down Expand Up @@ -125,10 +123,12 @@ fun AddFavoritesSearchSheet(
inputField = {
SearchBarDefaults.InputField(
query = addSearchBarValue,
onQueryChange = { s -> homeViewModel.onAddQueryChange(s) },
onSearch = { addSearchActive = false; homeViewModel.onSearch(it) },
onQueryChange = onQueryChange,
onSearch = {}, // Search occurs automatically when typing
expanded = addSearchActive,
onExpandedChange = { b -> addSearchActive = b },
onExpandedChange = { isExpanded ->
addSearchActive = isExpanded
},
placeholder = { Text(text = "Search for a stop to add") },
leadingIcon = { Icon(Icons.Outlined.Search, "Search", tint = IconGray) },
colors = SearchBarDefaults.inputFieldColors(
Expand All @@ -142,7 +142,7 @@ fun AddFavoritesSearchSheet(
Icon(
Icons.Outlined.Clear,
"Clear",
modifier = Modifier.clickable { homeViewModel.onAddQueryChange("") })
modifier = Modifier.clickable { onClearChange() })
}
},
modifier = Modifier.border(
Expand All @@ -153,43 +153,17 @@ fun AddFavoritesSearchSheet(
)
},
expanded = addSearchActive,
onExpandedChange = { b -> addSearchActive = b },
onExpandedChange = { isExpanded -> addSearchActive = isExpanded },
shape = RoundedCornerShape(size = 8.dp),
colors = SearchBarDefaults.colors(
containerColor = Color.White,
dividerColor = DividerGray,
)
) {
when (placeQueryResponse) {
is ApiResponse.Error -> {
LocationNotFound()
}

ApiResponse.Pending -> {
ProgressCircle()
}

is ApiResponse.Success -> {
if (placeQueryResponse.data.isEmpty()) {
LocationNotFound()
}
LazyColumn {
items(placeQueryResponse.data) {
MenuItem(
type = it.type,
label = it.name,
sublabel = it.subLabel,
onClick = {
if (it !in favorites) {
favoritesViewModel.addFavorite(it)
keyboardController?.hide()
}
})
}
}
}
}

LoadingLocationItems(
placeQueryResponse,
onClick = { onItemClick(it); keyboardController?.hide() }
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.cornellappdev.transit.R
import com.cornellappdev.transit.models.Place
import com.cornellappdev.transit.ui.theme.TransitBlue
import com.cornellappdev.transit.ui.theme.robotoFamily
import com.cornellappdev.transit.ui.viewmodels.FavoritesViewModel
Expand All @@ -32,23 +33,18 @@ import com.google.android.gms.maps.model.LatLng
* Contents of BottomSheet in HomeScreen
* @param editText The text in the edit/done button
* @param editState The state of the lazyRow, whether it's currently being edited or not
* @param data The data the lazyRow contains
* @param onclick The Function to run when the edit/done button is clicked
* @param onEditToggleClick The Function to run when the edit/done button is clicked
*/
@Composable
fun BottomSheetContent(
editText: String,
editState: Boolean,
onclick: () -> Unit,
onEditToggleClick: () -> Unit,
favoritesData: Set<Place>,
itemOnClick: (Place) -> Unit,
addOnClick: () -> Unit,
removeOnClick: () -> Unit,
changeEndLocation: (LocationUIState) -> Unit,
favoritesViewModel: FavoritesViewModel = hiltViewModel(),
navController: NavController
removeOnClick: (Place) -> Unit,
) {

val data = favoritesViewModel.favoritesStops.collectAsState().value.toList()

Column {
Row(
modifier = Modifier
Expand All @@ -70,7 +66,7 @@ fun BottomSheetContent(
Spacer(modifier = Modifier.weight(1f))
Text(
text = editText,
modifier = Modifier.clickable(onClick = onclick),
modifier = Modifier.clickable(onClick = onEditToggleClick),
color = TransitBlue,
textAlign = TextAlign.Right,
fontSize = 14.sp,
Expand All @@ -79,7 +75,7 @@ fun BottomSheetContent(
)
}

LazyRow(modifier = Modifier.padding(bottom = 20.dp)) {
LazyRow(modifier = Modifier.padding(bottom = 12.dp)) {
item {
LocationItem(
image = painterResource(id = R.drawable.ellipse),
Expand All @@ -92,27 +88,16 @@ fun BottomSheetContent(
removeOnClick = {}
)
}
items(data) {
items(favoritesData.toList()) {
LocationItem(
image = painterResource(id = R.drawable.location_icon),
editImage = painterResource(id = R.drawable.location_icon_edit),
label = it.name,
sublabel = "",
editing = editState,
{
changeEndLocation(
LocationUIState.Place(
it.name,
LatLng(
it.latitude,
it.longitude
)
)
)
navController.navigate("route")
},
itemOnClick = { itemOnClick(it) },
addOnClick = {},
removeOnClick = { favoritesViewModel.removeFavorite(it); removeOnClick() },
removeOnClick = { removeOnClick(it) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.cornellappdev.transit.ui.components

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import com.cornellappdev.transit.models.Place
import com.cornellappdev.transit.networking.ApiResponse

/**
* Composable that enumerates all potential outcomes of a search for places API call [searchResult].
* For outcomes with one or more place results, [onClick] is called when clicking on these places.
*/
@Composable
fun LoadingLocationItems(searchResult: ApiResponse<List<Place>>, onClick: (Place) -> Unit) {
when (searchResult) {
is ApiResponse.Error -> {
LocationNotFound()
}

is ApiResponse.Pending -> {
ProgressCircle()
}

is ApiResponse.Success -> {
if (searchResult.data.isEmpty()) {
LocationNotFound()
} else {
LazyColumn {
items(
searchResult.data
) {
MenuItem(
type = it.type,
label = it.name,
sublabel = it.subLabel,
onClick = { onClick(it) }
)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ fun SearchSuggestions(
recents: List<Place>,
onFavoriteAdd: () -> Unit,
onRecentClear: () -> Unit,
changeStartLocation: (LocationUIState) -> Unit,
changeEndLocation: (LocationUIState) -> Unit,
navController: NavController,
onStopPressed: (Place) -> Unit,
onItemClick: (Place) -> Unit,
maxFavorites: Int = 4,
maxRecents: Int = 4
) {
Column(
modifier = Modifier
Expand All @@ -39,26 +38,13 @@ fun SearchSuggestions(
buttonText = "Add",
onClick = onFavoriteAdd
)
favorites.take(minOf(5, favorites.size)).forEach {
favorites.take(maxFavorites).forEach {
MenuItem(
type = it.type,
label = it.name,
sublabel = it.subLabel,
onClick = {
onStopPressed(it)
changeStartLocation(
LocationUIState.CurrentLocation
)
changeEndLocation(
LocationUIState.Place(
it.name,
LatLng(
it.latitude,
it.longitude
)
)
)
navController.navigate("route")
onItemClick(it)
}
)
}
Expand All @@ -68,26 +54,13 @@ fun SearchSuggestions(
buttonText = "Clear",
onClick = onRecentClear
)
recents.forEach {
recents.take(maxRecents).forEach {
MenuItem(
type = it.type,
label = it.name,
sublabel = it.subLabel,
onClick = {
onStopPressed(it)
changeStartLocation(
LocationUIState.CurrentLocation
)
changeEndLocation(
LocationUIState.Place(
it.name,
LatLng(
it.latitude,
it.longitude
)
)
)
navController.navigate("route")
onItemClick(it)
}
)
}
Expand Down
Loading