From bd671d2c6c98576a237b86da68286bd06fb688a7 Mon Sep 17 00:00:00 2001 From: Jintin Date: Mon, 16 Nov 2020 22:43:54 +0800 Subject: [PATCH] update readme of custom provider --- README.md | 55 +++++++++++++++++++ .../jintin/fancylocation/app/MainViewModel.kt | 22 ++++---- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 893033d..da8abbd 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,61 @@ locationFlow.get().collect { ``` +## Custom Location Provider +FancyLocationProvider using GMS as the default location provider as it serve the most use case. +But you can also used it with any other provider you want like Huawei HMS. Just create the custom provider implement the `ILocationProvider` interface like this: +```kotlin +class LocationProvider( + private val context: Context, + private val locationRequest: LocationRequest +) : ILocationProvider { + + private val client by lazy { + LocationServices.getFusedLocationProviderClient(context) + } + private val locationListener by lazy { + LocationListener() + } + private var locationObserver: ILocationObserver? = null + + @RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION]) + override fun requestLocationUpdates(locationObserver: ILocationObserver) { + this.locationObserver = locationObserver + client.requestLocationUpdates( + locationRequest, + locationListener, + Looper.getMainLooper() + ) + } + + override fun removeLocationUpdates() { + client.removeLocationUpdates(locationListener) + } + + inner class LocationListener : LocationCallback() { + override fun onLocationResult(result: LocationResult?) { + result?.lastLocation?.let { + locationObserver?.onLocationResult(it) + } + } + + override fun onLocationAvailability(availability: LocationAvailability?) { + if (availability?.isLocationAvailable == false) { + locationObserver?.onLocationFailed() + } + } + } +} +``` +Then you can create the custom provider and transform it into LiveData or Flow. +```kotlin +private val locationProvider: ILocationProvider = LocationProvider(context, locationRequest) + +@ExperimentalCoroutinesApi +val locationFlow: LocationFlow = locationProvider.asFlow() +val locationLiveData: LocationLiveData = locationProvider.asLiveData() +``` + ## Contributing Bug reports and pull requests are welcome on GitHub at [https://github.com/Jintin/FancyLocationProvider](https://github.com/Jintin/FancyLocationProvider). diff --git a/app/src/main/java/com/jintin/fancylocation/app/MainViewModel.kt b/app/src/main/java/com/jintin/fancylocation/app/MainViewModel.kt index e14f3f9..36142ab 100644 --- a/app/src/main/java/com/jintin/fancylocation/app/MainViewModel.kt +++ b/app/src/main/java/com/jintin/fancylocation/app/MainViewModel.kt @@ -3,8 +3,10 @@ package com.jintin.fancylocation.app import android.app.Application import androidx.lifecycle.AndroidViewModel import com.google.android.gms.location.LocationRequest -import com.jintin.fancylocation.LocationFlow -import com.jintin.fancylocation.LocationLiveData +import com.jintin.fancylocation.ILocationProvider +import com.jintin.fancylocation.LocationProvider +import com.jintin.fancylocation.asFlow +import com.jintin.fancylocation.asLiveData import kotlinx.coroutines.ExperimentalCoroutinesApi class MainViewModel(application: Application) : AndroidViewModel(application) { @@ -15,16 +17,14 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) - @ExperimentalCoroutinesApi - val locationFlow = LocationFlow(application, locationRequest) - val locationLiveData = LocationLiveData(application, locationRequest) - -// // we can also provide custom vendor by create your own LocationProvider -// private val locationProvider: ILocationProvider = LocationProvider(application, locationRequest) -// // @ExperimentalCoroutinesApi -// val locationFlow = locationProvider.asFlow() -// val locationLiveData = locationProvider.asLiveData() +// val locationFlow = LocationFlow(application, locationRequest) +// val locationLiveData = LocationLiveData(application, locationRequest) + // we can also provide custom vendor by create your own LocationProvider + private val locationProvider: ILocationProvider = LocationProvider(application, locationRequest) + @ExperimentalCoroutinesApi + val locationFlow = locationProvider.asFlow() + val locationLiveData = locationProvider.asLiveData() } \ No newline at end of file