Skip to content

Commit

Permalink
Completely remove calls for Place Details API
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Chagas <[email protected]>
  • Loading branch information
rtchagas committed May 20, 2020
1 parent 5979a44 commit 6b55894
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ data class SimplePlace(
@Json(name = "types")
val types: List<String> = emptyList(),
@Json(name = "vicinity")
val vicinity: String = ""
val vicinity: String = "",
@Json(name = "formatted_address")
val formattedAddress: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhotoMetadata>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,9 +65,9 @@ class GoogleMapsRepository constructor(

return googleMapsAPI.searchNearby(locationParam, PingPlacePicker.mapsApiKey)
.map { searchResult ->
val placeList = mutableListOf<NearbyPlace>()
val placeList = mutableListOf<CustomPlace>()
for (simplePlace in searchResult.results) {
placeList.add(mapToNearbyPlace(simplePlace))
placeList.add(mapToCustomPlace(simplePlace))
}
placeList
}
Expand Down Expand Up @@ -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<Place> {

// 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
Expand All @@ -159,7 +133,7 @@ class GoogleMapsRepository constructor(
)
}

private fun mapToNearbyPlace(place: SimplePlace): NearbyPlace {
private fun mapToCustomPlace(place: SimplePlace): CustomPlace {

val photoList = mutableListOf<PhotoMetadata>()
place.photos.forEach {
Expand All @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 6b55894

Please sign in to comment.