Skip to content

Commit

Permalink
Updated according feedback from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
thubalek authored and rtchagas committed Oct 23, 2019
1 parent 3c05618 commit 30d96db
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ interface PlaceRepository {

fun getPlacePhoto(photoMetadata: PhotoMetadata): Single<Bitmap>

fun getPlaceByLocation(location: LatLng): Single<Place?>
fun getPlaceByLocation(location: LatLng): Single<Place>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package com.rtchagas.pingplacepicker.repository.googlemaps

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.location.Location
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.libraries.places.api.model.AddressComponents
import com.google.android.libraries.places.api.model.OpeningHours
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.model.PlusCode
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
Expand All @@ -23,7 +15,6 @@ import com.rtchagas.pingplacepicker.PingPlacePicker
import com.rtchagas.pingplacepicker.model.SearchResult
import com.rtchagas.pingplacepicker.repository.PlaceRepository
import io.reactivex.Single
import kotlin.math.absoluteValue


class GoogleMapsRepository constructor(
Expand Down Expand Up @@ -122,7 +113,7 @@ class GoogleMapsRepository constructor(
* [Places SDK for Android Usage and
Billing](https://developers.google.com/maps/documentation/geocoding/usage-and-billing#pricing-for-the-geocoding-api)
*/
override fun getPlaceByLocation(location: LatLng): Single<Place?> {
override fun getPlaceByLocation(location: LatLng): Single<Place> {

val paramLocation = "${location.latitude},${location.longitude}"

Expand All @@ -131,127 +122,12 @@ class GoogleMapsRepository constructor(
if (("OK" == result.status) && result.results.isNotEmpty()) {
return@flatMap getPlaceById(result.results[0].placeId)
}
return@flatMap Single.just(PlaceFromCoordinates(location.latitude, location.longitude))
return@flatMap Single.just(PlaceFromCoordinates(
location.latitude,
location.longitude))
}
}

/**
* Place without any additional info. Just latitude and longitude.
*/
private class PlaceFromCoordinates(private val latitude: Double, private val longitude: Double) : Place() {
constructor(parcel: Parcel) : this(
parcel.readDouble(),
parcel.readDouble())

override fun getUserRatingsTotal(): Int? {
return null
}

override fun getName(): String? {
return "${formatLatitude(latitude)}, ${formatLongitude(longitude)}"
}

override fun getOpeningHours(): OpeningHours? {
return null
}

override fun getId(): String? {
return null
}

override fun getPhotoMetadatas(): MutableList<PhotoMetadata> {
return mutableListOf()
}

override fun getWebsiteUri(): Uri? {
return null
}

override fun getPhoneNumber(): String? {
return null
}

override fun getRating(): Double? {
return null
}

override fun getPriceLevel(): Int? {
return null
}

override fun getAddressComponents(): AddressComponents? {
return null
}

override fun getAttributions(): MutableList<String> {
return mutableListOf()
}

override fun getAddress(): String? {
return null
}

override fun getPlusCode(): PlusCode? {
return null
}

override fun getUtcOffsetMinutes(): Int? {
return null
}

override fun getTypes(): MutableList<Type> {
return mutableListOf()
}

override fun getViewport(): LatLngBounds? {
return null
}

override fun getLatLng(): LatLng? {
return LatLng(latitude, longitude)
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeDouble(latitude)
parcel.writeDouble(longitude)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<PlaceFromCoordinates> {
override fun createFromParcel(parcel: Parcel): PlaceFromCoordinates {
return PlaceFromCoordinates(parcel)
}

override fun newArray(size: Int): Array<PlaceFromCoordinates?> {
return arrayOfNulls(size)
}
}

// formatting methods -----------------------------------------------------------------

private fun formatLatitude(latitude: Double): String {
val direction = if (latitude > 0) "N" else "S"
return "${replaceDelimiters(Location.convert(latitude.absoluteValue,
Location.FORMAT_SECONDS))} $direction"
}

private fun formatLongitude(longitude: Double): String {
val direction = if (longitude > 0) "W" else "E"
return "${replaceDelimiters(Location.convert(longitude.absoluteValue,
Location.FORMAT_SECONDS))} $direction"
}

private fun replaceDelimiters(original: String): String {
val parts = original.split(":")
val idx = parts[2].indexOfAny(arrayOf(',','.').toCharArray())
val seconds = parts[2].subSequence(0, idx)
return "${parts[0]}° ${parts[1]}' $seconds\""
}
}

/**
* This call to Google Places API is totally free :)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.rtchagas.pingplacepicker.repository.googlemaps

import android.location.Location
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.libraries.places.api.model.AddressComponents
import com.google.android.libraries.places.api.model.OpeningHours
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.PlusCode
import kotlin.math.absoluteValue

/**
* Place without any additional info. Just latitude and longitude.
*/
internal class PlaceFromCoordinates(private val latitude: Double, private val longitude: Double) : Place() {
constructor(parcel: Parcel) : this(
parcel.readDouble(),
parcel.readDouble())

override fun getUserRatingsTotal(): Int? {
return null
}

override fun getName(): String? {
return "${formatLatitude(latitude)}, ${formatLongitude(longitude)}"
}

override fun getOpeningHours(): OpeningHours? {
return null
}

override fun getId(): String? {
return null
}

override fun getPhotoMetadatas(): MutableList<PhotoMetadata> {
return mutableListOf()
}

override fun getWebsiteUri(): Uri? {
return null
}

override fun getPhoneNumber(): String? {
return null
}

override fun getRating(): Double? {
return null
}

override fun getPriceLevel(): Int? {
return null
}

override fun getAddressComponents(): AddressComponents? {
return null
}

override fun getAttributions(): MutableList<String> {
return mutableListOf()
}

override fun getAddress(): String? {
return null
}

override fun getPlusCode(): PlusCode? {
return null
}

override fun getUtcOffsetMinutes(): Int? {
return null
}

override fun getTypes(): MutableList<Type> {
return mutableListOf()
}

override fun getViewport(): LatLngBounds? {
return null
}

override fun getLatLng(): LatLng? {
return LatLng(latitude, longitude)
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeDouble(latitude)
parcel.writeDouble(longitude)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<PlaceFromCoordinates> {
override fun createFromParcel(parcel: Parcel): PlaceFromCoordinates {
return PlaceFromCoordinates(parcel)
}

override fun newArray(size: Int): Array<PlaceFromCoordinates?> {
return arrayOfNulls(size)
}
}

// formatting methods -----------------------------------------------------------------

private fun formatLatitude(latitude: Double): String {
val direction = if (latitude > 0) "N" else "S"
return "${replaceDelimiters(Location.convert(latitude.absoluteValue,
Location.FORMAT_SECONDS))} $direction"
}

private fun formatLongitude(longitude: Double): String {
val direction = if (longitude > 0) "W" else "E"
return "${replaceDelimiters(Location.convert(longitude.absoluteValue,
Location.FORMAT_SECONDS))} $direction"
}

private fun replaceDelimiters(original: String): String {
val parts = original.split(":")
val idx = parts[2].indexOfAny(arrayOf(',','.').toCharArray())
val seconds = parts[2].subSequence(0, idx)
return "${parts[0]}° ${parts[1]}' $seconds\""
}
}

0 comments on commit 30d96db

Please sign in to comment.