-
Notifications
You must be signed in to change notification settings - Fork 33
/
open-with.service.ts
136 lines (128 loc) · 5.4 KB
/
open-with.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { inject, Injectable, NgZone } from "@angular/core";
import { Router } from "@angular/router";
import { MatDialog } from "@angular/material/dialog";
import { App } from "@capacitor/app";
import { RunningContextService } from "./running-context.service";
import { FileService } from "./file.service";
import { ToastService } from "./toast.service";
import { ResourcesService } from "./resources.service";
import { LoggingService } from "./logging.service";
import { getIdFromLatLng, RouteStrings } from "./hash.service";
import { SpatialService } from "./spatial.service";
@Injectable()
export class OpenWithService {
private readonly resources = inject(ResourcesService);
private readonly runningContextService = inject(RunningContextService);
private readonly fileService = inject(FileService);
private readonly toastService = inject(ToastService);
private readonly matDialog = inject(MatDialog);
private readonly router = inject(Router);
private readonly loggingService = inject(LoggingService);
private readonly ngZone = inject(NgZone);
public initialize() {
if (!this.runningContextService.isCapacitor) {
return;
}
App.addListener("appUrlOpen", (data) => {
if (!data || !data.url) {
return;
}
if (data.url.startsWith("ihm://")) {
// no need to do anything as this is part of the login flow
return;
}
if (data.url.startsWith("geo")) {
const coordsRegExp = /:(-?\d+\.\d+),(-?\d+\.\d+)/;
const coords = coordsRegExp.exec(data.url);
this.moveToCoordinates(coords);
return;
}
if (data.url.startsWith("http")) {
this.handleHttpUrl(data.url);
} else {
this.handleFileUrl(data.url);
}
});
}
private handleIHMUrl(url: URL) {
this.logAndCloseDialogs(url);
const pathname = url.pathname;
if (pathname.startsWith("/share")) {
const shareId = pathname.replace("/share/", "");
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_SHARE, shareId]);
});
} else if (pathname.startsWith("/poi")) {
const sourceAndId = pathname.replace("/poi/", "");
const source = sourceAndId.split("/")[0];
const id = sourceAndId.split("/")[1];
const language = new URLSearchParams(url.search).get("language");
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_POI, source, id],
{ queryParams: { language } });
});
} else if (pathname.startsWith("/url")) {
const urlData = pathname.replace("/url/", "");
const baseLayer = new URLSearchParams(url.search).get("baselayer");
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_URL, urlData],
{ queryParams: { baseLayer } });
});
} else if (pathname.startsWith("/map")) {
const mapLocation = pathname.replace("/map/", "");
const zoom = mapLocation.split("/")[0];
const lat = mapLocation.split("/")[1];
const lng = mapLocation.split("/")[2];
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_MAP, zoom, lat, lng]);
});
} else {
this.ngZone.run(() => {
this.router.navigate(["/"]);
});
}
}
private handleFileUrl(url: string) {
this.loggingService.info("[OpenWith] Opening a file shared with the app " + url);
setTimeout(async () => {
try {
const file = await this.fileService.getFileFromUrl(url);
this.fileService.addRoutesFromFile(file);
} catch (ex) {
this.toastService.error(ex, this.resources.unableToLoadFromFile);
}
}, 0);
}
private handleHttpUrl(href: string) {
const url = new URL(href);
if (url.host.toLocaleLowerCase() === "israelhiking.osm.org.il") {
this.handleIHMUrl(url);
return;
}
this.loggingService.info("[OpenWith] Opening an external url: " + href);
if (url.href.indexOf("maps?q=") !== -1) {
const coordsRegExp = /q=(\d+\.\d+),(\d+\.\d+)&z=/;
const coords = coordsRegExp.exec(decodeURIComponent(href));
this.moveToCoordinates(coords);
return;
}
if (url.href.indexOf("maps/place") !== -1) {
const coordsRegExp = /@(\d+\.\d+),(\d+\.\d+),/;
const coords = coordsRegExp.exec(href);
this.moveToCoordinates(coords);
return;
}
this.router.navigate([RouteStrings.ROUTE_URL, href]);
}
private logAndCloseDialogs(url: URL) {
this.loggingService.info("[OpenWith] Opening: " + url.href);
if (this.matDialog.openDialogs.length > 0) {
this.matDialog.closeAll();
}
}
private moveToCoordinates(coords: string[]) {
const latLng = SpatialService.toLatLng([+coords[2], +coords[1]]);
this.router.navigate([RouteStrings.ROUTE_POI, RouteStrings.COORDINATES, getIdFromLatLng(latLng)],
{ queryParams: { language: this.resources.getCurrentLanguageCodeSimplified() } });
}
}