Add location manager & place picker

feature/top10
Aurelien Hubert 7 years ago
parent ed2c3a9f04
commit 8049ef8628
  1. 40
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/PlacePickerManager.kt
  2. 57
      app/src/main/java/net/pokeranalytics/android/util/LocationManager.kt

@ -0,0 +1,40 @@
package net.pokeranalytics.android.ui.fragment.components
import androidx.appcompat.app.AlertDialog
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.view.RowRepresentable
/**
* Display a dialog with a list of places around the user
*/
class PlacePickerManager {
companion object {
fun create(
activity: PokerAnalyticsActivity,
row: RowRepresentable,
delegate: RowRepresentableDelegate,
maxResults: Int? = 2
) {
activity.askForPlacesRequest { success, places ->
if (success && places.size > 0) {
val placesArray = ArrayList<CharSequence>()
for ((index, place) in places.withIndex()) {
placesArray.add(place.place.name.toString())
if (index == maxResults) {
break
}
}
val builder = AlertDialog.Builder(activity)
builder.setItems(placesArray.toTypedArray()) { _, which ->
delegate.onRowValueChanged(places[which].place, row)
}
builder.show()
}
}
}
}
}

@ -0,0 +1,57 @@
package net.pokeranalytics.android.util
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import com.google.android.gms.common.api.ApiException
import com.google.android.libraries.places.api.Places
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.FindCurrentPlaceRequest
import timber.log.Timber
import java.util.*
class LocationManager(private var context: Context) {
/**
* Return the [places] around the user
*/
fun askForPlacesRequest(callback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)?) {
// Initialize Places.
Places.initialize(context, context.getString(net.pokeranalytics.android.R.string.google_places_api))
// Create a new Places client instance.
val placesClient = Places.createClient(context)
// Use fields to define the data types to return.
val placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG)
// Use the builder to create a FindCurrentPlaceRequest.
val request = FindCurrentPlaceRequest.builder(placeFields).build()
// Call findCurrentPlace and handle the response (first check that the user has granted permission).
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val placeResponse = placesClient.findCurrentPlace(request)
placeResponse.addOnCompleteListener { task ->
val places = ArrayList<PlaceLikelihood>()
if (task.isSuccessful) {
val response = task.result
response?.placeLikelihoods?.let {
places.addAll(it)
}
} else {
val exception = task.exception
if (exception is ApiException) {
Timber.d("Error: ${"Place not found: " + exception.statusCode}")
}
}
callback?.invoke(task.isSuccessful, places)
}
}
}
}
Loading…
Cancel
Save