From 86b832c69a00ec21cfa3204c72b8261369caf2c0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 6 Apr 2026 13:02:04 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20async=20notification=20s?= =?UTF-8?q?cheduling=20with=20Promise.all?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced sequential `await` loops with parallel `Promise.all` arrays mapped with `try/catch` and `return await` for robustness in `scheduleShiftNotifications` and `schedulePinnedNotifications`. Co-authored-by: TargetMisser <52361977+TargetMisser@users.noreply.github.com> --- package-lock.json | 51 ++++++------- src/screens/FlightScreen.tsx | 142 ++++++++++++++++++++--------------- 2 files changed, 106 insertions(+), 87 deletions(-) diff --git a/package-lock.json b/package-lock.json index a3bbe99..9b54474 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "dependencies": { "@expo/vector-icons": "^15.0.3", "@react-native-async-storage/async-storage": "2.2.0", - "@react-native-picker/picker": "2.11.1", + "@react-native-picker/picker": "2.11.4", "@types/tesseract.js": "^0.0.2", "expo": "~54.0.0", "expo-blur": "~15.0.8", @@ -23,18 +23,19 @@ "expo-linear-gradient": "~15.0.8", "expo-location": "~19.0.8", "expo-notifications": "~0.32.16", + "expo-secure-store": "~15.0.5", "expo-status-bar": "~3.0.9", "react": "19.1.0", "react-native": "0.81.5", "react-native-android-widget": "^0.20.1", "react-native-calendars": "^1.1314.0", - "react-native-webview": "13.15.0", + "react-native-webview": "13.16.1", "tesseract.js": "^7.0.0" }, "devDependencies": { "@react-native-community/cli": "^20.1.3", "@types/react": "~19.1.10", - "pdfjs-dist": "^5.5.207", + "pdfjs-dist": "^5.6.205", "typescript": "~5.9.2" } }, @@ -80,7 +81,6 @@ "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.29.0.tgz", "integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==", "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.29.0", "@babel/generator": "^7.29.0", @@ -1492,7 +1492,6 @@ "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.2.tgz", "integrity": "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==", "license": "MIT", - "peer": true, "engines": { "node": ">=6.9.0" } @@ -2431,7 +2430,6 @@ "integrity": "sha512-sLo8cu9JyFNfuuF1C+8NJ4DHE/PEFaXGd4enkcxi/OJjGG8+sOQrdjNQ4i+cVh/2c+ah1mEMwsYjc3z0+/MqSg==", "devOptional": true, "license": "MIT", - "peer": true, "dependencies": { "@react-native-community/cli-clean": "20.1.3", "@react-native-community/cli-config": "20.1.3", @@ -2996,9 +2994,9 @@ } }, "node_modules/@react-native-picker/picker": { - "version": "2.11.1", - "resolved": "https://registry.npmjs.org/@react-native-picker/picker/-/picker-2.11.1.tgz", - "integrity": "sha512-ThklnkK4fV3yynnIIRBkxxjxR4IFbdMNJVF6tlLdOJ/zEFUEFUEdXY0KmH0iYzMwY8W4/InWsLiA7AkpAbnexA==", + "version": "2.11.4", + "resolved": "https://registry.npmjs.org/@react-native-picker/picker/-/picker-2.11.4.tgz", + "integrity": "sha512-Kf8h1AMnBo54b1fdiVylP2P/iFcZqzpMYcglC28EEFB1DEnOjsNr6Ucqc+3R9e91vHxEDnhZFbYDmAe79P2gjA==", "license": "MIT", "workspaces": [ "example" @@ -3399,7 +3397,6 @@ "integrity": "sha512-Qec1E3mhALmaspIrhWt9jkQMNdw6bReVu64mjvhbhq2NFPftLPVr+l1SZgmw/66WwBNpDh7ao5AT6gF5v41PFA==", "devOptional": true, "license": "MIT", - "peer": true, "dependencies": { "csstype": "^3.0.2" } @@ -4109,7 +4106,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -5014,7 +5010,6 @@ "resolved": "https://registry.npmjs.org/expo/-/expo-54.0.33.tgz", "integrity": "sha512-3yOEfAKqo+gqHcV8vKcnq0uA5zxlohnhA3fu4G43likN8ct5ZZ3LjAh9wDdKteEkoad3tFPvwxmXW711S5OHUw==", "license": "MIT", - "peer": true, "dependencies": { "@babel/runtime": "^7.20.0", "@expo/cli": "54.0.23", @@ -5130,7 +5125,6 @@ "resolved": "https://registry.npmjs.org/expo-font/-/expo-font-14.0.11.tgz", "integrity": "sha512-ga0q61ny4s/kr4k8JX9hVH69exVSIfcIc19+qZ7gt71Mqtm7xy2c6kwsPTCyhBW2Ro5yXTT8EaZOpuRi35rHbg==", "license": "MIT", - "peer": true, "dependencies": { "fontfaceobserver": "^2.1.0" }, @@ -5239,6 +5233,15 @@ "react-native": "*" } }, + "node_modules/expo-secure-store": { + "version": "15.0.8", + "resolved": "https://registry.npmjs.org/expo-secure-store/-/expo-secure-store-15.0.8.tgz", + "integrity": "sha512-lHnzvRajBu4u+P99+0GEMijQMFCOYpWRO4dWsXSuMt77+THPIGjzNvVKrGSl6mMrLsfVaKL8BpwYZLGlgA+zAw==", + "license": "MIT", + "peerDependencies": { + "expo": "*" + } + }, "node_modules/expo-server": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/expo-server/-/expo-server-1.0.5.tgz", @@ -8412,16 +8415,16 @@ } }, "node_modules/pdfjs-dist": { - "version": "5.5.207", - "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-5.5.207.tgz", - "integrity": "sha512-WMqqw06w1vUt9ZfT0gOFhMf3wHsWhaCrxGrckGs5Cci6ybDW87IvPaOd2pnBwT6BJuP/CzXDZxjFgmSULLdsdw==", + "version": "5.6.205", + "resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-5.6.205.tgz", + "integrity": "sha512-tlUj+2IDa7G1SbvBNN74UHRLJybZDWYom+k6p5KIZl7huBvsA4APi6mKL+zCxd3tLjN5hOOEE9Tv7VdzO88pfg==", "dev": true, "license": "Apache-2.0", "engines": { "node": ">=20.19.0 || >=22.13.0 || >=24" }, "optionalDependencies": { - "@napi-rs/canvas": "^0.1.95", + "@napi-rs/canvas": "^0.1.96", "node-readable-to-web-readable-stream": "^0.4.2" } }, @@ -8436,7 +8439,6 @@ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.3.tgz", "integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==", "license": "MIT", - "peer": true, "engines": { "node": ">=12" }, @@ -8716,7 +8718,6 @@ "resolved": "https://registry.npmjs.org/react/-/react-19.1.0.tgz", "integrity": "sha512-FS+XFBNvn3GTAWq26joslQgWNoFu08F4kl0J4CgdNKADkdSGXQyTCnKteIAJy96Br6YbpEU1LSzV5dYtjMkMDg==", "license": "MIT", - "peer": true, "engines": { "node": ">=0.10.0" } @@ -8742,7 +8743,6 @@ "resolved": "https://registry.npmjs.org/react-native/-/react-native-0.81.5.tgz", "integrity": "sha512-1w+/oSjEXZjMqsIvmkCRsOc8UBYv163bTWKTI8+1mxztvQPhCRYGTvZ/PL1w16xXHneIj/SLGfxWg2GWN2uexw==", "license": "MIT", - "peer": true, "dependencies": { "@jest/create-cache-key-function": "^29.7.0", "@react-native/assets-registry": "0.81.5", @@ -8849,11 +8849,10 @@ "license": "MIT" }, "node_modules/react-native-webview": { - "version": "13.15.0", - "resolved": "https://registry.npmjs.org/react-native-webview/-/react-native-webview-13.15.0.tgz", - "integrity": "sha512-Vzjgy8mmxa/JO6l5KZrsTC7YemSdq+qB01diA0FqjUTaWGAGwuykpJ73MDj3+mzBSlaDxAEugHzTtkUQkQEQeQ==", + "version": "13.16.1", + "resolved": "https://registry.npmjs.org/react-native-webview/-/react-native-webview-13.16.1.tgz", + "integrity": "sha512-If0eHhoEdOYDcHsX+xBFwHMbWBGK1BvGDQDQdVkwtSIXiq1uiqjkpWVP2uQ1as94J0CzvFE9PUNDuhiX0Z6ubw==", "license": "MIT", - "peer": true, "dependencies": { "escape-string-regexp": "^4.0.0", "invariant": "2.2.4" @@ -8952,7 +8951,6 @@ "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.14.2.tgz", "integrity": "sha512-jCvmsr+1IUSMUyzOkRcvnVbX3ZYC6g9TDrDbFuFmRDq7PD4yaGbLKNQL6k2jnArV8hjYxh7hVhAZB6s9HDGpZA==", "license": "MIT", - "peer": true, "engines": { "node": ">=0.10.0" } @@ -10211,9 +10209,8 @@ "version": "5.9.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/src/screens/FlightScreen.tsx b/src/screens/FlightScreen.tsx index 155fb94..c76c764 100644 --- a/src/screens/FlightScreen.tsx +++ b/src/screens/FlightScreen.tsx @@ -113,47 +113,58 @@ async function scheduleShiftNotifications( ): Promise { await cancelPreviousNotifications(); const now = Date.now() / 1000; - const newIds: string[] = []; - for (const item of shiftFlights) { - const ts: number | undefined = item.flight?.time?.scheduled?.arrival; - if (!ts) continue; - const secondsUntilNotify = ts - 15 * 60 - now; // 15 min prima - if (secondsUntilNotify <= 0) continue; // già passato + // Promise.all to schedule flight notifications in parallel + const flightPromises = shiftFlights.map(async (item) => { + try { + const ts: number | undefined = item.flight?.time?.scheduled?.arrival; + if (!ts) return undefined; + const secondsUntilNotify = ts - 15 * 60 - now; // 15 min prima + if (secondsUntilNotify <= 0) return undefined; // già passato + + const flightNumber = item.flight?.identification?.number?.default || 'N/A'; + const airline = item.flight?.airline?.name || 'Sconosciuta'; + const origin = item.flight?.airport?.origin?.name + || item.flight?.airport?.origin?.code?.iata + || 'N/A'; + const arrivalTime = new Date(ts * 1000).toLocaleTimeString('it-IT', { hour: '2-digit', minute: '2-digit' }); + + return await Notifications.scheduleNotificationAsync({ + content: { + title: `✈️ Arrivo tra 15 min — ${flightNumber}`, + body: `${airline} da ${origin} · atterraggio alle ${arrivalTime}`, + sound: true, + data: { flightNumber, ts }, + }, + trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secondsUntilNotify), repeats: false }, + }); + } catch (e) { + console.warn('[scheduleShiftNotifications] error scheduling', e); + return undefined; + } + }); - const flightNumber = item.flight?.identification?.number?.default || 'N/A'; - const airline = item.flight?.airline?.name || 'Sconosciuta'; - const origin = item.flight?.airport?.origin?.name - || item.flight?.airport?.origin?.code?.iata - || 'N/A'; - const arrivalTime = new Date(ts * 1000).toLocaleTimeString('it-IT', { hour: '2-digit', minute: '2-digit' }); - - const id = await Notifications.scheduleNotificationAsync({ - content: { - title: `✈️ Arrivo tra 15 min — ${flightNumber}`, - body: `${airline} da ${origin} · atterraggio alle ${arrivalTime}`, - sound: true, - data: { flightNumber, ts }, - }, - trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secondsUntilNotify), repeats: false }, - }); - newIds.push(id); - } + const idsArr = await Promise.all(flightPromises); + const newIds = idsArr.filter((id): id is string => id !== undefined); // Notifica fine turno const secondsUntilEnd = shiftEnd - now; if (secondsUntilEnd > 0) { - const endTime = new Date(shiftEnd * 1000).toLocaleTimeString('it-IT', { hour: '2-digit', minute: '2-digit' }); - const endId = await Notifications.scheduleNotificationAsync({ - content: { - title: '🏁 Turno terminato', - body: `Buon lavoro! Il tuo turno delle ${endTime} è concluso.`, - sound: true, - data: { type: 'shift_end' }, - }, - trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secondsUntilEnd), repeats: false }, - }); - newIds.push(endId); + try { + const endTime = new Date(shiftEnd * 1000).toLocaleTimeString('it-IT', { hour: '2-digit', minute: '2-digit' }); + const endId = await Notifications.scheduleNotificationAsync({ + content: { + title: '🏁 Turno terminato', + body: `Buon lavoro! Il tuo turno delle ${endTime} è concluso.`, + sound: true, + data: { type: 'shift_end' }, + }, + trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secondsUntilEnd), repeats: false }, + }); + newIds.push(endId); + } catch (e) { + console.warn('[scheduleShiftNotifications] error scheduling shift end', e); + } } await AsyncStorage.setItem(NOTIF_IDS_KEY, JSON.stringify(newIds)); @@ -171,7 +182,7 @@ async function cancelPinnedNotifications() { async function schedulePinnedNotifications(item: any, tab: 'arrivals' | 'departures'): Promise { await cancelPinnedNotifications(); const now = Date.now() / 1000; - const ids: string[] = []; + let ids: string[] = []; const flightNumber = item.flight?.identification?.number?.default || 'N/A'; const airline = item.flight?.airline?.name || 'Sconosciuta'; @@ -183,16 +194,20 @@ async function schedulePinnedNotifications(item: any, tab: 'arrivals' | 'departu const arrTime = new Date(ts * 1000).toLocaleTimeString('it-IT', { hour: '2-digit', minute: '2-digit' }); const secsUntil = ts - 15 * 60 - now; if (secsUntil > 0) { - const id = await Notifications.scheduleNotificationAsync({ - content: { - title: `📌 Arrivo tra 15 min — ${flightNumber}`, - body: `${airline} da ${origin} · atterraggio alle ${arrTime}`, - sound: true, - data: { flightNumber, ts, pinned: true }, - }, - trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secsUntil), repeats: false }, - }); - ids.push(id); + try { + const id = await Notifications.scheduleNotificationAsync({ + content: { + title: `📌 Arrivo tra 15 min — ${flightNumber}`, + body: `${airline} da ${origin} · atterraggio alle ${arrTime}`, + sound: true, + data: { flightNumber, ts, pinned: true }, + }, + trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secsUntil), repeats: false }, + }); + ids.push(id); + } catch (e) { + console.warn('[schedulePinnedNotifications] error scheduling arrival', e); + } } } else { const ts = item.flight?.time?.scheduled?.departure; @@ -208,20 +223,27 @@ async function schedulePinnedNotifications(item: any, tab: 'arrivals' | 'departu { offset: 10, title: `📌 Partenza tra 10 min — ${flightNumber}`, body: `${airline} → ${dest} · partenza alle ${depTime}` }, ]; - for (const phase of phases) { - const secsUntil = ts - phase.offset * 60 - now; - if (secsUntil <= 0) continue; - const id = await Notifications.scheduleNotificationAsync({ - content: { - title: phase.title, - body: phase.body, - sound: true, - data: { flightNumber, ts, pinned: true }, - }, - trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secsUntil), repeats: false }, - }); - ids.push(id); - } + const phasePromises = phases.map(async (phase) => { + try { + const secsUntil = ts - phase.offset * 60 - now; + if (secsUntil <= 0) return undefined; + return await Notifications.scheduleNotificationAsync({ + content: { + title: phase.title, + body: phase.body, + sound: true, + data: { flightNumber, ts, pinned: true }, + }, + trigger: { type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL, seconds: Math.round(secsUntil), repeats: false }, + }); + } catch (e) { + console.warn('[schedulePinnedNotifications] error scheduling phase', e); + return undefined; + } + }); + + const phaseIds = await Promise.all(phasePromises); + ids = phaseIds.filter((id): id is string => id !== undefined); } if (ids.length > 0) {