Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 70cf8bf4 authored by Ellen Poe's avatar Ellen Poe
Browse files

feat: allow users to re-run search if there are no results

parent 3dbb2571
Loading
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -32,8 +32,8 @@ abstract class GeocodingService(private val locationRepository: LocationReposito
     * @param focusPoint Optional focus point for viewport biasing
     * @return Place objects
     */
    suspend fun geocode(query: String, focusPoint: LatLng? = null): List<Place> {
        return convertResultsToPlaces(geocodeRaw(query, focusPoint))
    suspend fun geocode(query: String, focusPoint: LatLng? = null, autocomplete: Boolean = true): List<Place> {
        return convertResultsToPlaces(geocodeRaw(query, focusPoint, autocomplete))
    }

    /**
@@ -46,6 +46,13 @@ abstract class GeocodingService(private val locationRepository: LocationReposito
        return convertResultsToPlaces(reverseGeocodeRaw(latitude, longitude))
    }

    /**
     * Query whether the is service has separate autocomplete behavior.
     * @return true if this geocoding service has separate behavior for autocomplete and "normal"
     * full-text search. false otherwise.
     */
    abstract fun hasSeparateAutocomplete(): Boolean

    /**
     * Find nearby places around a given point, returning Place objects.
     * @param latitude The latitude coordinate
@@ -76,7 +83,8 @@ abstract class GeocodingService(private val locationRepository: LocationReposito
     */
    abstract suspend fun geocodeRaw(
        query: String,
        focusPoint: LatLng? = null
        focusPoint: LatLng? = null,
        autocomplete: Boolean
    ): List<GeocodeResult>

    /**
+11 −3
Original line number Diff line number Diff line
@@ -30,11 +30,11 @@ class MultiplexedGeocodingService(
    locationRepository: LocationRepository,
) : GeocodingService(locationRepository) {

    override suspend fun geocodeRaw(query: String, focusPoint: LatLng?): List<GeocodeResult> {
    override suspend fun geocodeRaw(query: String, focusPoint: LatLng?, autocomplete: Boolean): List<GeocodeResult> {
        return if (appPreferenceRepository.offlineMode.value) {
            offlineGeocodingService.geocodeRaw(query, focusPoint)
            offlineGeocodingService.geocodeRaw(query, focusPoint, autocomplete)
        } else {
            onlineGeocodingService.geocodeRaw(query, focusPoint)
            onlineGeocodingService.geocodeRaw(query, focusPoint, autocomplete)
        }
    }

@@ -60,4 +60,12 @@ class MultiplexedGeocodingService(
            onlineGeocodingService.nearbyRaw(latitude, longitude, selectedCategories)
        }
    }

    override fun hasSeparateAutocomplete(): Boolean {
        return if (appPreferenceRepository.offlineMode.value) {
            offlineGeocodingService.hasSeparateAutocomplete()
        } else {
            onlineGeocodingService.hasSeparateAutocomplete()
        }
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ class OfflineGeocodingService(
    private val geocoderDir = File(context.filesDir, "geocoder").apply { mkdirs() }
    private val airmailIndex = newAirmailIndex("en", geocoderDir.absolutePath)

    override suspend fun geocodeRaw(query: String, focusPoint: LatLng?): List<GeocodeResult> {
    override suspend fun geocodeRaw(query: String, focusPoint: LatLng?, autocomplete: Boolean): List<GeocodeResult> {
        try {
            val results = airmailIndex.searchPhrase(query)
            val geocodeResults = results.map { poi ->
@@ -132,6 +132,10 @@ class OfflineGeocodingService(
        )
    }

    override fun hasSeparateAutocomplete(): Boolean {
        return false
    }

    companion object {
        const val TAG = "OfflineGeocodingService"
    }
+11 −2
Original line number Diff line number Diff line
@@ -58,11 +58,16 @@ class PeliasGeocodingService(
        install(Logging)
    }

    override suspend fun geocodeRaw(query: String, focusPoint: LatLng?): List<GeocodeResult> {
    override suspend fun geocodeRaw(query: String, focusPoint: LatLng?, autocomplete: Boolean): List<GeocodeResult> {
        try {
            Log.d(TAG, "Geocoding query: $query, focusPoint: $focusPoint")
            val config = appPreferenceRepository.peliasApiConfig.value
            val response = client.get("${config.baseUrl}/autocomplete") {
            val endpoint = if (autocomplete) {
                "${config.baseUrl}/autocomplete"
            } else {
                "${config.baseUrl}/search"
            }
            val response = client.get(endpoint) {
                parameter("text", query)
                parameter("size", "10")
                config.apiKey?.let { parameter("api_key", it) }
@@ -200,4 +205,8 @@ class PeliasGeocodingService(
            null
        }
    }

    override fun hasSeparateAutocomplete(): Boolean {
        return true
    }
}
+8 −2
Original line number Diff line number Diff line
@@ -64,6 +64,8 @@ abstract class BaseSearchViewModel(
    var searchError by mutableStateOf<String?>(null)
        protected set

    val expandedResultsAvailable: Boolean get() = geocodingService.hasSeparateAutocomplete()

    init {
        // Set up debounced search
        searchQueryFlow
@@ -93,14 +95,14 @@ abstract class BaseSearchViewModel(
     * Performs the actual search operation
     * Can be overridden by subclasses to provide custom focus point logic
     */
    protected open fun performSearch(query: String) {
    protected open fun performSearch(query: String, autoComplete: Boolean = true) {
        viewModelScope.launch {
            isSearching = true
            searchError = null
            try {
                // Get focus point for viewport biasing - subclasses can override this
                val focusPoint = getSearchFocusPoint()
                geocodeResults.value = geocodingService.geocode(query, focusPoint)
                geocodeResults.value = geocodingService.geocode(query, focusPoint, autoComplete)
                isSearching = false
            } catch (e: Exception) {
                // Handle error
@@ -135,4 +137,8 @@ abstract class BaseSearchViewModel(
    open fun onPlaceSelected(place: Place) {
        addRecentSearch(place)
    }

    fun rerunWithoutAutocomplete() {
        performSearch(searchQuery, autoComplete = false)
    }
}
 No newline at end of file
Loading