Skip to content

Commit

Permalink
feat: Provide suggestions interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallazzi committed Sep 2, 2024
1 parent f5b4405 commit 63faeaa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ngallazzi.places.data

import com.ngallazzi.places.domain.Address
import com.ngallazzi.places.domain.City
import com.ngallazzi.places.domain.Country
import com.ngallazzi.places.domain.PlacesDataSource
import com.ngallazzi.places.domain.SuggestionsInteractor

internal class SuggestionsInteractorImpl(
private val placesDataSource: PlacesDataSource,
) : SuggestionsInteractor {
override suspend fun getCountrySuggestions(
search: String,
languageCode: String
): Result<List<Country>> {
return runCatching {
val response = placesDataSource.searchCountry(name = search, languageCode).getOrThrow()
val countries = response.predictions.map { Country(it.placeId, it.description) }
countries
}
}

override suspend fun getCitySuggestions(
search: String,
languageCode: String
): Result<List<City>> {
return runCatching {
val response =
placesDataSource.searchCity(name = search, languageCode).getOrThrow()
val cities =
response.predictions.map { City(it.placeId, it.structuredFormatting.mainText) }
cities
}
}

override suspend fun getAddressSuggestions(
search: String,
languageCode: String
): Result<List<Address>> {
return runCatching {
val response =
placesDataSource.searchAddress(address = search, languageCode)
.getOrThrow()
val predictions =
response.predictions.map { Address(it.placeId, it.structuredFormatting.mainText) }
predictions
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ngallazzi.places.domain

interface SuggestionsInteractor {
suspend fun getCountrySuggestions(search: String, languageCode: String): Result<List<Country>>
suspend fun getCitySuggestions(
search: String,
languageCode: String
): Result<List<City>>

suspend fun getAddressSuggestions(
search: String,
languageCode: String
): Result<List<Address>>
}

0 comments on commit 63faeaa

Please sign in to comment.