From 59d4e24ec12fb4ec642fb70317c85b98056c2232 Mon Sep 17 00:00:00 2001
From: Santo Cariotti <santo@dcariotti.me>
Date: Fri, 13 Sep 2024 12:58:24 +0200
Subject: Reproduce an alert audio from remote file

---
 app/(tabs)/index.tsx | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx
index d45f1ac..4168c8f 100644
--- a/app/(tabs)/index.tsx
+++ b/app/(tabs)/index.tsx
@@ -147,8 +147,23 @@ export default function HomeScreen() {
 
   const [sound, setSound] = useState<Audio.Sound | null>(null);
 
-  async function playSound() {
-    const { sound } = await Audio.Sound.createAsync(require('../../assets/sounds/occhio.mp3'));
+  async function playSound(alert: string, level: string) {
+    let levelDigit: string;
+    switch (level) {
+      case "ONE":
+        levelDigit = "1";
+        break;
+      case "TWO":
+        levelDigit = "2";
+        break;
+      case "THREE":
+        levelDigit = "3";
+        break;
+      default: return;
+    }
+    const { sound } = await Audio.Sound.createAsync({
+      uri: `${process.env.EXPO_PUBLIC_API_URL}/assets/sounds/alert-${alert}-text-${levelDigit}.mp3`,
+    });
     setSound(sound);
     await sound.playAsync();
   }
@@ -378,7 +393,7 @@ export default function HomeScreen() {
               "Content-Type": "application/json",
             },
             body: JSON.stringify({
-              query: `{ notifications(seen: false) { id, createdAt, level, alert { text1 text2 text3 } position { movingActivity } } }`,
+              query: `{ notifications(seen: false) { id, createdAt, level, alert { id, text1 text2 text3 } position { movingActivity } } }`,
             }),
           },
         );
@@ -386,9 +401,10 @@ export default function HomeScreen() {
         const data = await response.json();
 
         if (data.data && data.data.notifications && data.data.notifications.length) {
-          setNotification(data.data.notifications[0]);
-          if (data.data.notifications[0].position.movingActivity == "IN_VEHICLE") {
-            playSound();
+          const n = data.data.notifications[0];
+          setNotification(notification);
+          if (n.position.movingActivity == "IN_VEHICLE") {
+            playSound(n.alert.id, n.level);
           }
         } else {
           setNotification(null);
-- 
cgit v1.2.3-18-g5258