Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme of custom provider #8

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}