Colota - Android GPS Location tracker
Add offline banner to maps and network check to server connection
Max Dietrich4ae7d89fd60a
diff --git a/apps/mobile/android/app/src/main/java/com/colota/LocationServiceModule.kt b/apps/mobile/android/app/src/main/java/com/colota/LocationServiceModule.ktindex 1bcf694c..4367834a 100644--- a/apps/mobile/android/app/src/main/java/com/colota/LocationServiceModule.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/LocationServiceModule.kt@@ -32,6 +32,7 @@ class LocationServiceModule(reactContext: ReactApplicationContext) :private val deviceInfo = DeviceInfoHelper(reactContext)private val geofenceHelper = GeofenceHelper(reactContext)private val secureStorage = SecureStorageHelper.getInstance(reactContext)+ private val networkManager = NetworkManager(reactContext)// Coroutine scope for async operationsprivate val moduleScope = CoroutineScope(Dispatchers.IO + SupervisorJob())@@ -455,6 +456,15 @@ class LocationServiceModule(reactContext: ReactApplicationContext) :}}+ // ==============================================================+ // NETWORK+ // ==============================================================++ @ReactMethod+ fun isNetworkAvailable(promise: Promise) {+ promise.resolve(networkManager.isNetworkAvailable())+ }+// ==============================================================// UTILITY// ==============================================================diff --git a/apps/mobile/src/components/features/dashboard/DashboardMap.tsx b/apps/mobile/src/components/features/dashboard/DashboardMap.tsxindex 715f654a..4a70eeb0 100644--- a/apps/mobile/src/components/features/dashboard/DashboardMap.tsx+++ b/apps/mobile/src/components/features/dashboard/DashboardMap.tsx@@ -9,7 +9,10 @@ import { WebView } from "react-native-webview"import { LocationCoords } from "../../../types/global"import { useTheme } from "../../../hooks/useTheme"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 { MapCenterButton } from "../map/MapCenterButton"import icon from "../../../assets/icons/icon.png"@@ -31,6 +34,18 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {const [hasInitialCoords, setHasInitialCoords] = useState(false)const [currentSilentZone, setCurrentSilentZone] = useState<string | null>(null)+ const [isOffline, setIsOffline] = useState(false)++ useFocusEffect(+ useCallback(() => {+ const check = () => {+ NativeLocationService.isNetworkAvailable().then((available) => setIsOffline(!available))+ }+ check()+ const interval = setInterval(check, STATS_REFRESH_IDLE)+ return () => clearInterval(interval)+ }, [])+ )const isValidCoords = (c: LocationCoords | null): c is LocationCoords => {return c !== null && c.latitude !== 0 && c.longitude !== 0@@ -499,6 +514,13 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {<MapCenterButton visible={!isCentered} onPress={handleCenterMe} />+ {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>+ </View>+ )}+{activeZoneName && (<Viewstyle={[@@ -556,5 +578,23 @@ const styles = StyleSheet.create({zIndex: 5},infoTitle: { fontSize: 16, ...fonts.bold, marginBottom: 2 },- infoSub: { fontSize: 13 }+ infoSub: { fontSize: 13 },+ offlineBanner: {+ position: "absolute",+ bottom: 90,+ left: 14,+ right: 14,+ flexDirection: "row",+ alignItems: "center",+ gap: 8,+ padding: 12,+ borderRadius: 12,+ elevation: 8,+ shadowOpacity: 0.2,+ zIndex: 5+ },+ offlineText: {+ fontSize: 13,+ ...fonts.medium+ }})diff --git a/apps/mobile/src/components/features/dashboard/ServerConnection.tsx b/apps/mobile/src/components/features/dashboard/ServerConnection.tsxindex 53a659e4..ec8bc12e 100644--- a/apps/mobile/src/components/features/dashboard/ServerConnection.tsx+++ b/apps/mobile/src/components/features/dashboard/ServerConnection.tsx@@ -19,7 +19,7 @@ export function ServerConnection({ endpoint, navigation }: ServerConnectionPropsconst { settings } = useTracking()const isOffline = settings.isOfflineMode- const [serverStatus, setServerStatus] = useState<ServerStatus | "offline" | null>(null)+ const [serverStatus, setServerStatus] = useState<ServerStatus | "offline" | "deviceOffline" | null>(null)const hasChecked = useRef(false)@@ -30,6 +30,13 @@ export function ServerConnection({ endpoint, navigation }: ServerConnectionPropsreturn}+ const networkAvailable = await NativeLocationService.isNetworkAvailable()+ if (!networkAvailable) {+ setServerStatus("deviceOffline")+ hasChecked.current = true+ return+ }+if (!endpoint) {setServerStatus("notConfigured")hasChecked.current = true@@ -99,12 +106,15 @@ export function ServerConnection({ endpoint, navigation }: ServerConnectionPropsconnected: { color: colors.success, label: "Connected" },error: { color: colors.error, label: "Unreachable" },notConfigured: { color: colors.warning, label: "No endpoint" },+ deviceOffline: { color: colors.textSecondary, label: "Device offline" },offline: { color: colors.textSecondary, label: "Offline" },loading: { color: colors.textLight, label: "Checking" }}if (serverStatus === null) return statusMap.loading- return isOffline ? statusMap.offline : statusMap[serverStatus as ServerStatus] || statusMap.error+ if (isOffline) return statusMap.offline+ if (serverStatus === "deviceOffline") return statusMap.deviceOffline+ return statusMap[serverStatus as ServerStatus] || statusMap.error}, [serverStatus, colors, isOffline])return (diff --git a/apps/mobile/src/screens/GeofenceScreen.tsx b/apps/mobile/src/screens/GeofenceScreen.tsxindex 2797a6dc..03bf0083 100644--- a/apps/mobile/src/screens/GeofenceScreen.tsx+++ b/apps/mobile/src/screens/GeofenceScreen.tsx@@ -21,8 +21,10 @@ import NativeLocationService from "../services/NativeLocationService"import { Geofence, ScreenProps } from "../types/global"import { useTracking } from "../contexts/TrackingProvider"import { fonts } from "../styles/typography"-import { X } from "lucide-react-native"+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 { MapCenterButton } from "../components/features/map/MapCenterButton"export function GeofenceScreen({}: ScreenProps) {@@ -38,6 +40,7 @@ export function GeofenceScreen({}: ScreenProps) {const [mapReady, setMapReady] = useState(false)const [hasInitialCoords, setHasInitialCoords] = useState(false)const [currentSilentZone, setCurrentSilentZone] = useState<string | null>(null)+ const [isOffline, setIsOffline] = useState(false)const webviewRef = useRef<WebView>(null)const initialCenter = useRef<{@@ -46,6 +49,17 @@ export function GeofenceScreen({}: ScreenProps) {accuracy: number} | null>(null)+ useFocusEffect(+ useCallback(() => {+ const check = () => {+ NativeLocationService.isNetworkAvailable().then((available) => setIsOffline(!available))+ }+ check()+ const interval = setInterval(check, STATS_REFRESH_IDLE)+ return () => clearInterval(interval)+ }, [])+ )+// Set initial map center: use live coords, fall back to last known from DB, then defaultuseEffect(() => {if (hasInitialCoords) return@@ -617,6 +631,15 @@ export function GeofenceScreen({}: ScreenProps) {/><MapCenterButton visible={!isCentered && tracking} onPress={handleCenterMe} />++ {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>+ </View>+ )}</View><FlatList@@ -764,5 +787,23 @@ const styles = StyleSheet.create({textAlign: "center",maxWidth: 260,lineHeight: 18+ },+ offlineBanner: {+ position: "absolute",+ top: 14,+ left: 66,+ right: 14,+ flexDirection: "row",+ alignItems: "center",+ gap: 8,+ padding: 12,+ borderRadius: 12,+ elevation: 8,+ shadowOpacity: 0.2,+ zIndex: 5+ },+ offlineText: {+ fontSize: 13,+ ...fonts.medium}})diff --git a/apps/mobile/src/services/NativeLocationService.ts b/apps/mobile/src/services/NativeLocationService.tsindex a02fbe0e..21ee1b13 100644--- a/apps/mobile/src/services/NativeLocationService.ts+++ b/apps/mobile/src/services/NativeLocationService.ts@@ -338,6 +338,18 @@ class NativeLocationService {return this.safeExecute(() => LocationServiceModule.getAllSettings(), {}, "getAllSettings failed")}+ // ============================================================================+ // NETWORK+ // ============================================================================++ /**+ * Checks if the device has an active internet connection+ */+ static async isNetworkAvailable(): Promise<boolean> {+ this.ensureModule()+ return this.safeExecute(() => LocationServiceModule.isNetworkAvailable(), false, "isNetworkAvailable failed")+ }+// ============================================================================// BATTERY OPTIMIZATION// ============================================================================