|
1 | 1 | import { debug, info } from "../utils"; |
2 | 2 | import type { LocationPoint } from "./types"; |
3 | 3 | import { Observable } from "../observable"; |
4 | | -import { getConfig } from "../config"; |
| 4 | +import { getConfigStore } from "../config"; |
5 | 5 |
|
6 | 6 | export class LocationTracker extends Observable<LocationPoint> { |
7 | | - /** |
8 | | - * Initialize position tracker and EventListener |
9 | | - */ |
10 | 7 | constructor() { |
11 | | - // TODO: move this out |
12 | 8 | super(); |
13 | | - |
14 | | - document.addEventListener( |
15 | | - "visibilitychange", |
16 | | - this.handleVisibilityChange, |
17 | | - ); |
18 | | - |
19 | | - // Initialize map elements |
20 | | - // TODO: map elements in this class feels like tight coupling |
| 9 | + getConfigStore().addListener(({ key, value }) => { |
| 10 | + if (key === "hasGrantedLocationAccess") { |
| 11 | + if (value === true) { |
| 12 | + debug("[LocationTracker] hasGrantedLocationAccess changed"); |
| 13 | + this.start(); |
| 14 | + } |
| 15 | + } |
| 16 | + }); |
21 | 17 | } |
22 | | - |
23 | 18 | /** |
24 | 19 | * start tracking via navigator.geolocation |
25 | 20 | */ |
@@ -56,19 +51,20 @@ export class LocationTracker extends Observable<LocationPoint> { |
56 | 51 | } |
57 | 52 | }; |
58 | 53 |
|
59 | | - private handleVisibilityChange = () => { |
| 54 | + public handleVisibilityChange = () => { |
60 | 55 | debug("[LocationTracker] handleVisibilityChange"); |
61 | 56 | if (document.hidden) { |
62 | 57 | this.stop(); |
63 | 58 | } else { |
64 | | - // TODO: only if the user has hit the locate button |
65 | | - this.start(); |
| 59 | + if (getConfigStore().config.hasGrantedLocationAccess) { |
| 60 | + this.start(); |
| 61 | + } |
66 | 62 | } |
67 | 63 | }; |
68 | 64 |
|
69 | | - private handlePosition = (position: GeolocationPosition) => { |
| 65 | + public handlePosition = (position: GeolocationPosition) => { |
70 | 66 | const { latitude, longitude, accuracy } = position.coords; |
71 | | - const bounds = getConfig().getBounds(); |
| 67 | + const bounds = getConfigStore().getBounds(); |
72 | 68 | if (bounds && !bounds.contains([latitude, longitude])) { |
73 | 69 | info( |
74 | 70 | "[LocationTracker] location outside bounds, stopping tracking", |
|
0 commit comments