Colota - Android GPS Location tracker
feat: add OpenLayers asset caching for improved performance
Max Dietrich208fec60f691
diff --git a/src/components/features/dashboard/DashboardMap.tsx b/src/components/features/dashboard/DashboardMap.tsxindex 6e727201..f1e237ed 100644--- a/src/components/features/dashboard/DashboardMap.tsx+++ b/src/components/features/dashboard/DashboardMap.tsx@@ -25,6 +25,7 @@ import { useTheme } from "../../../hooks/useTheme";import NativeLocationService from "../../../services/NativeLocationService";import { SvgXml } from "react-native-svg";import RNFS from "react-native-fs";+import { loadOpenLayersAssets } from "../../../helpers/openlayersLoader"type Props = {coords: LocationCoords | null;@@ -145,31 +146,11 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {}, [coords]);useEffect(() => {- const loadAssets = async () => {- try {- let css, js;-- if (Platform.OS === "android") {- // Read from Android Assets folder (app/src/main/assets)- css = await RNFS.readFileAssets("openlayers/ol.css", "utf8");- js = await RNFS.readFileAssets("openlayers/ol.js", "utf8");- } else {- // iOS uses MainBundlePath- const cssPath = `${RNFS.MainBundlePath}/openlayers/ol.css`;- const jsPath = `${RNFS.MainBundlePath}/openlayers/ol.js`;- css = await RNFS.readFile(cssPath, "utf8");- js = await RNFS.readFile(jsPath, "utf8");- }-- setOlCss(css);- setOlJs(js);- } catch (err) {- console.error("Failed to load OpenLayers assets:", err);- }- };-- loadAssets();- }, []);+ loadOpenLayersAssets().then(({ css, js }) => {+ setOlCss(css);+ setOlJs(js);+ });+}, []);const html = useMemo(() => {if (!hasInitialCoords || !initialCoords.current || !olCss || !olJs)diff --git a/src/helpers/openlayersLoader.ts b/src/helpers/openlayersLoader.tsnew file mode 100644index 00000000..cf03005d--- /dev/null+++ b/src/helpers/openlayersLoader.ts@@ -0,0 +1,40 @@+import { Platform } from "react-native";+import RNFS from "react-native-fs";++let cachedOlCss: string | null = null;+let cachedOlJs: string | null = null;+let loadingPromise: Promise<{css: string, js: string}> | null = null;++export async function loadOpenLayersAssets() {+ // Return cached if available+ if (cachedOlCss && cachedOlJs) {+ return { css: cachedOlCss, js: cachedOlJs };+ }++ // Return existing promise if already loading+ if (loadingPromise) {+ return loadingPromise;+ }++ // Start loading+ loadingPromise = (async () => {+ try {+ if (Platform.OS === "android") {+ cachedOlCss = await RNFS.readFileAssets("openlayers/ol.css", "utf8");+ cachedOlJs = await RNFS.readFileAssets("openlayers/ol.js", "utf8");+ } else {+ const cssPath = `${RNFS.MainBundlePath}/openlayers/ol.css`;+ const jsPath = `${RNFS.MainBundlePath}/openlayers/ol.js`;+ cachedOlCss = await RNFS.readFile(cssPath, "utf8");+ cachedOlJs = await RNFS.readFile(jsPath, "utf8");+ }+ return { css: cachedOlCss, js: cachedOlJs };+ } catch (err) {+ console.error("Failed to load OpenLayers assets:", err);+ loadingPromise = null; // Reset to allow retry+ throw err;+ }+ })();++ return loadingPromise;+}\ No newline at end of filediff --git a/src/screens/GeofenceScreen.tsx b/src/screens/GeofenceScreen.tsxindex 9349de44..d18f4eff 100644--- a/src/screens/GeofenceScreen.tsx+++ b/src/screens/GeofenceScreen.tsx@@ -30,6 +30,7 @@ import { Geofence, ScreenProps, LocationCoords } from "../types/global";import { useTracking } from "../contexts/TrackingProvider";import { Container, SectionTitle, Card } from "../components";import RNFS from "react-native-fs";+import { loadOpenLayersAssets } from "../helpers/openlayersLoader";export function GeofenceScreen({}: ScreenProps) {const { coords, tracking } = useTracking();@@ -243,31 +244,11 @@ export function GeofenceScreen({}: ScreenProps) {);useEffect(() => {- const loadAssets = async () => {- try {- let css, js;-- if (Platform.OS === "android") {- // Read from Android Assets folder (app/src/main/assets)- css = await RNFS.readFileAssets("openlayers/ol.css", "utf8");- js = await RNFS.readFileAssets("openlayers/ol.js", "utf8");- } else {- // iOS uses MainBundlePath- const cssPath = `${RNFS.MainBundlePath}/openlayers/ol.css`;- const jsPath = `${RNFS.MainBundlePath}/openlayers/ol.js`;- css = await RNFS.readFile(cssPath, "utf8");- js = await RNFS.readFile(jsPath, "utf8");- }-- setOlCss(css);- setOlJs(js);- } catch (err) {- console.error("Failed to load OpenLayers assets:", err);- }- };-- loadAssets();- }, []);+ loadOpenLayersAssets().then(({ css, js }) => {+ setOlCss(css);+ setOlJs(js);+ });+ }, [])const html = useMemo(() => {if (!hasInitialCoords || !initialCoords.current || !olCss || !olJs)