Skip to content

Commit

Permalink
implement place search
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Aug 11, 2024
1 parent a6373d5 commit 063f4bd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.car2go.maps.model.LatLng
import net.vonforst.evmap.R
import net.vonforst.evmap.autocomplete.PlaceWithBounds
import net.vonforst.evmap.location.FusionEngine
import net.vonforst.evmap.location.LocationEngine
import net.vonforst.evmap.location.Priority
Expand Down Expand Up @@ -190,7 +191,7 @@ class EVMapSession(val cas: CarAppService) : Session(), DefaultLifecycleObserver
val lon = it.getQueryParameter("longitude")?.toDouble()
val name = it.getQueryParameter("name")
if (lat != null && lon != null) {
prefs.placeSearchResultAndroidAuto = LatLng(lat, lon)
prefs.placeSearchResultAndroidAuto = PlaceWithBounds(LatLng(lat, lon), null)
prefs.placeSearchResultAndroidAutoName = name ?: "%.4f,%.4f".format(lat, lon)
return null
} else if (name != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class LegacyMapScreen(ctx: CarContext, val session: EVMapSession) :
val location = location ?: return

val searchLocation =
prefs.placeSearchResultAndroidAuto ?: LatLng.fromLocation(location)
prefs.placeSearchResultAndroidAuto?.latLng ?: LatLng.fromLocation(location)
this.searchLocation = searchLocation

updateCoroutine = lifecycleScope.launch {
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/net/vonforst/evmap/auto/MapScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ class MapScreen(ctx: CarContext, val session: EVMapSession) :
if (prefs.placeSearchResultAndroidAuto != null) {
prefs.placeSearchResultAndroidAutoName = null
prefs.placeSearchResultAndroidAuto = null
chargers = null
loadChargers()
markerManager?.searchResult = null
invalidate()
} else {
screenManager.pushForResult(
PlaceSearchScreen(
Expand Down Expand Up @@ -595,6 +595,7 @@ class MapScreen(ctx: CarContext, val session: EVMapSession) :
)
)
}
searchResult = prefs.placeSearchResultAndroidAuto
}

map.setMyLocationEnabled(true)
Expand All @@ -611,7 +612,16 @@ class MapScreen(ctx: CarContext, val session: EVMapSession) :
if (mode == Configuration.UI_MODE_NIGHT_YES) AnyMap.Style.DARK else AnyMap.Style.NORMAL
)

