Refactoring intent receivers for geo: locations

This commit is contained in:
Eric van der Vlist 2023-04-17 14:57:08 +02:00
parent d5a8118c2a
commit c3c93139ed
5 changed files with 46 additions and 14 deletions

View File

@ -92,21 +92,13 @@ const Map: Component = () => {
); );
window.plugins.intentShim.onIntent(function (intent: any) { window.plugins.intentShim.onIntent(function (intent: any) {
console.log({ caller: 'Intent receiver', intent }); console.log({ caller: 'Intent receiver', intent });
if (receiveIntent(intent)) { if (receiveIntent(intent, navigate)) {
return; return;
} }
if (intent.action === 'android.intent.action.VIEW') { if (intent.action === 'android.intent.action.VIEW') {
const url = new URL(intent.data); const url = new URL(intent.data);
console.log({ caller: 'Intent receiver', intent, url }); console.log({ caller: 'Intent receiver', intent, url });
if (url.protocol === 'geo:') { if (url.protocol === 'https:' && url.hostname === 'web.dyomedea.app') {
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'
) {
location.hash = url.hash; location.hash = url.hash;
location.pathname = url.pathname; location.pathname = url.pathname;
} }

View File

@ -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;
};

View File

@ -16,7 +16,11 @@ const ACCEPTED_TYPES = [
'image/jpeg', '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)) { if (!ACCEPTED_ACTIONS.includes(intent.action)) {
return false; return false;
} }

View File

@ -1,12 +1,14 @@
import { geoLocationReceiver } from './geo-location';
import { gpxReceiver } from './gpx-receiver'; import { gpxReceiver } from './gpx-receiver';
import { invitationReceiver } from './invitation-receiver'; import { invitationReceiver } from './invitation-receiver';
const receivers = { const receivers = {
invitationReceiver, invitationReceiver,
gpxReceiver, gpxReceiver,
geoLocationReceiver,
}; };
export const receiveIntent = (intent: any) => { export const receiveIntent = (intent: any, navigate: any) => {
let url: URL | undefined; let url: URL | undefined;
try { try {
url = new URL( url = new URL(
@ -19,7 +21,7 @@ export const receiveIntent = (intent: any) => {
} }
const found = Object.values(receivers).some((receiver) => const found = Object.values(receivers).some((receiver) =>
receiver(intent, url) receiver(intent, url, navigate)
); );
return found; return found;

View File

@ -4,7 +4,11 @@ const ACCEPTED_ACTIONS = ['android.intent.action.VIEW'];
const ACCEPTED_PROTOCOLS = ['geo:']; 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)) { if (!ACCEPTED_ACTIONS.includes(intent.action)) {
return false; return false;
} }