Wrapper of FusedLocationProviderClient for Android to support modern usage like LiveData and Flow

Overview

FancyLocationProvider

CircleCI jitpack

Wrapper of FusedLocationProviderClient for Android to support modern usage like LiveData or Flow.

Install

Add Jitpack repository to your root build.grable:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Then add dependency in your module build.gradle:

dependencies {
  implementation 'com.github.jintin:FancyLocationProvider:2.0.0'
}

Usage

LiveData

Here is an example of how you can create a LocationLiveData in ViewModel layer, just provide Context and the LocationRequest with your own config.

class MainViewModel(application: Application) : AndroidViewModel(application) {
    private val locationRequest =
        LocationRequest.create()
            .setInterval(3000)
            .setFastestInterval(3000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)

    val locationLiveData = LocationLiveData(application, locationRequest)
}

And here is how you observe the update on View layer.

// check for the location permission first
locationLiveData.observe(this) {
    when (it) {
        is LocationData.Success -> // get location by "it.location"
        is LocationData.Fail -> // Fail to get location
    }
}

Flow

val locationFlow = LocationFlow(application, locationRequest)

// check for the location permission first
// should inside coroutine
locationFlow.get().collect {
    when (it) {
        is LocationData.Success -> // get location by "it.location"
        is LocationData.Fail -> // Fail to get location
    }
}

You can go to ./app module for more information.

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:

class LocationProvider(
    private val context: Context,
    private val locationRequest: LocationRequest
) : ILocationProvider {

    private val locationProviderClient by lazy {
        LocationServices.getFusedLocationProviderClient(context)
    }
    private val locationListener by lazy {
        LocationListener()
    }

    @RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
    override fun requestLocationUpdates(locationObserver: ILocationObserver) {
        locationListener.locationObserver = locationObserver
        locationProviderClient.requestLocationUpdates(
            locationRequest,
            locationListener,
            Looper.getMainLooper()
        )
    }

    override fun removeLocationUpdates() {
        locationProviderClient.removeLocationUpdates(locationListener)
    }

    private class LocationListener : LocationCallback() {
        var locationObserver: ILocationObserver? = null

        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.

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.

License

The package is available as open source under the terms of the MIT License.

Buy Me A Coffee

You might also like...
Disk Usage/Free Utility - a better 'df' alternative
Disk Usage/Free Utility - a better 'df' alternative

duf Disk Usage/Free Utility (Linux, BSD, macOS & Windows) Features User-friendly, colorful output Adjusts to your terminal's theme & width Sort the re

Account-lib - A suite of libraries to facilitate the usage of account-sdk

Usage Clone this repository (skip this step if the repo is on your local machine). The default branch is fine. git clone https://github.com/AFBlockcha

Sample app that implements MVVM architecture using Kotlin, ViewModel, LiveData, and etc.

TheShard is a project to showcase different architectural approaches to developing Android apps. In its different branches you will find the same app (A movie Listing and detail page) implemented with small differences.

Android To-Do MVVM Architecture App written in Kotlin.(ViewModel, ROOM, Livedata, Coroutines)

MVVM-To-Do-App A To-Do application written in kotlin using Android Architectural components What's new? Room + Coroutines - Upgraded Room to v2.1. Roo

Android Library for requesting Permissions with Kotlin Coroutines or AndroidX LiveData

PEKO PErmissions with KOtlin Android Permissions with Kotlin Coroutines or LiveData No more callbacks, builders, listeners or verbose code for request

ViewModel e LiveData - Android Studio - Kotlin

Unscramble App Starter code for Android Basics codelab - Store the data in a ViewModel Unscramble is a single player game app that displays scrambled

Android Clean Architechture with MVVM, LiveData, Coroutine, Dagger Hilt, Room, DataStore
Android Clean Architechture with MVVM, LiveData, Coroutine, Dagger Hilt, Room, DataStore

MovKu An application that displays a list of popular movies and detail Concepts Tech Stack Kotlin -A modern programming language that makes developers

[Android-Kotlin] MVVM, ViewModel, LiveData, Observer, DataBinding, Repository, Retrofit, Dagger example

SimpleMvvmDaggerKotlin [Android-Kotlin] MVVM, ViewModel, LiveData, Observer, DataBinding, Repository, Retrofit, Dagger example [Image1 : User informat

Anime quotes rest api app with mvvm, LiveData, Coroutines, Navigation Component etc

AnimeQuote Anime quotes image rest api app with mvvm, LiveData, Coroutines, Navigation Component, clean achitecture etc An app created during mentorin

Releases(2.0.0)
Owner
Jintin
Android GDE, husband and dad. Love to build interesting things to make life easier.
Jintin
Sample application to demonstrate Multi-module Clean MVVM Architecture and usage of Android Hilt, Kotlin Flow, Navigation Graph, Unit tests etc.

MoneyHeist-Chars Sample application to demonstrate Multi-module Clean MVVM Architecture and usage of Android Hilt, Kotlin Flow, Navigation Graph, Room

Hisham 20 Nov 19, 2022
Basic app to use different type of observables StateFlow, Flow, SharedFlow, LiveData, State, Channel...

stateflow-flow-sharedflow-livedata Basic app to use different type of observables StateFlow, Flow, SharedFlow, LiveData, State, Channel... StateFlow,

Raheem 5 Dec 21, 2022
CryptoMovies is a small app that show modern Android developement: with Hilt, Coroutines, Flow, Jetpack and Material Design 3

CryptoMovies is a small app that show modern Android developement: with Hilt, Coroutines, Flow, Jetpack and Material Design 3.

Leonardo Pirro 7 Sep 2, 2022
A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, ViewModel, LiveData, Room, Navigation-Compose, Coil, koin etc.

Paper - A Minimal Notes App A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, V

Akshay Sharma 139 Jan 2, 2023
🪐 Modern Android development with Hilt, Coroutines, Flow, JetPack(ViewModel) based on MVVM architecture.

Ceres ?? Modern Android development with Hilt, Coroutines, Flow, JetPack(ViewModel) based on MVVM architecture. Download Gradle Add the dependency bel

Teodor G. 21 Jan 11, 2023
A beautiful Fashion Store like Android App Mock built on Jetpack Compose with compose navigation, hilt, dark theme support and google's app architecture found on uplabs Here

A beautiful Fashion Store like Android App Mock built on Jetpack Compose with compose navigation, hilt, dark theme support and google's app architecture found on uplabs Here

Puncz 87 Nov 30, 2022
Clean MVVM with eliminating the usage of context from view models by introducing hilt for DI and sealed classes for displaying Errors in views using shared flows (one time event), and Stateflow for data

Clean ViewModel with Sealed Classes Following are the purposes of this repo Showing how you can remove the need of context in ViewModels. I. By using

Kashif Mehmood 22 Oct 26, 2022
Android Library to make SharedPreferences usage easier.

KotlinPreferences Kotlin Android Library, that makes preference usage simple and fun. KotlinPreferences now have a brother. With KotlinPreferences, yo

Marcin Moskała 50 Nov 6, 2022
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.

Delight-Playground ?? Native Android application built with Kotlin and Jetpack Compose. This app also illustrates the usage of advance libraries such

Kasem SM 41 Nov 6, 2022
SharedPreference usage made fun in Kotlin

PreferenceHolder Kotlin Android Library, that makes preference usage simple and fun. To stay up-to-date with news about library This library is younge

Marcin Moskała 155 Dec 15, 2022