diff --git a/build.gradle b/build.gradle
index 08ac432..2660105 100644
--- a/build.gradle
+++ b/build.gradle
@@ -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'
]
}
diff --git a/livedata-location/.gitignore b/livedata-location/.gitignore
new file mode 100644
index 0000000..796b96d
--- /dev/null
+++ b/livedata-location/.gitignore
@@ -0,0 +1 @@
+/build
diff --git a/livedata-location/build.gradle b/livedata-location/build.gradle
new file mode 100644
index 0000000..297284c
--- /dev/null
+++ b/livedata-location/build.gradle
@@ -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"
+}
diff --git a/livedata-location/src/main/AndroidManifest.xml b/livedata-location/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..b8a5237
--- /dev/null
+++ b/livedata-location/src/main/AndroidManifest.xml
@@ -0,0 +1,2 @@
+
diff --git a/livedata-location/src/main/java/ru/touchin/livedata/location/LocationLiveData.kt b/livedata-location/src/main/java/ru/touchin/livedata/location/LocationLiveData.kt
new file mode 100644
index 0000000..4074888
--- /dev/null
+++ b/livedata-location/src/main/java/ru/touchin/livedata/location/LocationLiveData.kt
@@ -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() {
+
+ 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) {
+ 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
+ )
+ }
+
+}
\ No newline at end of file
diff --git a/modules.gradle b/modules.gradle
index 7db546a..4eec8fa 100644
--- a/modules.gradle
+++ b/modules.gradle
@@ -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')