From b726305a441081a72f5a960d3073ecc5b26284e1 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 16 Dec 2024 17:52:08 +0100 Subject: [PATCH] Importation automatique d'un train depuis Rail Planner par partage de l'URL d'un trajet --- client/app.json | 4 +- client/app/(tabs)/train.tsx | 21 ++++++++- client/app/_layout.tsx | 39 ++++++++-------- client/components/providers/GameProvider.tsx | 12 +++++ client/package-lock.json | 47 ++++++++------------ client/package.json | 2 +- 6 files changed, 74 insertions(+), 51 deletions(-) diff --git a/client/app.json b/client/app.json index 834c2e4..7afe28e 100644 --- a/client/app.json +++ b/client/app.json @@ -5,7 +5,7 @@ "version": "1.0.0", "orientation": "portrait", "icon": "./assets/images/icon.png", - "scheme": "myapp", + "scheme": "traintrapemoi", "userInterfaceStyle": "automatic", "newArchEnabled": true, "ios": { @@ -45,7 +45,7 @@ "expo-notifications", "expo-router", "expo-secure-store", - "expo-share-extension", + "expo-share-intent", [ "expo-splash-screen", { diff --git a/client/app/(tabs)/train.tsx b/client/app/(tabs)/train.tsx index fd63719..a682353 100644 --- a/client/app/(tabs)/train.tsx +++ b/client/app/(tabs)/train.tsx @@ -5,7 +5,8 @@ import { useTrain } from '@/hooks/useTrain' import { TrainTrip } from '@/utils/features/train/trainSlice' import { FontAwesome6 } from '@expo/vector-icons' import { useQueryClient } from '@tanstack/react-query' -import { useMemo, useState } from 'react' +import { useShareIntentContext } from 'expo-share-intent' +import { useEffect, useMemo, useState } from 'react' import { FlatList, StyleSheet } from 'react-native' import { Button, Dialog, Divider, FAB, HelperText, List, MD3Colors, Portal, Snackbar, Surface, Text, TextInput } from 'react-native-paper' @@ -38,6 +39,24 @@ export default function TrainScreen() { const trains = useTrain() + + const { hasShareIntent, shareIntent, resetShareIntent } = useShareIntentContext() + useEffect(() => { + console.log(hasShareIntent, shareIntent) + if (hasShareIntent) { + resetShareIntent() + if (!shareIntent.text || !shareIntent.text.includes("eurailapp.com/share")) + return + const parsedTrainId = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/.exec(shareIntent.text)?.[0] + if (parsedTrainId) + addTrainMutation.mutate(parsedTrainId) + else { + setErrorVisible(true) + setError([400, "Impossible de récupérer l'identifiant du train à ajouter"]) + } + } + }, [hasShareIntent]) + return ( diff --git a/client/app/_layout.tsx b/client/app/_layout.tsx index a3c9692..dc2e366 100644 --- a/client/app/_layout.tsx +++ b/client/app/_layout.tsx @@ -6,6 +6,7 @@ import { QueryClient } from '@tanstack/react-query' import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister' import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client' import { Stack, useNavigationContainerRef } from 'expo-router' +import { ShareIntentProvider } from 'expo-share-intent' import { StatusBar } from 'expo-status-bar' import { MD3DarkTheme, MD3LightTheme, PaperProvider } from 'react-native-paper' import { Provider as StoreProvider } from 'react-redux' @@ -42,24 +43,26 @@ export default function RootLayout() { client={queryClient} persistOptions={{ persister: asyncStoragePersister }} onSuccess={() => queryClient.resumePausedMutations().then(() => queryClient.invalidateQueries())}> - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + ) diff --git a/client/components/providers/GameProvider.tsx b/client/components/providers/GameProvider.tsx index 376e189..32d83de 100644 --- a/client/components/providers/GameProvider.tsx +++ b/client/components/providers/GameProvider.tsx @@ -8,6 +8,8 @@ import { isAuthValid } from '@/utils/features/auth/authSlice' import { ChallengeAction, ChallengeActionPayload } from '@/utils/features/challengeActions/challengeActionsSlice' import { Challenge } from '@/utils/features/challenges/challengesSlice' import { useQuery } from '@tanstack/react-query' +import { router } from 'expo-router' +import { useShareIntentContext } from 'expo-share-intent' import { ReactNode, useEffect } from 'react' export default function GameProvider({ children }: { children: ReactNode }) { @@ -108,6 +110,16 @@ export default function GameProvider({ children }: { children: ReactNode }) { } }, [game.currentRunner, challengeActions]) + const { hasShareIntent, shareIntent, resetShareIntent } = useShareIntentContext() + useEffect(() => { + console.log(hasShareIntent, shareIntent) + if (hasShareIntent) { + if (!shareIntent.text || !shareIntent.text.includes("eurailapp.com/share")) + return resetShareIntent() + router.replace('/train') + } + }, [hasShareIntent]) + return <> {children} diff --git a/client/package-lock.json b/client/package-lock.json index fd7ad52..a46031a 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -31,7 +31,7 @@ "expo-notifications": "~0.29.11", "expo-router": "~4.0.9", "expo-secure-store": "~14.0.0", - "expo-share-extension": "^2.0.1", + "expo-share-intent": "^3.1.1", "expo-splash-screen": "~0.29.13", "expo-status-bar": "~2.0.0", "expo-symbols": "~0.2.0", @@ -8121,33 +8121,31 @@ "expo": "*" } }, - "node_modules/expo-share-extension": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/expo-share-extension/-/expo-share-extension-2.0.1.tgz", - "integrity": "sha512-MFjzN8xapBAz3f8lZCTmgVYjVeVa8gUC0NLuTTMgeIwNlua3HW/ySbkYxyRsSX48q7MtVV/OwxqkOfnw6Bpj/w==", + "node_modules/expo-share-intent": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/expo-share-intent/-/expo-share-intent-3.1.1.tgz", + "integrity": "sha512-YoO7arEP8oRRevQ3digP7Fu1r1Nk6Zo2LfPndWfLXota1O8BLlAkl15W5dOLqRrYIAuRfQpcmmtXu/pUA0pvQg==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/achorein" + }, + "https://www.buymeacoffee.com/achorein" + ], "license": "MIT", "dependencies": { - "semver": "^7.6.3", - "zod": "^3.23.8" + "@expo/config-plugins": "^9.0.9", + "expo-constants": "^17.0.2", + "expo-linking": "~7.0.2" }, "peerDependencies": { - "expo": "*", + "expo": "^52", + "expo-constants": ">=17.0.2", + "expo-linking": ">=7.0.2", "react": "*", "react-native": "*" } }, - "node_modules/expo-share-extension/node_modules/semver": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", - "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", - "license": "ISC", - "bin": { - "semver": "bin/semver.js" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/expo-splash-screen": { "version": "0.29.18", "resolved": "https://registry.npmjs.org/expo-splash-screen/-/expo-splash-screen-0.29.18.tgz", @@ -16801,15 +16799,6 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } - }, - "node_modules/zod": { - "version": "3.24.1", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.24.1.tgz", - "integrity": "sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A==", - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } } } } diff --git a/client/package.json b/client/package.json index 1c5f5c0..280ef37 100644 --- a/client/package.json +++ b/client/package.json @@ -37,7 +37,7 @@ "expo-notifications": "~0.29.11", "expo-router": "~4.0.9", "expo-secure-store": "~14.0.0", - "expo-share-extension": "^2.0.1", + "expo-share-intent": "^3.1.1", "expo-splash-screen": "~0.29.13", "expo-status-bar": "~2.0.0", "expo-symbols": "~0.2.0",