maps: searching & get google reviews
- Commit
de89fc4b9c974334aa493df94042cad9cd3d379e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-02-25 05:31 UTC (9 months ago)
- Parents
5bbc071
Diff
maps/src/main/java/com/vayunmathur/maps/Reviews.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/Reviews.kt b/maps/src/main/java/com/vayunmathur/maps/Reviews.ktnew file mode 100644index 0000000000..50fe7b2430--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/Reviews.kt@@ -0,0 +1,71 @@+package com.vayunmathur.maps++import io.ktor.client.HttpClient+import io.ktor.client.call.body+import io.ktor.client.engine.cio.CIO+import io.ktor.client.plugins.contentnegotiation.ContentNegotiation+import io.ktor.client.request.post+import io.ktor.client.request.setBody+import io.ktor.http.ContentType+import io.ktor.http.contentType+import io.ktor.serialization.kotlinx.json.json+import kotlinx.serialization.json.Json+import kotlinx.serialization.Serializable++@Serializable+data class PlaceMatchRequest(val name: String, val lat: Double, val lon: Double)++@Serializable+data class PlaceIdResponse(val id: String)++@Serializable+data class PlaceRatingRequest(val id: String)++@Serializable+data class PlaceRatingResponse(val rating: Float, val userRatingCount: Int)++@Serializable+data class FullPlaceInfo(val id: String, val rating: Float, val userRatingCount: Int)++object Reviews {+ private const val BASE_URL = "https://api.vayunmathur.com"++ private val client = HttpClient(CIO) { // Using CIO engine+ install(ContentNegotiation) {+ json(Json {+ ignoreUnknownKeys = true+ encodeDefaults = true+ })+ }+ }++ /**+ * Chained call: 1. Match OSM data to Google ID -> 2. Get Stars from ID+ */+ suspend fun getRatingForOsmLocation(name: String, lat: Double, lon: Double): FullPlaceInfo? {+ return try {+ // Step 1: Get the Google Place ID from your server+ val idResponse: PlaceIdResponse = client.post("$BASE_URL/maps/place_match") {+ contentType(ContentType.Application.Json)+ setBody(PlaceMatchRequest(name, lat, lon))+ }.body()++ val placeId = idResponse.id++ // Step 2: Get the Rating using that ID+ val ratingResponse: PlaceRatingResponse = client.post("$BASE_URL/maps/place_rating") {+ contentType(ContentType.Application.Json)+ setBody(PlaceRatingRequest(placeId))+ }.body()++ FullPlaceInfo(+ id = placeId,+ rating = ratingResponse.rating,+ userRatingCount = ratingResponse.userRatingCount+ )+ } catch (e: Exception) {+ e.printStackTrace()+ null // Handle errors or 404s gracefully+ }+ }+}\ No newline at end of file
maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.ktindex 0eb10cf239..1adfe90751 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt@@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.rememberLazyListState+import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.BottomSheetScaffold import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults@@ -159,6 +160,10 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel, }, false) ) + LaunchedEffect(Unit) {+ if(selectedFeature != null) scaffoldState.bottomSheetState.expand()+ }+ suspend fun hide() { allowProgrammaticHide = true scaffoldState.bottomSheetState.hide()@@ -280,6 +285,20 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel, } } }+ } else {+ val name = if(selectedFeature is SpecificFeature.RoutableFeature) {+ (selectedFeature as SpecificFeature.RoutableFeature).name+ } else {+ "Search..."+ }+ Card(Modifier.padding(16.dp), shape = RoundedCornerShape(12.dp)) {+ ListItem({+ Text(name)+ }, colors = ListItemDefaults.colors(Color.Transparent), modifier = Modifier.clickable {+ val bbox = camera.projection!!.queryVisibleBoundingBox()+ backStack.add(Route.SearchPage(null, bbox.east, bbox.west, bbox.north, bbox.south))+ })+ } } // DOWNLOAD DIALOG
maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.ktindex c2c0cb732f..fcabf0a0a1 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt@@ -24,6 +24,7 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.navigation3.runtime.NavBackStack import com.vayunmathur.library.ui.IconNavigation@@ -35,6 +36,7 @@ import com.vayunmathur.maps.data.AmenityDatabase import com.vayunmathur.maps.data.AmenityEntity import com.vayunmathur.maps.data.OpeningHours import com.vayunmathur.maps.data.SpecificFeature+import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.maplibre.spatialk.geojson.Position @@ -86,9 +88,9 @@ fun SearchPage( placeholder = { Text("Search nearby...") }, modifier = Modifier.fillMaxWidth(), colors = TextFieldDefaults.colors(- focusedContainerColor = androidx.compose.ui.graphics.Color.Transparent,- unfocusedContainerColor = androidx.compose.ui.graphics.Color.Transparent,- disabledContainerColor = androidx.compose.ui.graphics.Color.Transparent,+ focusedContainerColor = Color.Transparent,+ unfocusedContainerColor = Color.Transparent,+ disabledContainerColor = Color.Transparent, ), leadingIcon = { IconSearch() }, singleLine = true@@ -124,20 +126,20 @@ fun SearchPage( }, modifier = Modifier.clickable { scope.launch {- println("CLICKED") val tags = db.tagDao().getTags(amenity.id).associate { it.key to it.value }- println("GOT TAGS") val feature = SpecificFeature.Restaurant(tags["name"] ?: "", tags["phone"], tags["website"], tags["website:menu"], tags["opening_hours"]?.let { OpeningHours.from(it) }, Position(amenity.lon, amenity.lat) )- println("MADE FEATURES") // Dispatch the selected result back to the registry- val f = viewModel.selectedFeature.value as SpecificFeature.Route- viewModel.set(f.copy(waypoints = f.waypoints.mapIndexed { idx2, it ->- if (idx2 == idx) feature else it- }))-- println("DISPATCHED RESULT")+ if(idx != null) {+ val f =+ viewModel.selectedFeature.value as SpecificFeature.Route+ viewModel.set(f.copy(waypoints = f.waypoints.mapIndexed { idx2, it ->+ if (idx2 == idx) feature else it+ }))+ } else {+ viewModel.set(feature)+ } // Pop the backstack to return to the previous screen (the map) backStack.pop() }
maps/src/main/java/com/vayunmathur/maps/ui/components/RestaurantBottomSheet.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/components/RestaurantBottomSheet.kt b/maps/src/main/java/com/vayunmathur/maps/ui/components/RestaurantBottomSheet.ktindex 3099ae71b4..2b047a521e 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/components/RestaurantBottomSheet.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/components/RestaurantBottomSheet.kt@@ -9,16 +9,19 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Button import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.Icon import androidx.compose.material3.ListItem+import androidx.compose.material3.ListItemColors import androidx.compose.material3.ListItemDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember@@ -34,7 +37,9 @@ import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.dp import androidx.core.net.toUri+import com.vayunmathur.maps.FullPlaceInfo import com.vayunmathur.maps.R+import com.vayunmathur.maps.Reviews import com.vayunmathur.maps.data.SpecificFeature import com.vayunmathur.maps.data.timeFormat import kotlinx.datetime.TimeZone@@ -50,17 +55,29 @@ fun goto(context: Context, uri: String) { @Composable fun RestaurantBottomSheet(inactiveNavigation: SpecificFeature.Route?, feature: SpecificFeature.Restaurant, requestDirections: () -> Unit) {+ var reviews by remember { mutableStateOf<FullPlaceInfo?>(null) }+ LaunchedEffect(Unit) {+ reviews = Reviews.getRatingForOsmLocation(feature.name, feature.position.latitude, feature.position.longitude)+ } val context = LocalContext.current Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { Row(verticalAlignment = Alignment.CenterVertically) {- Text(feature.name, style = MaterialTheme.typography.titleLarge, modifier = Modifier.weight(1f))- Button({requestDirections()}) {- if(inactiveNavigation == null) {- Text("Directions")- } else {- Text("Add Stop to Route")+ ListItem({+ Text(+ feature.name,+ style = MaterialTheme.typography.titleLarge,+ )+ }, supportingContent = reviews?.let { reviews ->+ { Text("${reviews.rating} stars | ${reviews.userRatingCount} reviews") }+ }, trailingContent = {+ Button({requestDirections()}) {+ if(inactiveNavigation == null) {+ Text("Directions")+ } else {+ Text("Add Stop to Route")+ } }- }+ }, colors = ListItemDefaults.colors(containerColor = Color.Transparent)) } feature.openingHours?.let { Spacer(Modifier.height(8.dp))