location livedata (#3)
This commit is contained in:
parent
877754e77d
commit
e29b1cdbee
|
|
@ -33,6 +33,7 @@ ext {
|
||||||
retrofit : '2.4.0',
|
retrofit : '2.4.0',
|
||||||
rxJava : '2.2.2',
|
rxJava : '2.2.2',
|
||||||
rxAndroid : '2.1.0',
|
rxAndroid : '2.1.0',
|
||||||
crashlytics : '2.9.5'
|
crashlytics : '2.9.5',
|
||||||
|
location : '16.0.0'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
/build
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
apply plugin: 'com.android.library'
|
||||||
|
apply plugin: 'kotlin-android'
|
||||||
|
|
||||||
|
android {
|
||||||
|
compileSdkVersion versions.compileSdk
|
||||||
|
|
||||||
|
defaultConfig {
|
||||||
|
minSdkVersion 16
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api project(":lifecycle")
|
||||||
|
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "com.google.android.gms:play-services-location:$versions.location"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
<manifest
|
||||||
|
package="ru.touchin.livedata.location"/>
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package ru.touchin.livedata.location
|
||||||
|
|
||||||
|
import android.Manifest.permission.ACCESS_COARSE_LOCATION
|
||||||
|
import android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
|
import android.content.Context
|
||||||
|
import android.location.Location
|
||||||
|
import androidx.annotation.RequiresPermission
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.Observer
|
||||||
|
import com.google.android.gms.location.FusedLocationProviderClient
|
||||||
|
import com.google.android.gms.location.LocationAvailability
|
||||||
|
import com.google.android.gms.location.LocationCallback
|
||||||
|
import com.google.android.gms.location.LocationRequest
|
||||||
|
import com.google.android.gms.location.LocationResult
|
||||||
|
import com.google.android.gms.location.LocationServices
|
||||||
|
|
||||||
|
class LocationLiveData(
|
||||||
|
context: Context,
|
||||||
|
private val request: LocationRequest
|
||||||
|
) : LiveData<Location>() {
|
||||||
|
|
||||||
|
private val fusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
|
||||||
|
private val locationCallback = object : LocationCallback() {
|
||||||
|
override fun onLocationResult(result: LocationResult) {
|
||||||
|
postValue(result.lastLocation)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLocationAvailability(availability: LocationAvailability) {
|
||||||
|
if (!availability.isLocationAvailable) postValue(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresPermission(anyOf = [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION])
|
||||||
|
override fun observe(owner: LifecycleOwner, observer: Observer<in Location>) {
|
||||||
|
super.observe(owner, observer)
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresPermission(anyOf = [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION])
|
||||||
|
override fun onActive() {
|
||||||
|
startListening()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onInactive() {
|
||||||
|
stopListening()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopListening() {
|
||||||
|
fusedLocationClient.removeLocationUpdates(locationCallback)
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresPermission(anyOf = [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION])
|
||||||
|
private fun startListening() {
|
||||||
|
fusedLocationClient.requestLocationUpdates(
|
||||||
|
request,
|
||||||
|
locationCallback,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,7 @@ include ':views'
|
||||||
include ':recyclerview-adapters'
|
include ':recyclerview-adapters'
|
||||||
include ':kotlin-extensions'
|
include ':kotlin-extensions'
|
||||||
include ':templates'
|
include ':templates'
|
||||||
|
include ':livedata-location'
|
||||||
|
|
||||||
project(':utils').projectDir = new File(rootDir, 'utils')
|
project(':utils').projectDir = new File(rootDir, 'utils')
|
||||||
project(':logging').projectDir = new File(rootDir, 'logging')
|
project(':logging').projectDir = new File(rootDir, 'logging')
|
||||||
|
|
@ -28,3 +29,4 @@ project(':views').projectDir = new File(rootDir, 'views')
|
||||||
project(':recyclerview-adapters').projectDir = new File(rootDir, 'recyclerview-adapters')
|
project(':recyclerview-adapters').projectDir = new File(rootDir, 'recyclerview-adapters')
|
||||||
project(':kotlin-extensions').projectDir = new File(rootDir, 'kotlin-extensions')
|
project(':kotlin-extensions').projectDir = new File(rootDir, 'kotlin-extensions')
|
||||||
project(':templates').projectDir = new File(rootDir, 'templates')
|
project(':templates').projectDir = new File(rootDir, 'templates')
|
||||||
|
project(':livedata-location').projectDir = new File(rootDir, 'livedata-location')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue