diff --git a/src/components/map/Map.tsx b/src/components/map/Map.tsx index fda5c90..e033588 100644 --- a/src/components/map/Map.tsx +++ b/src/components/map/Map.tsx @@ -92,21 +92,13 @@ const Map: Component = () => { ); window.plugins.intentShim.onIntent(function (intent: any) { console.log({ caller: 'Intent receiver', intent }); - if (receiveIntent(intent)) { + if (receiveIntent(intent, navigate)) { return; } if (intent.action === 'android.intent.action.VIEW') { const url = new URL(intent.data); console.log({ caller: 'Intent receiver', intent, url }); - if (url.protocol === 'geo:') { - const q = url.search; - const [, lat, lon] = q.match(/q=([0-9.-]+),([0-9.-]+)/); - console.log({ caller: 'Intent receiver', intent, url, lat, lon }); - findLocation(navigate, [lon, lat]); - } else if ( - url.protocol === 'https:' && - url.hostname === 'web.dyomedea.app' - ) { + if (url.protocol === 'https:' && url.hostname === 'web.dyomedea.app') { location.hash = url.hash; location.pathname = url.pathname; } diff --git a/src/lib/intent-receivers/geo-location.ts b/src/lib/intent-receivers/geo-location.ts new file mode 100644 index 0000000..a95763b --- /dev/null +++ b/src/lib/intent-receivers/geo-location.ts @@ -0,0 +1,30 @@ +import { findLocation } from '../../components/finder'; + +const ACCEPTED_ACTIONS = ['android.intent.action.VIEW']; + +const ACCEPTED_PROTOCOLS = ['geo:']; + +export const geoLocationReceiver = ( + intent: any, + url: URL | undefined, + navigate: any +) => { + if (!ACCEPTED_ACTIONS.includes(intent.action)) { + return false; + } + + if (!url) { + return false; + } + + if (!ACCEPTED_PROTOCOLS.includes(url.protocol)) { + return false; + } + + const q = url.search; + const [, lat, lon] = q.match(/q=([0-9.-]+),([0-9.-]+)/); + console.log({ caller: 'geoLocationReceiver', intent, url, lat, lon }); + findLocation(navigate, [lon, lat]); + + return true; +}; diff --git a/src/lib/intent-receivers/gpx-receiver.ts b/src/lib/intent-receivers/gpx-receiver.ts index 14e4c5d..897ffac 100644 --- a/src/lib/intent-receivers/gpx-receiver.ts +++ b/src/lib/intent-receivers/gpx-receiver.ts @@ -16,7 +16,11 @@ const ACCEPTED_TYPES = [ 'image/jpeg', ]; -export const gpxReceiver = (intent: any, url: URL | undefined) => { +export const gpxReceiver = ( + intent: any, + url: URL | undefined, + navigate: any +) => { if (!ACCEPTED_ACTIONS.includes(intent.action)) { return false; } diff --git a/src/lib/intent-receivers/index.ts b/src/lib/intent-receivers/index.ts index 1b3732e..b1cb2c3 100644 --- a/src/lib/intent-receivers/index.ts +++ b/src/lib/intent-receivers/index.ts @@ -1,12 +1,14 @@ +import { geoLocationReceiver } from './geo-location'; import { gpxReceiver } from './gpx-receiver'; import { invitationReceiver } from './invitation-receiver'; const receivers = { invitationReceiver, gpxReceiver, + geoLocationReceiver, }; -export const receiveIntent = (intent: any) => { +export const receiveIntent = (intent: any, navigate: any) => { let url: URL | undefined; try { url = new URL( @@ -19,7 +21,7 @@ export const receiveIntent = (intent: any) => { } const found = Object.values(receivers).some((receiver) => - receiver(intent, url) + receiver(intent, url, navigate) ); return found; diff --git a/src/lib/intent-receivers/invitation-receiver.ts b/src/lib/intent-receivers/invitation-receiver.ts index c39e19a..52a2aba 100644 --- a/src/lib/intent-receivers/invitation-receiver.ts +++ b/src/lib/intent-receivers/invitation-receiver.ts @@ -4,7 +4,11 @@ const ACCEPTED_ACTIONS = ['android.intent.action.VIEW']; const ACCEPTED_PROTOCOLS = ['geo:']; -export const invitationReceiver = (intent: any, url: URL | undefined) => { +export const invitationReceiver = ( + intent: any, + url: URL | undefined, + navigate: any +) => { if (!ACCEPTED_ACTIONS.includes(intent.action)) { return false; }