Simple location fetcher for Android Apps built with Kotlin, Coroutines and Compose.
This is a completely independent artifact from the Activity/Fragment LocationFetcher, and is built exclusively for use with Jetpack Compose.
@Composable
fun NearbyRestaurants() {
LocationFetcher(
requestConfig = {
interval = 15.seconds.inWholeMilliseconds
fastestInterval = 15.seconds.inWholeMilliseconds
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
},
rationale = "We need your location for finding nearby restaurants."
) {
val (locationResult, settingEnabled, permissionsGranted) = LocalLocationFetcher.current
when (locationResult) {
null -> MissingLocation()
else -> NearbyRestaurants(locationResult)
}
}
}
This library is hosted in Maven Central, so you must set it up for your project before adding the module-level dependency.
The new way to install dependencies repositories is through the dependencyResolutionManagement
DSL in settings.gradle(.kts)
.
Kotlin or Groovy:
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
OR
On project-level build.gradle
:
Kotlin or Groovy:
allprojects {
repositories {
mavenCentral()
}
}
On app-level build.gradle(.kts)
, add dependency:
dependencies {
implementation("app.freel:locationfetcher-compose:9.0.0")
}
On any Composable
, create a LocationFetcher
composable. All children composables will have
access to a CompositionLocal
called LocalLocationFetcher.current
, which returns a
LocationState
:
@Immutable
public data class LocationState(
/** Latest location results, or null if no locations were reported lately. */
val locationResult: LocationResult?,
/** Whether location setting is enabled, or null if status is unknown. */
val settingEnabled: Boolean?,
/** Whether location permissions are granted. */
val permissionsGranted: Boolean
)
See LocationFetcher
inline documentation for customizations.