Zatrzymaj aktywny kurs po wygaśnięciu sesji

This commit is contained in:
admin
2026-07-18 16:29:34 +02:00
parent ffbae9edfd
commit fc49eae512
4 changed files with 40 additions and 11 deletions
@@ -67,9 +67,11 @@ class RoutePointWorker(
"retryable" to error.retryable, "retryable" to error.retryable,
), ),
) )
if (shouldStopRouteTrackingAfterPointError(error.kind, error.code)) {
ActiveRouteTrackingService.stop(applicationContext, routeId)
}
if (isInactiveRoutePointError(error.code)) { if (isInactiveRoutePointError(error.code)) {
dao.delete(pointIds) dao.delete(pointIds)
ActiveRouteTrackingService.stop(applicationContext, routeId)
continue continue
} }
@@ -105,3 +107,6 @@ class RoutePointWorker(
} }
internal fun isInactiveRoutePointError(code: String?): Boolean = code == "ROUTE_POINTS_INVALID_STATUS" internal fun isInactiveRoutePointError(code: String?): Boolean = code == "ROUTE_POINTS_INVALID_STATUS"
internal fun shouldStopRouteTrackingAfterPointError(kind: ApiErrorKind, code: String?): Boolean =
kind == ApiErrorKind.Auth || isInactiveRoutePointError(code)
@@ -136,6 +136,13 @@ class ActiveRouteTrackingService : Service(), LocationListener {
override fun onDestroy() { override fun onDestroy() {
runCatching { locationManager.removeUpdates(this) } runCatching { locationManager.removeUpdates(this) }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
@Suppress("DEPRECATION")
stopForeground(true)
}
getSystemService(NotificationManager::class.java).cancel(NOTIFICATION_ID)
getSharedPreferences(TRACKING_PREFERENCES, Context.MODE_PRIVATE) getSharedPreferences(TRACKING_PREFERENCES, Context.MODE_PRIVATE)
.edit() .edit()
.remove(ACTIVE_ROUTE_ID) .remove(ACTIVE_ROUTE_ID)
@@ -463,17 +463,16 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
}.onFailure { throwable -> }.onFailure { throwable ->
if (generation != routesRequestGeneration) return@onFailure if (generation != routesRequestGeneration) return@onFailure
reportHandledException("load_routes", throwable, mapOf("date" to date)) reportHandledException("load_routes", throwable, mapOf("date" to date))
if (ApiErrorMapper.map(throwable).kind == ApiErrorKind.Auth) {
handleExpiredSession()
} else {
_state.update { _state.update {
val apiError = ApiErrorMapper.map(throwable)
it.withApiError(throwable).copy( it.withApiError(throwable).copy(
screen = when { screen = if (it.screen == DriverScreen.Initializing) DriverScreen.Routes else it.screen,
apiError.kind == ApiErrorKind.Auth -> DriverScreen.Phone
it.screen == DriverScreen.Initializing -> DriverScreen.Routes
else -> it.screen
},
) )
} }
} }
}
if (generation == routesRequestGeneration) _state.update { if (generation == routesRequestGeneration) _state.update {
it.copy(loading = false, refreshing = false) it.copy(loading = false, refreshing = false)
@@ -548,7 +547,12 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
} }
} }
.onFailure { throwable -> .onFailure { throwable ->
if (generation == routeDetailRequestGeneration) _state.update { it.withApiError(throwable) } if (generation != routeDetailRequestGeneration) return@onFailure
if (ApiErrorMapper.map(throwable).kind == ApiErrorKind.Auth) {
handleExpiredSession()
} else {
_state.update { it.withApiError(throwable) }
}
} }
if (generation == routeDetailRequestGeneration) _state.update { it.copy(refreshing = false) } if (generation == routeDetailRequestGeneration) _state.update { it.copy(refreshing = false) }
@@ -1455,6 +1459,7 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
dispatchSheetUploadsJob?.cancel() dispatchSheetUploadsJob?.cancel()
realtimeBannerJob?.cancel() realtimeBannerJob?.cancel()
liveSyncClient.stop() liveSyncClient.stop()
ActiveRouteTrackingService.stop(getApplication())
offlineOutboxManager.clearAll() offlineOutboxManager.clearAll()
repository.logout(notifyServer = _state.value.isOnline) repository.logout(notifyServer = _state.value.isOnline)
syncRepository.clearCache() syncRepository.clearCache()
@@ -1665,6 +1670,7 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
private suspend fun handleExpiredSession() { private suspend fun handleExpiredSession() {
liveSyncClient.stop() liveSyncClient.stop()
ActiveRouteTrackingService.stop(getApplication())
offlineOutboxManager.clearAll() offlineOutboxManager.clearAll()
repository.logout(notifyServer = false) repository.logout(notifyServer = false)
syncRepository.clearCache() syncRepository.clearCache()
@@ -3,6 +3,7 @@ package pl.firmatpp.kierowca.data.upload
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import pl.firmatpp.kierowca.data.ApiErrorKind
class RoutePointWorkerRulesTest { class RoutePointWorkerRulesTest {
@Test @Test
@@ -15,4 +16,14 @@ class RoutePointWorkerRulesTest {
assertFalse(isInactiveRoutePointError("SERVER_UNAVAILABLE")) assertFalse(isInactiveRoutePointError("SERVER_UNAVAILABLE"))
assertFalse(isInactiveRoutePointError(null)) assertFalse(isInactiveRoutePointError(null))
} }
@Test
fun revokedSessionStopsRouteTracking() {
assertTrue(shouldStopRouteTrackingAfterPointError(ApiErrorKind.Auth, null))
}
@Test
fun temporaryFailureKeepsRouteTrackingActive() {
assertFalse(shouldStopRouteTrackingAfterPointError(ApiErrorKind.Server, "SERVER_UNAVAILABLE"))
}
} }