Stop location tracking only when callbacks list is empty to avoid non wanting stop

This commit is contained in:
Maxime NATUREL 2022-06-09 10:15:57 +02:00
parent 1b88cc39a9
commit a1aa337edb
4 changed files with 15 additions and 6 deletions

View File

@ -205,7 +205,6 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
private fun destroyMe() {
locationTracker.removeCallback(this)
locationTracker.stop()
timers.forEach { it.cancel() }
timers.clear()
stopSelf()

View File

@ -114,7 +114,6 @@ class LocationSharingViewModel @AssistedInject constructor(
override fun onCleared() {
super.onCleared()
locationTracker.removeCallback(this)
locationTracker.stop()
}
override fun handle(action: LocationSharingAction) {

View File

@ -144,6 +144,9 @@ class LocationTracker @Inject constructor(
fun removeCallback(callback: Callback) {
callbacks.remove(callback)
if (callbacks.size == 0) {
stop()
}
}
override fun onLocationChanged(location: Location) {

View File

@ -123,16 +123,24 @@ class LocationTrackerTest {
@Test
fun `when adding or removing a callback then it is added into or removed from the list of callbacks`() {
val callback = mockCallback()
val callback1 = mockCallback()
val callback2 = mockCallback()
locationTracker.addCallback(callback)
locationTracker.addCallback(callback1)
locationTracker.addCallback(callback2)
locationTracker.callbacks.size shouldBeEqualTo 2
locationTracker.callbacks.first() shouldBeEqualTo callback1
locationTracker.callbacks[1] shouldBeEqualTo callback2
locationTracker.removeCallback(callback1)
locationTracker.callbacks.size shouldBeEqualTo 1
locationTracker.callbacks.first() shouldBeEqualTo callback
locationTracker.removeCallback(callback)
locationTracker.removeCallback(callback2)
locationTracker.callbacks.size shouldBeEqualTo 0
verify { locationTracker.stop() }
}
@Test