Skip to content

Commit

Permalink
Merge pull request #8 from Jintin/feature/readme
Browse files Browse the repository at this point in the history
Update readme of custom provider
  • Loading branch information
Jintin authored Nov 16, 2020
2 parents 0f0e9a4 + bd671d2 commit ffaf34e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/com/jintin/fancylocation/app/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}

0 comments on commit ffaf34e

Please sign in to comment.