Refactoring intent receivers for invitations

This commit is contained in:
Eric van der Vlist 2023-04-17 14:45:04 +02:00
parent f82181347c
commit d5a8118c2a
5 changed files with 36 additions and 36 deletions

View File

@ -36,27 +36,8 @@ const fetchInvitation = () => {
});
};
export const searchInvitationInIntent = (intent: any) => {
if (intent.action !== 'android.intent.action.VIEW') {
return false;
}
let url;
export const openInvitation = (payload: string) => {
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 });
@ -64,8 +45,7 @@ export const searchInvitationInIntent = (intent: any) => {
} catch {
console.warn({
caller: 'Invitation / searchInvitationInIntent',
intent,
matches,
payload,
message: 'Query is not a base64 encoded URL',
});
}

View File

@ -1 +1 @@
export { default, searchInvitation, searchInvitationInIntent } from './Invitation';
export { default, searchInvitation } from './Invitation';

View File

@ -1,5 +1,5 @@
import { Component, createEffect, createSignal, onMount } from 'solid-js';
import { useParams, useNavigate, useLocation } from '@solidjs/router';
import { useParams, useNavigate } from '@solidjs/router';
import OlMap from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
@ -16,7 +16,7 @@ import DragRotate from 'ol/interaction/DragRotate';
import 'ol/ol.css';
import './Map.css';
import { Collection, Overlay } from 'ol';
import { Collection } from 'ol';
import { Point } from 'ol/geom';
import { Style, Icon } from 'ol/style';
import GetLocation, { getCurrentLocation } from '../get-location';
@ -26,7 +26,6 @@ import GpxImport, { importUrls } from '../import';
import AllGpxes from '../all-gpxes';
import MapTileProvider, { mapTileProviders } from '../map-tile-provider';
import Interaction from 'ol/interaction/Interaction';
import DoubleClickZoom from 'ol/interaction/DoubleClickZoom';
import DragPan from 'ol/interaction/DragPan';
import PinchRotate from 'ol/interaction/PinchRotate';
import PinchZoom from 'ol/interaction/PinchZoom';
@ -41,16 +40,10 @@ import dispatch from '../../workers/dispatcher-main';
import { debounce } from 'lodash';
import { AndroidFullScreen } from '@awesome-cordova-plugins/android-full-screen';
import Account from '../account';
import { Overlays } from '../overlays/Overlays';
import Finder, { findLocation } from '../finder';
import { ZoomIn } from '@suid/icons-material';
import Note from '../note';
import Invitation, {
searchInvitation,
searchInvitationInIntent,
} from '../invitation';
import { returnOrUpdate } from 'ol/extent';
import Invitation, { searchInvitation } from '../invitation';
import Updater from '../updater';
import SystemDrawer from '../system-drawer';
import { receiveIntent } from '../../lib/intent-receivers';
@ -99,9 +92,6 @@ const Map: Component = () => {
);
window.plugins.intentShim.onIntent(function (intent: any) {
console.log({ caller: 'Intent receiver', intent });
if (searchInvitationInIntent(intent)) {
return;
}
if (receiveIntent(intent)) {
return;
}

View File

@ -1,6 +1,8 @@
import { gpxReceiver } from './gpx-receiver';
import { invitationReceiver } from './invitation-receiver';
const receivers = {
invitationReceiver,
gpxReceiver,
};

View File

@ -0,0 +1,28 @@
import { openInvitation } from '../../components/invitation/Invitation';
const ACCEPTED_ACTIONS = ['android.intent.action.VIEW'];
const ACCEPTED_PROTOCOLS = ['geo:'];
export const invitationReceiver = (intent: any, url: URL | undefined) => {
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 matches = q.match(/invitation=(.+)/);
if (!matches) {
return false;
}
const payload = matches[1];
openInvitation(payload);
return true;
};