Skip to content

Commit

Permalink
Add documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrocalles committed Jun 2, 2024
1 parent cd9a11e commit bbe48bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package com.github.swent.echo.connectivity
import com.mapbox.mapboxsdk.geometry.LatLng
import kotlinx.coroutines.flow.StateFlow

/** A GPS service. Allows the location to be accessed either as */
/** A service that provides the user's current location if it's accessible. */
interface GPSService {

/** The user location as a state flow. */
val userLocation: StateFlow<LatLng?>

/** The current user location. */
fun currentUserLocation(): LatLng?

/** Force the service to refresh the user location */
fun refreshUserLocation()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

/**
* A simple implementation of a [GPSService]
*
* @param context The context of the application that's using this service.
*/
class GPSServiceImpl(val context: Context) : GPSService {
private val locationProviderClient = LocationServices.getFusedLocationProviderClient(context)

// This manager allows to check whether the location is disabled in settings.
private val locationManager: LocationManager? =
getSystemService(context, LocationManager::class.java)

// User's last known location
private var _location = MutableStateFlow<LatLng?>(null)

/** Updates the last known location. */
private fun getLocation() {
if (
LOCATION_PERMISSIONS.any {
Expand All @@ -32,7 +42,9 @@ class GPSServiceImpl(val context: Context) : GPSService {
this.let { location -> LatLng(location.latitude, location.longitude) }
)
}
// Request again if null, unless location is turned off in settings
// Request again if null, unless location is turned off in settings.
// Even if this process has permissions to access the location, as
// long as it's disabled in settings, the listener will return null.
locationManager?.apply {
// TODO maybe also check for NETWORK_PROVIDER
if (isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Expand Down

0 comments on commit bbe48bc

Please sign in to comment.