From 6278b157578527ae784a844d4ff1b7f82afcca0e Mon Sep 17 00:00:00 2001
From: Santo Cariotti <santo@dcariotti.me>
Date: Tue, 10 Sep 2024 11:36:52 +0200
Subject: Track the moving activity using the speed location field

---
 app/(tabs)/index.tsx | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

(limited to 'app/(tabs)/index.tsx')

diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx
index b5bddcc..251bb90 100644
--- a/app/(tabs)/index.tsx
+++ b/app/(tabs)/index.tsx
@@ -236,25 +236,36 @@ export default function HomeScreen() {
     return `${date.toDateString()} ${date.getHours()}:${(date.getMinutes() < 10 ? "0" : "") + date.getMinutes()}`;
   };
 
-  const updateLocation = async (coords: LatLng) => {
+  const updateLocation = async (coords: LatLng, speed: number) => {
     setCoordinates({
       latitude: coords.latitude,
       longitude: coords.longitude,
     });
 
     if (region.longitude == 0 && region.latitude == 0) {
-    setRegion({
-      latitude: coords.latitude,
-      longitude: coords.longitude,
-      latitudeDelta: 0.03,
-      longitudeDelta: 0.03,
-    });
+      setRegion({
+        latitude: coords.latitude,
+        longitude: coords.longitude,
+        latitudeDelta: 0.03,
+        longitudeDelta: 0.03,
+      });
     }
 
 
     if (!token || !userId) return;
 
     try {
+      let movingActivity: string;
+      if (speed == 0) {
+        movingActivity = "STILL";
+      } else if (speed < 1.5) {
+        movingActivity = "WALKING";
+      } else if (speed >= 1.5 && speed < 5) {
+        movingActivity = "RUNNING";
+      } else {
+        movingActivity = "IN_VEHICLE";
+      }
+
       const response = await fetch(
         `${process.env.EXPO_PUBLIC_API_URL}/graphql`,
         {
@@ -275,7 +286,7 @@ export default function HomeScreen() {
               input: {
                 latitude: coords.latitude,
                 longitude: coords.longitude,
-                movingActivity: 'STILL',
+                movingActivity,
               },
             },
           }),
@@ -357,7 +368,7 @@ export default function HomeScreen() {
         if (status === "granted") {
           setInterval(async () => {
             const location = await Location.getCurrentPositionAsync({});
-            updateLocation(location.coords);
+            updateLocation(location.coords, location.coords.speed);
           }, 2000);
 
           await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
-- 
cgit v1.2.3-71-g8e6c

