Using Duration to have clearer syntax

This commit is contained in:
Maxime NATUREL 2022-08-02 09:27:27 +02:00
parent 0944b2ff39
commit 49ad37a7f6
3 changed files with 16 additions and 11 deletions

View File

@ -22,5 +22,3 @@ const val DEFAULT_PIN_ID = "DEFAULT_PIN_ID"
const val INITIAL_MAP_ZOOM_IN_PREVIEW = 15.0
const val INITIAL_MAP_ZOOM_IN_TIMELINE = 17.0
const val MIN_TIME_TO_UPDATE_LOCATION_MILLIS = 5 * 1_000L // every 5 seconds
const val MIN_DISTANCE_TO_UPDATE_LOCATION_METERS = 10f

View File

@ -36,6 +36,10 @@ import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Duration.Companion.seconds
@VisibleForTesting
const val MIN_DISTANCE_TO_UPDATE_LOCATION_METERS = 10f
@Singleton
class LocationTracker @Inject constructor(
@ -64,6 +68,9 @@ class LocationTracker @Inject constructor(
private var firstLocationHandled = false
private val _locations = MutableSharedFlow<Location>(replay = 1)
@VisibleForTesting
val minDurationToUpdateLocationMillis = 5.seconds.inWholeMilliseconds
/**
* SharedFlow to collect location updates.
*/
@ -71,7 +78,7 @@ class LocationTracker @Inject constructor(
.onEach { Timber.d("new location emitted") }
.debounce {
if (firstLocationHandled) {
MIN_TIME_TO_UPDATE_LOCATION_MILLIS
minDurationToUpdateLocationMillis
} else {
firstLocationHandled = true
0
@ -103,7 +110,7 @@ class LocationTracker @Inject constructor(
locationManager.requestLocationUpdates(
provider,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
minDurationToUpdateLocationMillis,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
this
)

View File

@ -75,19 +75,19 @@ class LocationTrackerTest {
verifyOrder {
fakeLocationManager.instance.requestLocationUpdates(
LocationManager.FUSED_PROVIDER,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
locationTracker.minDurationToUpdateLocationMillis,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
locationTracker
)
fakeLocationManager.instance.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
locationTracker.minDurationToUpdateLocationMillis,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
locationTracker
)
fakeLocationManager.instance.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
locationTracker.minDurationToUpdateLocationMillis,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
locationTracker
)
@ -154,7 +154,7 @@ class LocationTrackerTest {
locationTracker.onLocationChanged(fusedLocation)
locationTracker.onLocationChanged(gpsLocation)
locationTracker.onLocationChanged(networkLocation)
advanceTimeBy(MIN_TIME_TO_UPDATE_LOCATION_MILLIS + 1)
advanceTimeBy(locationTracker.minDurationToUpdateLocationMillis + 1)
val expectedLocationData = LocationData(
latitude = 1.0,
@ -188,7 +188,7 @@ class LocationTrackerTest {
locationTracker.onLocationChanged(gpsLocation)
locationTracker.onLocationChanged(networkLocation)
advanceTimeBy(MIN_TIME_TO_UPDATE_LOCATION_MILLIS + 1)
advanceTimeBy(locationTracker.minDurationToUpdateLocationMillis + 1)
val expectedLocationData = LocationData(
latitude = 1.0,
@ -217,7 +217,7 @@ class LocationTrackerTest {
val resultUpdates = locationTracker.locations.test(this)
locationTracker.onLocationChanged(networkLocation)
advanceTimeBy(MIN_TIME_TO_UPDATE_LOCATION_MILLIS + 1)
advanceTimeBy(locationTracker.minDurationToUpdateLocationMillis + 1)
val expectedLocationData = LocationData(
latitude = 1.0,
@ -243,7 +243,7 @@ class LocationTrackerTest {
val resultUpdates = locationTracker.locations.test(this)
locationTracker.requestLastKnownLocation()
advanceTimeBy(MIN_TIME_TO_UPDATE_LOCATION_MILLIS + 1)
advanceTimeBy(locationTracker.minDurationToUpdateLocationMillis + 1)
val expectedLocationData = LocationData(
latitude = A_LATITUDE,