-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from trafi/location-search
Android location search module
- Loading branch information
Showing
10 changed files
with
256 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'kotlin-android' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
|
||
defaultConfig { | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(":core") | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'com.squareup.okhttp3:okhttp:4.9.0' | ||
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | ||
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' | ||
implementation 'com.squareup.moshi:moshi:1.10.0' | ||
implementation 'com.squareup.moshi:moshi-kotlin:1.10.0' | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="com.trafi.locations" /> |
76 changes: 76 additions & 0 deletions
76
android/locations/src/main/java/com/trafi/locations/LocationsApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.trafi.locations | ||
|
||
import android.util.Log | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import com.trafi.core.ApiResult | ||
import com.trafi.core.android.model.AutoCompleteLocation | ||
import com.trafi.core.android.model.LatLng | ||
import com.trafi.core.android.model.Location | ||
import okhttp3.OkHttpClient | ||
import retrofit2.HttpException | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
|
||
class LocationsApi(baseUrl: String, apiKey: String, private val regionId: String) { | ||
|
||
private val moshi = Moshi.Builder() | ||
.add(KotlinJsonAdapterFactory()) | ||
.build() | ||
private val okhttp = OkHttpClient.Builder() | ||
.addInterceptor { chain -> | ||
val request = chain.request().newBuilder() | ||
.addHeader("accept-language", "en") // required for v1/autocomplete | ||
.addHeader("x-api-key", apiKey) | ||
.build() | ||
chain.proceed(request) | ||
} | ||
.build() | ||
private val service: LocationsService = Retrofit.Builder() | ||
.client(okhttp) | ||
.addConverterFactory(MoshiConverterFactory.create(moshi)) | ||
.baseUrl(baseUrl) | ||
.build() | ||
.create(LocationsService::class.java) | ||
|
||
suspend fun search( | ||
query: String, | ||
coordinate: LatLng? = null | ||
): ApiResult<List<AutoCompleteLocation>> = try { | ||
val result = service.search( | ||
query = query, | ||
regionId = regionId, | ||
lat = coordinate?.lat, | ||
lng = coordinate?.lng, | ||
) | ||
ApiResult.Success(result.locations) | ||
} catch (e: Throwable) { | ||
e.logError() | ||
ApiResult.Failure(e) | ||
} | ||
|
||
suspend fun resolveLocation(location: AutoCompleteLocation): ApiResult<Location> = try { | ||
location.toLocation()?.let { ApiResult.Success(it) } | ||
?: service.resolveCoordinate(location.id).toLocation()?.let { ApiResult.Success(it) } | ||
?: ApiResult.Failure(IllegalArgumentException("Failed to resolve coordinate for location id ${location.id}")) | ||
} catch (e: Throwable) { | ||
e.logError() | ||
ApiResult.Failure(e) | ||
} | ||
|
||
suspend fun resolveAddress(coordinate: LatLng): ApiResult<String?> = try { | ||
val result = service.reverseGeocode(coordinate.lat, coordinate.lng) | ||
ApiResult.Success(result.address) | ||
} catch (e: Throwable) { | ||
e.logError() | ||
ApiResult.Failure(e) | ||
} | ||
} | ||
|
||
private fun Throwable.logError() = (this as? HttpException)?.let { | ||
Log.e("LocationsApi", response()?.errorBody()?.string().orEmpty()) | ||
} | ||
|
||
private fun AutoCompleteLocation.toLocation(): Location? = coordinate?.let { coordinate -> | ||
Location(coordinate = coordinate, name = name, address = address) | ||
} |
31 changes: 31 additions & 0 deletions
31
android/locations/src/main/java/com/trafi/locations/LocationsService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.trafi.locations | ||
|
||
import com.trafi.core.android.model.AutoCompleteLocation | ||
import com.trafi.core.android.model.AutoCompleteLocations | ||
import com.trafi.core.android.model.ReverseGeocodeResponse | ||
import com.trafi.core.android.model.RoutesResult | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
import retrofit2.http.Query | ||
|
||
internal interface LocationsService { | ||
@GET("v1/autocomplete") | ||
suspend fun search( | ||
@Query("q") query: String, | ||
@Query("regionId") regionId: String, | ||
@Query("subregionId") subregionId: String? = null, | ||
@Query("lat") lat: Double? = null, | ||
@Query("lng") lng: Double? = null, | ||
): AutoCompleteLocations | ||
|
||
@GET("v1/autocomplete/id/{locationId}") | ||
suspend fun resolveCoordinate( | ||
@Path("locationId") locationId: String, | ||
): AutoCompleteLocation | ||
|
||
@GET("v1/location/reversegeocode") | ||
suspend fun reverseGeocode( | ||
@Query("lat") lat: Double, | ||
@Query("lng") lng: Double | ||
): ReverseGeocodeResponse | ||
} |
Oops, something went wrong.