-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
94 lines (82 loc) · 2.89 KB
/
index.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
import { Accuracy, GeolocationAdapter, Options } from "./adapter";
import { Geolocation, GeolocationLike as GL } from "./geolocation";
import { Observable } from "rxjs";
export { Geolocation };
export type GeolocationLike = GL;
const SECONDS = 1e3;
const MINUTES = 60 * SECONDS;
const defaultAccuracy = Accuracy.high;
const defaultInterval = MINUTES;
const defaultMinInterval = 5 * SECONDS;
const defaultTimeout = 5 * MINUTES;
const allowBackgroundByDefault = false;
const saveBatteryByDefault = true;
export class GeolocationProvider {
constructor(private adapter: GeolocationAdapter) {}
isReady(): Promise<boolean> {
return this.adapter.isReady();
}
prepare(watchAlways = false, openSettingsIfDenied = false): Promise<void> {
return this.adapter.prepare(watchAlways, openSettingsIfDenied);
}
async acquireLocation(options: AcquireOptions = {}): Promise<Geolocation> {
const location = await this.adapter.acquireLocation(
geolocationOptionsToPluginOptions(options)
);
return new Geolocation(location);
}
locationStream(options: StreamOptions = {}): Observable<Geolocation> {
return new Observable((subscriber) => {
const listenerId = this.adapter.listenChanges(
(location) => subscriber.next(new Geolocation(location)),
(err) => subscriber.error(err),
geolocationOptionsToPluginOptions(options)
);
return () => this.adapter.stopListening(listenerId);
});
}
}
export interface AcquireOptions {
highAccuracy?: boolean;
timeout?: number;
allowBackground?: boolean;
}
export interface StreamOptions extends AcquireOptions {
distanceFilter?: number;
stdInterval?: number;
minInterval?: number;
maxAge?: number;
saveBattery?: boolean;
}
function geolocationOptionsToPluginOptions(
options: AcquireOptions | StreamOptions
): Options {
const pluginOptions: Options = {
desiredAccuracy: "highAccuracy" in options ? Accuracy.any : defaultAccuracy,
updateTime:
"stdInterval" in options ? options.stdInterval : defaultInterval,
minimumUpdateTime:
"minInterval" in options ? options.minInterval : defaultMinInterval,
timeout: "timeout" in options ? options.timeout : defaultTimeout,
iosAllowsBackgroundLocationUpdates:
"allowBackground" in options
? options.allowBackground
: allowBackgroundByDefault,
iosPausesLocationUpdatesAutomatically:
"saveBattery" in options ? options.saveBattery : saveBatteryByDefault,
};
if ("distanceFilter" in options) {
pluginOptions.updateDistance = options.distanceFilter;
}
if ("maxAge" in options) {
pluginOptions.maximumAge = options.maxAge;
}
return pluginOptions;
}
let _geolocationProvider: GeolocationProvider;
export function getGeolocationProvider(): GeolocationProvider {
if (!_geolocationProvider) {
_geolocationProvider = new GeolocationProvider(new GeolocationAdapter());
}
return _geolocationProvider;
}