Skip to content

Colota - Android GPS Location tracker

Fix any types and extract map magic numbers to constants

Max Dietrich5344654288d4
diff --git a/apps/mobile/src/components/features/dashboard/DashboardMap.tsx b/apps/mobile/src/components/features/dashboard/DashboardMap.tsx
index db056562..e632ad93 100644
--- a/apps/mobile/src/components/features/dashboard/DashboardMap.tsx
+++ b/apps/mobile/src/components/features/dashboard/DashboardMap.tsx
@@ -12,7 +12,14 @@ import { fonts } from "../../../styles/typography"
import { WifiOff } from "lucide-react-native"
import NativeLocationService from "../../../services/NativeLocationService"
import { useFocusEffect } from "@react-navigation/native"
-import { STATS_REFRESH_IDLE } from "../../../constants"
+import {
+ STATS_REFRESH_IDLE,
+ DEFAULT_MAP_ZOOM,
+ MAX_MAP_ZOOM,
+ GEOFENCE_ZOOM_PADDING,
+ MARKER_ANIMATION_DURATION_MS,
+ MAP_ANIMATION_DURATION_MS
+} from "../../../constants"
import { MapCenterButton } from "../map/MapCenterButton"
import { mapStyles, mapMarkerHelpers } from "../map/mapHtml"
import icon from "../../../assets/icons/icon.png"
@@ -155,7 +162,7 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
],
view: new ol.View({
center: ol.proj.fromLonLat([${lon}, ${lat}]),
- zoom: 17,
+ zoom: ${DEFAULT_MAP_ZOOM},
}),
});
@@ -233,13 +240,13 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
if (data.action === "update_user_pos") {
const newPos = ol.proj.fromLonLat([data.coords.longitude, data.coords.latitude]);
- animateMarker(newPos, 500);
-
+ animateMarker(newPos, ${MARKER_ANIMATION_DURATION_MS});
+
// Only auto-center if user has not manually moved the map
if (isMapCentered()) {
map.getView().animate({
center: newPos,
- duration: 500,
+ duration: ${MARKER_ANIMATION_DURATION_MS},
easing: ol.easing.linear
});
}
@@ -270,7 +277,7 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
if (data.action === "center_map") {
const pos = ol.proj.fromLonLat([data.coords.longitude, data.coords.latitude]);
- map.getView().animate({ center: pos, zoom: 17, duration: 400 });
+ map.getView().animate({ center: pos, zoom: ${DEFAULT_MAP_ZOOM}, duration: ${MAP_ANIMATION_DURATION_MS} });
}
if (data.action === "zoom_to_geofence") {
@@ -279,8 +286,8 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
const extent = circle.getExtent();
map.getView().fit(extent, {
duration: 600,
- padding: [80, 80, 80, 80],
- maxZoom: 18
+ padding: [${GEOFENCE_ZOOM_PADDING}],
+ maxZoom: ${MAX_MAP_ZOOM}
});
}
diff --git a/apps/mobile/src/components/features/dashboard/DatabaseStatistics.tsx b/apps/mobile/src/components/features/dashboard/DatabaseStatistics.tsx
index 11bc400e..c30f0571 100644
--- a/apps/mobile/src/components/features/dashboard/DatabaseStatistics.tsx
+++ b/apps/mobile/src/components/features/dashboard/DatabaseStatistics.tsx
@@ -7,10 +7,11 @@ import { Text, StyleSheet, View } from "react-native"
import { SectionTitle, Card } from "../.."
import { useTheme } from "../../../hooks/useTheme"
import { fonts } from "../../../styles/typography"
+import { DatabaseStats } from "../../../types/global"
import { getQueueColor } from "../../../utils/queueStatus"
type DatabaseStatisticsProps = {
- stats: any
+ stats: DatabaseStats
}
export function DatabaseStatistics({ stats }: DatabaseStatisticsProps) {
diff --git a/apps/mobile/src/components/features/dashboard/QuickAccess.tsx b/apps/mobile/src/components/features/dashboard/QuickAccess.tsx
index 8ba70463..362af1f4 100644
--- a/apps/mobile/src/components/features/dashboard/QuickAccess.tsx
+++ b/apps/mobile/src/components/features/dashboard/QuickAccess.tsx
@@ -17,7 +17,7 @@ interface NavItem {
}
interface QuickAccessProps {
- navigation: any
+ navigation: { navigate: (screen: string) => void }
}
export function QuickAccess({ navigation }: QuickAccessProps) {
diff --git a/apps/mobile/src/components/features/inspector/TrackMap.tsx b/apps/mobile/src/components/features/inspector/TrackMap.tsx
index d1be10dd..ca33d540 100644
--- a/apps/mobile/src/components/features/inspector/TrackMap.tsx
+++ b/apps/mobile/src/components/features/inspector/TrackMap.tsx
@@ -12,6 +12,7 @@ import { fonts } from "../../../styles/typography"
import { MapCenterButton } from "../map/MapCenterButton"
import { mapStyles, mapSpeedColorHelpers } from "../map/mapHtml"
import { getSpeedUnit } from "../../../utils/geo"
+import { MAX_MAP_ZOOM, MAP_ANIMATION_DURATION_MS } from "../../../constants"
interface TrackLocation {
latitude: number
@@ -34,7 +35,6 @@ export function TrackMap({ locations, selectedPoint, colors, isDark }: Props) {
const isMounted = useRef(true)
const [mapReady, setMapReady] = useState(false)
const [isCentered, setIsCentered] = useState(true)
-
useEffect(() => {
return () => {
isMounted.current = false
@@ -235,9 +235,9 @@ export function TrackMap({ locations, selectedPoint, colors, isDark }: Props) {
function fitTrack() {
if (currentExtent) {
map.getView().fit(currentExtent, {
- duration: 400,
+ duration: ${MAP_ANIMATION_DURATION_MS},
padding: [60, 60, 60, 60],
- maxZoom: 18
+ maxZoom: ${MAX_MAP_ZOOM}
});
}
}
diff --git a/apps/mobile/src/constants/index.ts b/apps/mobile/src/constants/index.ts
index 4e708d79..e317e161 100644
--- a/apps/mobile/src/constants/index.ts
+++ b/apps/mobile/src/constants/index.ts
@@ -11,6 +11,15 @@ export const SERVER_TIMEOUT = 5_000
export const SERVER_CHECK_INTERVAL = 5 * 60 * 1000
export const SAVE_SUCCESS_DISPLAY_MS = 2000
+// Map
+export const DEFAULT_MAP_ZOOM = 17
+export const WORLD_MAP_ZOOM = 2
+export const MAX_MAP_ZOOM = 18
+export const GEOFENCE_ZOOM_PADDING = [80, 80, 80, 80] as const
+export const MARKER_ANIMATION_DURATION_MS = 500
+export const MAP_ANIMATION_DURATION_MS = 400
+export const MIN_STATS_INTERVAL_MS = 2000
+
// Thresholds
export const HIGH_QUEUE_THRESHOLD = 50
export const CRITICAL_QUEUE_THRESHOLD = 100
diff --git a/apps/mobile/src/screens/DashboardScreen.tsx b/apps/mobile/src/screens/DashboardScreen.tsx
index d739b514..d4c3c055 100644
--- a/apps/mobile/src/screens/DashboardScreen.tsx
+++ b/apps/mobile/src/screens/DashboardScreen.tsx
@@ -20,7 +20,7 @@ import {
DatabaseStatistics,
WelcomeCard
} from "../components"
-import { STATS_REFRESH_IDLE } from "../constants"
+import { STATS_REFRESH_IDLE, MIN_STATS_INTERVAL_MS } from "../constants"
import { Square, Play } from "lucide-react-native"
import { logger } from "../utils/logger"
@@ -111,7 +111,7 @@ export function DashboardScreen({ navigation }: ScreenProps) {
updateStats()
updatePauseZone()
- const interval = tracking ? Math.max(settings.interval * 1000, 2000) : STATS_REFRESH_IDLE
+ const interval = tracking ? Math.max(settings.interval * 1000, MIN_STATS_INTERVAL_MS) : STATS_REFRESH_IDLE
const statsTimer = setInterval(updateStats, interval)
diff --git a/apps/mobile/src/screens/GeofenceScreen.tsx b/apps/mobile/src/screens/GeofenceScreen.tsx
index cc719cc1..51be290c 100644
--- a/apps/mobile/src/screens/GeofenceScreen.tsx
+++ b/apps/mobile/src/screens/GeofenceScreen.tsx
@@ -5,7 +5,7 @@
import React, { useState, useEffect, useRef, useMemo, useCallback } from "react"
import { View, Text, StyleSheet, TextInput, TouchableOpacity, FlatList, Switch, DeviceEventEmitter } from "react-native"
-import { WebView } from "react-native-webview"
+import { WebView, WebViewMessageEvent } from "react-native-webview"
import { useTheme } from "../hooks/useTheme"
import NativeLocationService from "../services/NativeLocationService"
import { showAlert, showConfirm } from "../services/modalService"
@@ -15,7 +15,15 @@ import { fonts } from "../styles/typography"
import { X, WifiOff } from "lucide-react-native"
import { Container, SectionTitle, Card } from "../components"
import { useFocusEffect } from "@react-navigation/native"
-import { STATS_REFRESH_IDLE } from "../constants"
+import {
+ STATS_REFRESH_IDLE,
+ DEFAULT_MAP_ZOOM,
+ WORLD_MAP_ZOOM,
+ MAX_MAP_ZOOM,
+ GEOFENCE_ZOOM_PADDING,
+ MARKER_ANIMATION_DURATION_MS,
+ MAP_ANIMATION_DURATION_MS
+} from "../constants"
import { MapCenterButton } from "../components/features/map/MapCenterButton"
import { mapStyles, mapMarkerHelpers } from "../components/features/map/mapHtml"
import { logger } from "../utils/logger"
@@ -138,7 +146,7 @@ export function GeofenceScreen({}: ScreenProps) {
}, [geofences, mapReady])
const onMessage = useCallback(
- async (event: any) => {
+ async (event: WebViewMessageEvent) => {
try {
const data = JSON.parse(event.nativeEvent.data)
@@ -252,7 +260,7 @@ export function GeofenceScreen({}: ScreenProps) {
const lon = initialCenter.current.longitude
const lat = initialCenter.current.latitude
const hasRealCoords = lat !== 0 || lon !== 0
- const initialZoom = hasRealCoords ? 17 : 2
+ const initialZoom = hasRealCoords ? DEFAULT_MAP_ZOOM : WORLD_MAP_ZOOM
return `
<!DOCTYPE html>
@@ -362,12 +370,12 @@ export function GeofenceScreen({}: ScreenProps) {
markerEl.style.display = data.tracking ? "block" : "none";
if (data.tracking) {
markerOverlay.setPosition(newPos);
- animateMarker(newPos, 500);
+ animateMarker(newPos, ${MARKER_ANIMATION_DURATION_MS});
// Only auto-center if user has not manually moved the map
if (isMapCentered()) {
map.getView().animate({
center: newPos,
- duration: 500,
+ duration: ${MARKER_ANIMATION_DURATION_MS},
easing: ol.easing.linear
});
}
@@ -397,7 +405,7 @@ export function GeofenceScreen({}: ScreenProps) {
if (data.action === "center_map") {
const pos = ol.proj.fromLonLat([data.coords.longitude, data.coords.latitude]);
- map.getView().animate({ center: pos, zoom: 17, duration: 400 });
+ map.getView().animate({ center: pos, zoom: ${DEFAULT_MAP_ZOOM}, duration: ${MAP_ANIMATION_DURATION_MS} });
}
if (data.action === "zoom_to_geofence") {
@@ -406,8 +414,8 @@ export function GeofenceScreen({}: ScreenProps) {
const extent = circle.getExtent();
map.getView().fit(extent, {
duration: 600,
- padding: [80, 80, 80, 80],
- maxZoom: 18
+ padding: [${GEOFENCE_ZOOM_PADDING}],
+ maxZoom: ${MAX_MAP_ZOOM}
});
}