From 07cd0d04c57a05f962053e500da6350c8beaa521 Mon Sep 17 00:00:00 2001 From: Eric van der Vlist Date: Sun, 5 Mar 2023 09:53:26 +0100 Subject: [PATCH] Supporting invitations through geo URLs --- AndroidManifest.xml | 2 ++ src/components/invitation/Invitation.tsx | 36 ++++++++++++++++++++++++ src/components/invitation/index.ts | 2 +- src/components/map/Map.tsx | 9 +++++- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e911bcc..b203e8b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -34,7 +34,9 @@ + + diff --git a/src/components/invitation/Invitation.tsx b/src/components/invitation/Invitation.tsx index 955c669..cf0e40f 100644 --- a/src/components/invitation/Invitation.tsx +++ b/src/components/invitation/Invitation.tsx @@ -36,6 +36,42 @@ const fetchInvitation = () => { }); }; +export const searchInvitationInIntent = (intent: any) => { + if (intent.action !== 'android.intent.action.VIEW') { + return false; + } + let url; + try { + url = new URL(intent.data); + } catch (error) { + console.error({ caller: 'searchInvitationInIntent', intent, error }); + return false; + } + if (url.protocol !== 'geo:') { + return false; + } + const q = url.search; + const matches = q.match(/invitation=(.+)/); + if (!matches) { + return false; + } + try { + const payload = matches[1]; + const decoded = atob(payload); + const url = new URL(decoded); + setInvitation({ url }); + fetchInvitation(); + } catch { + console.warn({ + caller: 'Invitation / searchInvitationInIntent', + intent, + matches, + message: 'Query is not a base64 encoded URL', + }); + } + return true; +}; + export const searchInvitation = () => { const hash = location.hash; console.log({ caller: 'Invitation / searchInvitation', hash }); diff --git a/src/components/invitation/index.ts b/src/components/invitation/index.ts index b7dbbb2..2be74d0 100644 --- a/src/components/invitation/index.ts +++ b/src/components/invitation/index.ts @@ -1 +1 @@ -export { default, searchInvitation } from './Invitation'; +export { default, searchInvitation, searchInvitationInIntent } from './Invitation'; diff --git a/src/components/map/Map.tsx b/src/components/map/Map.tsx index feae88d..693bdda 100644 --- a/src/components/map/Map.tsx +++ b/src/components/map/Map.tsx @@ -46,7 +46,11 @@ import { Overlays } from '../overlays/Overlays'; import Finder, { findLocation } from '../finder'; import { ZoomIn } from '@suid/icons-material'; import Note from '../note'; -import Invitation, { searchInvitation } from '../invitation'; +import Invitation, { + searchInvitation, + searchInvitationInIntent, +} from '../invitation'; +import { returnOrUpdate } from 'ol/extent'; const [getState, setState] = createSignal({ lon: 0, @@ -92,6 +96,9 @@ const Map: Component = () => { ); window.plugins.intentShim.onIntent(function (intent: any) { console.log({ caller: 'Intent receiver', intent }); + if (searchInvitationInIntent(intent)) { + return; + } if (intent.action === 'android.intent.action.VIEW') { const url = new URL(intent.data); console.log({ caller: 'Intent receiver', intent, url });