Created
March 20, 2023 08:56
-
-
Save webserveis/f99d053f8e3f5acbcb00dbfb7c46c2ba to your computer and use it in GitHub Desktop.
LocationLivedata.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codelaby.mapviewer | |
import android.annotation.SuppressLint | |
import android.content.Context | |
import android.location.Location | |
import android.os.Looper | |
import android.util.Log | |
import androidx.lifecycle.LiveData | |
import com.google.android.gms.location.* | |
//https://proandroiddev.com/monitoring-gps-and-location-permission-checks-livedata-part-1-278907344b77 | |
//https://maddevs.io/blog/device-location-using-livedata-architecture-component/ | |
class LocationLiveData(context: Context) : LiveData<LocationModel>() { | |
companion object { | |
private const val TAG = "LocationLiveData" | |
} | |
private var timeInterval: Long = 10000L | |
private var minimalDistance: Float = 12f | |
private var locationClient = LocationServices.getFusedLocationProviderClient(context) | |
private var locationRequest: LocationRequest | |
init { | |
locationRequest = createRequest() | |
} | |
override fun onInactive() { | |
super.onInactive() | |
Log.d(TAG, "onInactive() called") | |
locationClient.flushLocations() | |
locationClient.removeLocationUpdates(locationCallback) | |
} | |
@SuppressLint("MissingPermission") | |
override fun onActive() { | |
super.onActive() | |
Log.d(TAG, "onActive() called") | |
locationClient.lastLocation | |
.addOnSuccessListener { location: Location? -> | |
location?.also { | |
setLocationData(it) | |
} | |
} | |
startLocationUpdates() | |
} | |
@SuppressLint("MissingPermission") | |
private fun startLocationUpdates() { | |
val looper = Looper.myLooper() | |
locationClient.requestLocationUpdates( | |
locationRequest, | |
locationCallback, | |
looper | |
) | |
} | |
private fun createRequest(): LocationRequest = | |
// New builder | |
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, timeInterval).apply { | |
setMinUpdateDistanceMeters(minimalDistance) | |
setGranularity(Granularity.GRANULARITY_PERMISSION_LEVEL) | |
setWaitForAccurateLocation(true) | |
}.build() | |
private val locationCallback = object : LocationCallback() { | |
override fun onLocationResult(locationResult: LocationResult) { | |
Log.d(TAG, "onLocationResult() called with: locationResult = $locationResult") | |
for (location in locationResult.locations) { | |
setLocationData(location) | |
} | |
} | |
override fun onLocationAvailability(availability: LocationAvailability) { | |
Log.d(TAG, "onLocationAvailability() called with: availability = $availability") | |
availability.isLocationAvailable | |
} | |
} | |
private fun setLocationData(location: Location) { | |
value = LocationModel( | |
longitude = location.longitude, | |
latitude = location.latitude | |
) | |
} | |
} | |
data class LocationModel( | |
val longitude: Double, | |
val latitude: Double | |
) | |
/* | |
sealed class LocationState { | |
object WORKING : LocationState() | |
class SUCCESS(val result: LocationState) : MyState() | |
class FAILED(val error: AppFailure) : MyState() | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment