location livedata (#3)

This commit is contained in:
Maxim Bachinsky 2018-10-31 15:00:58 +03:00 committed by GitHub
parent 877754e77d
commit e29b1cdbee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 1 deletions

View File

@ -33,6 +33,7 @@ ext {
retrofit : '2.4.0',
rxJava : '2.2.2',
rxAndroid : '2.1.0',
crashlytics : '2.9.5'
crashlytics : '2.9.5',
location : '16.0.0'
]
}

1
livedata-location/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

View File

@ -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"
}

View File

@ -0,0 +1,2 @@
<manifest
package="ru.touchin.livedata.location"/>

View File

@ -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
)
}
}

View File

@ -16,6 +16,7 @@ include ':views'
include ':recyclerview-adapters'
include ':kotlin-extensions'
include ':templates'
include ':livedata-location'
project(':utils').projectDir = new File(rootDir, 'utils')
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(':kotlin-extensions').projectDir = new File(rootDir, 'kotlin-extensions')
project(':templates').projectDir = new File(rootDir, 'templates')
project(':livedata-location').projectDir = new File(rootDir, 'livedata-location')