Skip to content

Colota - Android GPS Location tracker

Fix cleared queue items falsely counting as sent

Max Dietrich402241d7fb1b
diff --git a/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt b/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
index 6eab35f2..7c9f5027 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
@@ -419,21 +419,28 @@ class DatabaseHelper private constructor(context: Context) :
fun removeBatchFromQueue(queueIds: List<Long>) {
if (queueIds.isEmpty()) return
-
+
val placeholders = queueIds.joinToString(",") { "?" }
val args = queueIds.map { it.toString() }.toTypedArray()
-
+
val deleted = writableDatabase.delete(
TABLE_QUEUE,
"id IN ($placeholders)",
args
)
-
+
if (BuildConfig.DEBUG && deleted > 0) {
Log.d(TAG, "Batch deleted $deleted queue items")
}
}
+ fun deleteLocations(locationIds: List<Long>) {
+ if (locationIds.isEmpty()) return
+ val placeholders = locationIds.joinToString(",") { "?" }
+ val args = locationIds.map { it.toString() }.toTypedArray()
+ writableDatabase.delete(TABLE_LOCATIONS, "id IN ($placeholders)", args)
+ }
+
@Synchronized
fun incrementRetryCount(queueId: Long, error: String? = null) {
val stmt = incrementRetryStmt ?: return // Exit if statement is null
@@ -507,7 +514,22 @@ class DatabaseHelper private constructor(context: Context) :
}
fun clearQueue(): Int {
- return writableDatabase.delete(TABLE_QUEUE, null, null)
+ // Collect location IDs before deleting queue entries
+ val locationIds = mutableListOf<Long>()
+ readableDatabase.rawQuery("SELECT location_id FROM $TABLE_QUEUE", null).use { cursor ->
+ while (cursor.moveToNext()) locationIds.add(cursor.getLong(0))
+ }
+
+ val deleted = writableDatabase.delete(TABLE_QUEUE, null, null)
+
+ // Remove the unsent locations
+ if (locationIds.isNotEmpty()) {
+ val placeholders = locationIds.joinToString(",") { "?" }
+ val args = locationIds.map { it.toString() }.toTypedArray()
+ writableDatabase.delete(TABLE_LOCATIONS, "id IN ($placeholders)", args)
+ }
+
+ return deleted
}
fun clearAllLocations(): Int {
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
index 8510e684..383b4789 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
@@ -248,6 +248,13 @@ class SyncManager(
val toRemove = successfulIds + permanentlyFailedIds
if (toRemove.isNotEmpty()) {
dbHelper.removeBatchFromQueue(toRemove)
+
+ // Delete locations of permanently failed items (never sent)
+ val failedLocationIds = chunk
+ .filter { it.queueId in permanentlyFailedIds }
+ .map { it.locationId }
+ dbHelper.deleteLocations(failedLocationIds)
+
totalProcessed += toRemove.size
totalSucceeded += successfulIds.size
totalFailed += permanentlyFailedIds.size