if (prefs.currentMapMyLocationEnabled) {
prefs.placeSearchResultAndroidAuto?.let { place ->
// move to the location of the search result
myLocationEnabled = false
markerManager?.searchResult = place
if (place.viewport != null) {
map.moveCamera(map.cameraUpdateFactory.newLatLngBounds(place.viewport, 0))
} else {
map.moveCamera(map.cameraUpdateFactory.newLatLngZoom(place.latLng, 12f))
}
} ?: if (prefs.currentMapMyLocationEnabled) {
enableLocation()
} else {
// use position saved in preferences, fall back to default (Europe)
Expand Down
23 changes: 19 additions & 4 deletions app/src/main/java/net/vonforst/evmap/auto/PlaceSearchScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,34 @@ import androidx.car.app.annotations.ExperimentalCarApi
import androidx.car.app.constraints.ConstraintManager
import androidx.car.app.hardware.CarHardwareManager
import androidx.car.app.hardware.info.EnergyLevel
import androidx.car.app.model.*
import androidx.car.app.model.Action
import androidx.car.app.model.CarColor
import androidx.car.app.model.CarIcon
import androidx.car.app.model.DistanceSpan
import androidx.car.app.model.ItemList
import androidx.car.app.model.Row
import androidx.car.app.model.SearchTemplate
import androidx.car.app.model.Template
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.car2go.maps.model.LatLng
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.vonforst.evmap.BuildConfig
import net.vonforst.evmap.R
import net.vonforst.evmap.adapter.iconForPlaceType
import net.vonforst.evmap.adapter.isSpecialPlace
import net.vonforst.evmap.autocomplete.*
import net.vonforst.evmap.autocomplete.ApiUnavailableException
import net.vonforst.evmap.autocomplete.AutocompletePlace
import net.vonforst.evmap.autocomplete.AutocompleteProvider
import net.vonforst.evmap.autocomplete.PlaceWithBounds
import net.vonforst.evmap.autocomplete.getAutocompleteProviders
import net.vonforst.evmap.storage.AppDatabase
import net.vonforst.evmap.storage.PreferenceDataSource
import net.vonforst.evmap.storage.RecentAutocompletePlace
Expand Down Expand Up @@ -117,7 +132,7 @@ class PlaceSearchScreen(
setOnClickListener {
lifecycleScope.launch {
val placeDetails = getDetails(place.id) ?: return@launch
prefs.placeSearchResultAndroidAuto = placeDetails.latLng
prefs.placeSearchResultAndroidAuto = placeDetails
prefs.placeSearchResultAndroidAutoName =
place.primaryText.toString()
screenManager.popTo(MapScreen.MARKER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.content.SharedPreferences.Editor
import androidx.preference.PreferenceManager
import com.car2go.maps.AnyMap
import com.car2go.maps.model.LatLng
import com.car2go.maps.model.LatLngBounds
import net.vonforst.evmap.R
import net.vonforst.evmap.autocomplete.PlaceWithBounds
import net.vonforst.evmap.model.FILTERS_CUSTOM
import net.vonforst.evmap.model.FILTERS_DISABLED
import java.time.Instant
Expand Down Expand Up @@ -250,10 +252,16 @@ class PreferenceDataSource(val context: Context) {
.apply()
}

var placeSearchResultAndroidAuto: LatLng?
get() = sp.getLatLng("place_search_result_android_auto")
var placeSearchResultAndroidAuto: PlaceWithBounds?
get() {
val latLng = sp.getLatLng("place_search_result_android_auto")
val bounds = sp.getLatLngBounds("place_search_result_android_auto_viewport")
return latLng?.let { PlaceWithBounds(latLng, bounds) }
}
set(value) {
sp.edit().putLatLng("place_search_result_android_auto", value).apply()
sp.edit().putLatLng("place_search_result_android_auto", value?.latLng).apply()
sp.edit().putLatLngBounds("place_search_result_android_auto_viewport", value?.viewport)
.apply()
}

var placeSearchResultAndroidAutoName: String?
Expand Down Expand Up @@ -315,7 +323,7 @@ class PreferenceDataSource(val context: Context) {
}

fun SharedPreferences.getLatLng(key: String): LatLng? =
if (contains("${key}_lat") && contains("${key}_lng")) {
if (containsLatLng(key)) {
LatLng(
Double.fromBits(getLong("${key}_lat", 0L)),
Double.fromBits(getLong("${key}_lng", 0L))
Expand All @@ -332,3 +340,23 @@ fun Editor.putLatLng(key: String, value: LatLng?): Editor {
}
return this
}

fun SharedPreferences.containsLatLng(key: String) = contains("${key}_lat") && contains("${key}_lng")

fun SharedPreferences.getLatLngBounds(key: String): LatLngBounds? =
if (containsLatLng("${key}_sw") && containsLatLng("${key}_ne")) {
LatLngBounds(
getLatLng("${key}_sw"), getLatLng("${key}_ne")
)
} else null

fun Editor.putLatLngBounds(key: String, value: LatLngBounds?): Editor {
if (value == null) {
putLatLng("${key}_sw", null)
putLatLng("${key}_ne", null)
} else {
putLatLng("${key}_sw", value.southwest)
putLatLng("${key}_ne", value.northeast)
}
return this
}

0 comments on commit 063f4bd

Please sign in to comment.