Fix findCurrentLocation

feature/top10
Aurelien Hubert 7 years ago
parent 91a741fd0c
commit 81a1a9efe0
  1. 30
      app/src/main/java/net/pokeranalytics/android/ui/activity/components/PokerAnalyticsActivity.kt

@ -8,16 +8,20 @@ import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.libraries.places.api.model.PlaceLikelihood
import io.realm.Realm
import net.pokeranalytics.android.model.realm.Location
import net.pokeranalytics.android.util.LocationManager
import timber.log.Timber
open class PokerAnalyticsActivity : AppCompatActivity() {
companion object {
const val PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 1000
const val PLAY_SERVICES_RESOLUTION_REQUEST = 2000
}
private val realm = Realm.getDefaultInstance()
@ -127,22 +131,36 @@ open class PokerAnalyticsActivity : AppCompatActivity() {
/**
* Find the current location
*/
fun findCurrentLocation(callback: ((location: Location?) -> Unit)?) {
if (LocationManager(this).databaseContainsLocationsWithCoordinates()) {
fun findCurrentLocation(callback: ((location: android.location.Location?) -> Unit)?) {
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager(this).findNearestLocationFromUser(callback)
LocationManager(this).findCurrentLocation(callback)
} else {
askForLocationPermission { granted ->
if (granted) {
LocationManager(this).findNearestLocationFromUser(callback)
LocationManager(this).findCurrentLocation(callback)
} else {
callback?.invoke(null)
}
}
}
} else {
callback?.invoke(null)
}
/**
* Check the Google Play Services
*/
fun checkPlayServices(): Boolean {
val googleAPI = GoogleApiAvailability.getInstance()
val result = googleAPI.isGooglePlayServicesAvailable(this)
Timber.d("checkPlayServices: $result")
if (result != ConnectionResult.SUCCESS) {
if (googleAPI.isUserResolvableError(result)) {
googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show()
}
return false
}
return true
}
}
Loading…
Cancel
Save