Created
March 22, 2018 12:47
-
-
Save AndroidKiran/64c39594c0d7418b9e7297abf8db42c3 to your computer and use it in GitHub Desktop.
Location Livedata.
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
| import android.annotation.SuppressLint | |
| import android.arch.lifecycle.LiveData | |
| import android.content.Context | |
| import android.location.Location | |
| import android.os.Looper | |
| import com.google.android.gms.location.* | |
| import swapp.items.com.swappify.firebase.utils.Result | |
| class CurrentLocationLiveData private constructor(context: Context) : LiveData<Result<Location>>() { | |
| private lateinit var fusedLocationProviderClient: FusedLocationProviderClient | |
| private lateinit var locationRequest: LocationRequest | |
| private lateinit var settingsClient: SettingsClient | |
| private lateinit var locationSettingsRequest: LocationSettingsRequest | |
| init { | |
| buildClient(context) | |
| } | |
| @Synchronized | |
| private fun buildClient(appContext: Context) { | |
| fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(appContext) | |
| settingsClient = LocationServices.getSettingsClient(appContext) | |
| locationRequest = createLocationRequest() | |
| locationSettingsRequest = LocationSettingsRequest.Builder().apply { | |
| this.addLocationRequest(locationRequest) | |
| }.build() | |
| } | |
| private fun createLocationRequest(): LocationRequest = | |
| LocationRequest().apply { | |
| this.interval = UPDATE_INTERVAL_IN_MILLISECONDS | |
| this.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS | |
| this.priority = LocationRequest.PRIORITY_HIGH_ACCURACY | |
| } | |
| override fun onInactive() { | |
| fusedLocationProviderClient.removeLocationUpdates(locationCallback) | |
| } | |
| @SuppressLint("MissingPermission") | |
| override fun onActive() { | |
| settingsClient.checkLocationSettings(locationSettingsRequest)?.also { | |
| it.addOnSuccessListener { | |
| fusedLocationProviderClient.requestLocationUpdates(locationRequest, | |
| locationCallback, Looper.myLooper()) | |
| } | |
| it.addOnFailureListener { | |
| value = Result(null, it) | |
| } | |
| } | |
| } | |
| private val locationCallback = object : LocationCallback() { | |
| override fun onLocationResult(locationResult: LocationResult?) { | |
| locationResult?.let { | |
| value = Result(it.lastLocation, null) | |
| } | |
| } | |
| } | |
| companion object { | |
| @SuppressLint("StaticFieldLeak") | |
| @JvmStatic | |
| @Volatile | |
| var INSTANCE: CurrentLocationLiveData? = null | |
| fun getInstance(context: Context): CurrentLocationLiveData = | |
| INSTANCE ?: synchronized(this) { | |
| INSTANCE ?: CurrentLocationLiveData(context) | |
| } | |
| const val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 10000 | |
| const val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2 | |
| } | |
| } | |
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
| data class Result<out T>(private val data: T?, private var throwable: Throwable?) { | |
| fun isSuccess(): Boolean = data != null | |
| val error: Throwable? | |
| get() { | |
| if (throwable == null) { | |
| throw IllegalStateException("Database operation is successful please check with isSuccess first") | |
| } | |
| return throwable | |
| } | |
| val value: T | |
| get() { | |
| if (data == null) { | |
| throw IllegalStateException("Database operation is not successful please check with isSuccess first") | |
| } | |
| return data | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment