-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aac986d
commit 188f72c
Showing
6 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/androidTest/java/com/github/swent/echo/connectivity/GPSServiceImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.swent.echo.connectivity | ||
|
||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.mapbox.mapboxsdk.geometry.LatLng | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class GPSServiceImplTest { | ||
private lateinit var gpsService: GPSService | ||
|
||
@get:Rule val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun shouldRefreshLocation() { | ||
lateinit var location: State<LatLng?> | ||
composeTestRule.setContent { | ||
gpsService = GPSServiceImpl(LocalContext.current) | ||
location = gpsService.userLocation.collectAsState() | ||
} | ||
composeTestRule.waitForIdle() | ||
assertNotNull(gpsService.userLocation) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/github/swent/echo/connectivity/GPSService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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 */ | ||
interface GPSService { | ||
val userLocation: StateFlow<LatLng?> | ||
|
||
fun currentUserLocation(): LatLng? | ||
} |
55 changes: 55 additions & 0 deletions
55
app/src/main/java/com/github/swent/echo/connectivity/GPSServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.github.swent.echo.connectivity | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.location.LocationManager | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat.getSystemService | ||
import com.github.swent.echo.compose.navigation.LOCATION_PERMISSIONS | ||
import com.google.android.gms.location.LocationServices | ||
import com.mapbox.mapboxsdk.geometry.LatLng | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
class GPSServiceImpl(context: Context) : GPSService { | ||
private val locationProviderClient = LocationServices.getFusedLocationProviderClient(context) | ||
private val locationManager: LocationManager? = | ||
getSystemService(context, LocationManager::class.java) | ||
private var _location = MutableStateFlow<LatLng?>(null) | ||
|
||
private fun getLocation(context: Context) { | ||
if ( | ||
LOCATION_PERMISSIONS.any { | ||
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED | ||
} | ||
) { | ||
locationProviderClient.lastLocation.addOnSuccessListener { | ||
it?.apply { | ||
val old = _location.value | ||
_location.compareAndSet( | ||
old, | ||
this.let { location -> LatLng(location.latitude, location.longitude) } | ||
) | ||
} | ||
// Request again if null, unless location is turned off in settings | ||
locationManager?.apply { | ||
// TODO maybe also check for NETWORK_PROVIDER | ||
if (isProviderEnabled(LocationManager.GPS_PROVIDER)) { | ||
it ?: getLocation(context) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
init { | ||
getLocation(context) | ||
} | ||
|
||
override val userLocation: StateFlow<LatLng?> = _location.asStateFlow() | ||
|
||
override fun currentUserLocation(): LatLng? = _location.value | ||
|
||
fun refreshUserLocation(context: Context) = getLocation(context) | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/github/swent/echo/di/GPSServiceModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.swent.echo.di | ||
|
||
import android.app.Application | ||
import com.github.swent.echo.connectivity.GPSService | ||
import com.github.swent.echo.connectivity.GPSServiceImpl | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object GPSServiceModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideGPSService(application: Application): GPSService { | ||
val context = application.applicationContext | ||
return GPSServiceImpl(context) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters