From 6b55894b1a8503f4d1c1ecd054351443e0c6d39f Mon Sep 17 00:00:00 2001 From: Rafael Chagas Date: Wed, 20 May 2020 16:26:13 +0200 Subject: [PATCH] Completely remove calls for Place Details API Signed-off-by: Rafael Chagas --- .../pingplacepicker/model/SimplePlace.kt | 4 +- .../{NearbyPlace.kt => CustomPlace.kt} | 2 +- .../googlemaps/GoogleMapsRepository.kt | 44 +++++-------------- .../ui/PlaceConfirmDialogFragment.kt | 10 ++++- 4 files changed, 23 insertions(+), 37 deletions(-) rename library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/{NearbyPlace.kt => CustomPlace.kt} (98%) diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/model/SimplePlace.kt b/library/src/main/java/com/rtchagas/pingplacepicker/model/SimplePlace.kt index 1ee8161..7148c94 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/model/SimplePlace.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/model/SimplePlace.kt @@ -17,5 +17,7 @@ data class SimplePlace( @Json(name = "types") val types: List = emptyList(), @Json(name = "vicinity") - val vicinity: String = "" + val vicinity: String = "", + @Json(name = "formatted_address") + val formattedAddress: String = "" ) \ No newline at end of file diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/NearbyPlace.kt b/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/CustomPlace.kt similarity index 98% rename from library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/NearbyPlace.kt rename to library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/CustomPlace.kt index 4a6eda9..bd6c8cf 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/NearbyPlace.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/CustomPlace.kt @@ -7,7 +7,7 @@ import com.google.android.libraries.places.api.model.* import kotlinx.android.parcel.Parcelize @Parcelize -class NearbyPlace( +class CustomPlace( var placeId: String, var placeName: String, var placePhotos: MutableList, diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/GoogleMapsRepository.kt b/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/GoogleMapsRepository.kt index 822247d..b7e2ee7 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/GoogleMapsRepository.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/repository/googlemaps/GoogleMapsRepository.kt @@ -7,7 +7,6 @@ import com.google.android.libraries.places.api.model.PhotoMetadata import com.google.android.libraries.places.api.model.Place import com.google.android.libraries.places.api.model.PlaceLikelihood import com.google.android.libraries.places.api.net.FetchPhotoRequest -import com.google.android.libraries.places.api.net.FetchPlaceRequest import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest import com.google.android.libraries.places.api.net.PlacesClient import com.rtchagas.pingplacepicker.Config @@ -66,9 +65,9 @@ class GoogleMapsRepository constructor( return googleMapsAPI.searchNearby(locationParam, PingPlacePicker.mapsApiKey) .map { searchResult -> - val placeList = mutableListOf() + val placeList = mutableListOf() for (simplePlace in searchResult.results) { - placeList.add(mapToNearbyPlace(simplePlace)) + placeList.add(mapToCustomPlace(simplePlace)) } placeList } @@ -110,39 +109,14 @@ class GoogleMapsRepository constructor( val paramLocation = "${location.latitude},${location.longitude}" return googleMapsAPI.findByLocation(paramLocation, PingPlacePicker.mapsApiKey) - .flatMap { result: SearchResult -> + .map { result: SearchResult -> if (("OK" == result.status) && result.results.isNotEmpty()) { - return@flatMap getPlaceById(result.results[0].placeId) + return@map mapToCustomPlace(result.results[0]) } - return@flatMap Single.just( - PlaceFromCoordinates( - location.latitude, - location.longitude - ) - ) + return@map PlaceFromCoordinates(location.latitude, location.longitude) } } - /** - * Billed according to - * https://developers.google.com/places/android-sdk/usage-and-billing#places-details - */ - private fun getPlaceById(placeId: String): Single { - - // Create the request - val request = FetchPlaceRequest.builder(placeId, getPlaceFields()).build() - - return Single.create { emitter -> - googleClient.fetchPlace(request) - .addOnSuccessListener { - emitter.onSuccess(it.place) - } - .addOnFailureListener { - emitter.tryOnError(it) - } - } - } - /** * These fields are not charged by Google. * https://developers.google.com/places/android-sdk/usage-and-billing#basic-data @@ -159,7 +133,7 @@ class GoogleMapsRepository constructor( ) } - private fun mapToNearbyPlace(place: SimplePlace): NearbyPlace { + private fun mapToCustomPlace(place: SimplePlace): CustomPlace { val photoList = mutableListOf() place.photos.forEach { @@ -180,7 +154,11 @@ class GoogleMapsRepository constructor( val latLng = LatLng(place.geometry.location.lat, place.geometry.location.lng) - return NearbyPlace(place.placeId, place.name, photoList, place.vicinity, typeList, latLng) + val address = + if (place.formattedAddress.isNotEmpty()) place.formattedAddress + else place.vicinity + + return CustomPlace(place.placeId, place.name, photoList, address, typeList, latLng) } /** diff --git a/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlaceConfirmDialogFragment.kt b/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlaceConfirmDialogFragment.kt index 0034e64..15d1a19 100644 --- a/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlaceConfirmDialogFragment.kt +++ b/library/src/main/java/com/rtchagas/pingplacepicker/ui/PlaceConfirmDialogFragment.kt @@ -11,6 +11,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatDialogFragment +import androidx.core.view.isVisible import androidx.lifecycle.Observer import androidx.transition.TransitionManager import com.google.android.libraries.places.api.model.Place @@ -73,7 +74,7 @@ class PlaceConfirmDialogFragment : AppCompatDialogFragment(), PingKoinComponent val builder = AlertDialog.Builder(activity!!) builder.setTitle(R.string.picker_place_confirm) - .setView(getContentView(activity!!)) + .setView(getContentView(requireContext())) .setPositiveButton(android.R.string.ok) { _, _ -> confirmListener?.onPlaceConfirmed(place) dismiss() @@ -92,7 +93,12 @@ class PlaceConfirmDialogFragment : AppCompatDialogFragment(), PingKoinComponent val content = LayoutInflater.from(context) .inflate(R.layout.fragment_dialog_place_confirm, null) - content.tvPlaceName.text = place.name + if (place.name.isNullOrEmpty()) { + content.tvPlaceName.isVisible = false + } else { + content.tvPlaceName.text = place.name + } + content.tvPlaceAddress.text = place.address fetchPlaceMap(content)