From 63faeaa1c831e93a1a580a8c61762f7e488b6ab4 Mon Sep 17 00:00:00 2001 From: Nicola Gallazzi Date: Mon, 2 Sep 2024 13:11:29 +0200 Subject: [PATCH] feat: Provide suggestions interactor --- .../places/data/SuggestionsInteractorImpl.kt | 49 +++++++++++++++++++ .../places/domain/SuggestionsInteractor.kt | 14 ++++++ 2 files changed, 63 insertions(+) create mode 100644 places/src/commonMain/kotlin/com/ngallazzi/places/data/SuggestionsInteractorImpl.kt create mode 100644 places/src/commonMain/kotlin/com/ngallazzi/places/domain/SuggestionsInteractor.kt diff --git a/places/src/commonMain/kotlin/com/ngallazzi/places/data/SuggestionsInteractorImpl.kt b/places/src/commonMain/kotlin/com/ngallazzi/places/data/SuggestionsInteractorImpl.kt new file mode 100644 index 0000000..700cf4a --- /dev/null +++ b/places/src/commonMain/kotlin/com/ngallazzi/places/data/SuggestionsInteractorImpl.kt @@ -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> { + 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> { + 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> { + return runCatching { + val response = + placesDataSource.searchAddress(address = search, languageCode) + .getOrThrow() + val predictions = + response.predictions.map { Address(it.placeId, it.structuredFormatting.mainText) } + predictions + } + } +} \ No newline at end of file diff --git a/places/src/commonMain/kotlin/com/ngallazzi/places/domain/SuggestionsInteractor.kt b/places/src/commonMain/kotlin/com/ngallazzi/places/domain/SuggestionsInteractor.kt new file mode 100644 index 0000000..f7b850d --- /dev/null +++ b/places/src/commonMain/kotlin/com/ngallazzi/places/domain/SuggestionsInteractor.kt @@ -0,0 +1,14 @@ +package com.ngallazzi.places.domain + +interface SuggestionsInteractor { + suspend fun getCountrySuggestions(search: String, languageCode: String): Result> + suspend fun getCitySuggestions( + search: String, + languageCode: String + ): Result> + + suspend fun getAddressSuggestions( + search: String, + languageCode: String + ): Result> +} \ No newline at end of file