Skip to content

Colota - Android GPS Location tracker

fix: prevent MapLibre crash on tracking stop, show newest locations first

Max Dietrichad4a0db519bb
diff --git a/.gitignore b/.gitignore
index 9e5bbbce..6622941e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -57,3 +57,4 @@ test-output.txt
# Misc
testers
+apps/mobile/test-checklist.md
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/data/DatabaseHelperSQLiteTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/data/DatabaseHelperSQLiteTest.kt
index b8c8a115..cbdd9bc1 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/data/DatabaseHelperSQLiteTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/data/DatabaseHelperSQLiteTest.kt
@@ -538,8 +538,14 @@ class DatabaseHelperSQLiteTest {
assertEquals(3, page1.size)
assertEquals(3, page2.size)
- // Locations ordered by timestamp DESC — page 1 has newest
- assertNotEquals(page1[0]["timestamp"], page2[0]["timestamp"])
+ // Locations ordered by timestamp DESC — newest first
+ val t1 = page1[0]["timestamp"] as Long
+ val t2 = page1[1]["timestamp"] as Long
+ val t3 = page1[2]["timestamp"] as Long
+ assertTrue("Page 1 should be sorted newest-first", t1 > t2 && t2 > t3)
+ // Page 2 starts after page 1
+ val t4 = page2[0]["timestamp"] as Long
+ assertTrue("Page 2 should continue after page 1", t3 > t4)
}
@Test
diff --git a/apps/mobile/src/components/features/dashboard/DashboardMap.tsx b/apps/mobile/src/components/features/dashboard/DashboardMap.tsx
index d581d46d..029bcc4f 100644
--- a/apps/mobile/src/components/features/dashboard/DashboardMap.tsx
+++ b/apps/mobile/src/components/features/dashboard/DashboardMap.tsx
@@ -123,58 +123,67 @@ export function DashboardMap({ coords, tracking, activeZoneName, activeProfileNa
// Geofence GeoJSON
const geofenceData = useMemo(() => buildGeofencesGeoJSON(geofences, colors), [geofences, colors])
- if (!tracking) {
- return (
- <View style={[styles.stateContainer, { backgroundColor: colors.card, borderRadius: colors.borderRadius }]}>
- <View style={[styles.iconCircle, { backgroundColor: colors.border }]}>
- <Image source={icon} style={styles.icon} />
- </View>
- <Text style={[styles.stateTitle, { color: colors.text }]}>Tracking Disabled</Text>
- <Text style={[styles.stateSubtext, { color: colors.textSecondary }]}>Start tracking to see the map.</Text>
- </View>
- )
- }
-
- if (!isValidCoords(coords)) {
- return (
- <View style={[styles.stateContainer, { backgroundColor: colors.card, borderRadius: colors.borderRadius }]}>
- <ActivityIndicator size="large" color={colors.primary} />
- <Text style={[styles.stateTitle, styles.stateTitleSpaced, { color: colors.text }]}>Searching GPS...</Text>
- <Text style={[styles.stateSubtext, { color: colors.textSecondary }]}>Waiting for GPS signal.</Text>
- </View>
- )
- }
+ const showMap = tracking && isValidCoords(coords)
return (
<View style={[styles.container, { borderRadius: colors.borderRadius }]}>
+ {/* Keep map mounted to avoid MapLibre/Fabric unmount race condition.
+ Hide it behind the placeholder when not tracking. */}
{hasInitialCoords && initialCoords.current ? (
- <ColotaMapView
- ref={mapRef}
- initialCenter={[initialCoords.current.longitude, initialCoords.current.latitude]}
- onRegionDidChange={handleRegionChange}
+ <View style={showMap ? styles.mapVisible : styles.mapHidden} pointerEvents={showMap ? "auto" : "none"}>
+ <ColotaMapView
+ ref={mapRef}
+ initialCenter={[initialCoords.current.longitude, initialCoords.current.latitude]}
+ onRegionDidChange={handleRegionChange}
+ >
+ <GeofenceLayers fills={geofenceData.fills} labels={geofenceData.labels} haloColor={colors.card} />
+
+ {/* Always keep overlay mounted to avoid MapLibre/Fabric unmount race condition */}
+ {coords && <UserLocationOverlay coords={coords} isPaused={!!currentPauseZone} colors={colors} />}
+ </ColotaMapView>
+ </View>
+ ) : null}
+
+ {!tracking && (
+ <View
+ style={[
+ styles.stateContainer,
+ styles.overlay,
+ { backgroundColor: colors.card, borderRadius: colors.borderRadius }
+ ]}
>
- <GeofenceLayers fills={geofenceData.fills} labels={geofenceData.labels} haloColor={colors.card} />
+ <View style={[styles.iconCircle, { backgroundColor: colors.border }]}>
+ <Image source={icon} style={styles.icon} />
+ </View>
+ <Text style={[styles.stateTitle, { color: colors.text }]}>Tracking Disabled</Text>
+ <Text style={[styles.stateSubtext, { color: colors.textSecondary }]}>Start tracking to see the map.</Text>
+ </View>
+ )}
- {/* Accuracy circle + user marker */}
- <UserLocationOverlay coords={coords} isPaused={!!currentPauseZone} colors={colors} />
- </ColotaMapView>
- ) : (
- <View style={[styles.stateContainer, { backgroundColor: colors.card }]}>
+ {tracking && !isValidCoords(coords) && (
+ <View
+ style={[
+ styles.stateContainer,
+ styles.overlay,
+ { backgroundColor: colors.card, borderRadius: colors.borderRadius }
+ ]}
+ >
<ActivityIndicator size="large" color={colors.primary} />
- <Text style={[styles.stateTitle, styles.stateTitleSpaced, { color: colors.text }]}>Loading Map...</Text>
+ <Text style={[styles.stateTitle, styles.stateTitleSpaced, { color: colors.text }]}>Searching GPS...</Text>
+ <Text style={[styles.stateSubtext, { color: colors.textSecondary }]}>Waiting for GPS signal.</Text>
</View>
)}
- <MapCenterButton visible={!isCentered} onPress={handleCenterMe} />
+ {showMap && <MapCenterButton visible={!isCentered} onPress={handleCenterMe} />}
- {isOffline && (
+ {showMap && isOffline && (
<View style={[styles.offlineBanner, { backgroundColor: colors.card }]}>
<WifiOff size={14} color={colors.textSecondary} />
- <Text style={[styles.offlineText, { color: colors.textSecondary }]}>Map tiles unavailable — no internet</Text>
+ <Text style={[styles.offlineText, { color: colors.textSecondary }]}>Map tiles unavailable - no internet</Text>
</View>
)}
- {activeZoneName && (
+ {showMap && activeZoneName && (
<View
style={[
styles.topInfoCard,
@@ -191,7 +200,7 @@ export function DashboardMap({ coords, tracking, activeZoneName, activeProfileNa
</View>
)}
- {!activeZoneName && activeProfileName && (
+ {showMap && !activeZoneName && activeProfileName && (
<View
style={[
styles.topInfoCard,
@@ -211,6 +220,9 @@ export function DashboardMap({ coords, tracking, activeZoneName, activeProfileNa
const styles = StyleSheet.create({
container: { flex: 1, width: "100%", overflow: "hidden" },
+ mapVisible: { flex: 1 },
+ mapHidden: { flex: 1, opacity: 0 },
+ overlay: { position: "absolute", top: 0, left: 0, right: 0, bottom: 0, zIndex: 1 },
stateContainer: {
flex: 1,
justifyContent: "center",
diff --git a/apps/mobile/src/components/features/inspector/LocationTable.tsx b/apps/mobile/src/components/features/inspector/LocationTable.tsx
index 61c4eee7..745e9885 100644
--- a/apps/mobile/src/components/features/inspector/LocationTable.tsx
+++ b/apps/mobile/src/components/features/inspector/LocationTable.tsx
@@ -89,17 +89,17 @@ const getItemLayout = (_: any, index: number) => ({
export function LocationTable({ locations, colors }: Props) {
const speedUnit = useMemo(() => getSpeedUnit(), [])
- const data = useMemo<TableRow[]>(
- () =>
- locations.map((loc, i) => ({
- ...loc,
- delta:
- i > 0 && loc.timestamp && locations[i - 1].timestamp
- ? Math.round(loc.timestamp - locations[i - 1].timestamp!)
- : null
- })),
- [locations]
- )
+ const data = useMemo<TableRow[]>(() => {
+ // Compute deltas in chronological order, then reverse for newest-first display
+ const rows = locations.map((loc, i) => ({
+ ...loc,
+ delta:
+ i > 0 && loc.timestamp && locations[i - 1].timestamp
+ ? Math.round(loc.timestamp - locations[i - 1].timestamp!)
+ : null
+ }))
+ return rows.reverse()
+ }, [locations])
const renderItem = useCallback(
({ item }: { item: TableRow }) => <LocationRow item={item} speedUnit={speedUnit} colors={colors} />,
diff --git a/apps/mobile/src/hooks/useLocationTracking.ts b/apps/mobile/src/hooks/useLocationTracking.ts
index a1644b01..9d8aa4a0 100644
--- a/apps/mobile/src/hooks/useLocationTracking.ts
+++ b/apps/mobile/src/hooks/useLocationTracking.ts
@@ -155,7 +155,6 @@ export function useLocationTracking(settings: Settings): LocationTrackingResult
listenerRef.current = null
}
setTracking(false)
- setCoords(null)
})
return () => sub.remove()
}, [])
@@ -221,7 +220,6 @@ export function useLocationTracking(settings: Settings): LocationTrackingResult
NativeLocationService.stop()
setTracking(false)
- setCoords(null)
}, [])
/**