Skip to content

Commit 5d0cc89

Browse files
authored
fix location access issue (#49)
* change visibility trackign * rename getConfig -> getConfigStore * remove comment code
1 parent d3ac3e0 commit 5d0cc89

8 files changed

Lines changed: 40 additions & 35 deletions

File tree

src/config/config-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const initConfig = (
5959
});
6060
};
6161

62-
export const getConfig = () => {
62+
export const getConfigStore = () => {
6363
if (!configStore) {
6464
throw Error("missing config store");
6565
}

src/config/default-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export const defaultConfig = {
88
[34.18152307750378, -116.41490672855878], // south-west
99
[34.18253355653219, -116.41373685883292], // north-east
1010
),
11+
hasGrantedLocationAccess: false,
1112
} as const satisfies Config;

src/config/types.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export type FeatureKey = keyof FeaturesConfig;
1313
export type Config = {
1414
features: Record<FeatureKey, FeatureConfig>;
1515
bounds?: L.LatLngBounds;
16-
} & Record<Exclude<string, "features" | "bounds">, unknown>;
16+
hasGrantedLocationAccess: boolean;
17+
};
1718

1819
export type FeatureUpdateEvent = {
1920
key: "features";
@@ -28,4 +29,12 @@ export type BoundsUpdateEvent = {
2829
value: L.LatLngBounds;
2930
};
3031

31-
export type ConfigEvent = FeatureUpdateEvent | BoundsUpdateEvent;
32+
export type LocationPermissionUpdateEvent = {
33+
key: "hasGrantedLocationAccess";
34+
value: boolean;
35+
};
36+
37+
export type ConfigEvent =
38+
| FeatureUpdateEvent
39+
| BoundsUpdateEvent
40+
| LocationPermissionUpdateEvent;

src/console/console-tracker.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { getConfig } from "../config";
1+
import { getConfigStore } from "../config";
22
import { getElementOrThrow, logger, type LogLevel } from "../utils";
33

44
export class ConsoleTracker {
55
constructor() {
6-
const configStore = getConfig();
6+
const configStore = getConfigStore();
77
this.consoleElement = getElementOrThrow({ id: "console" });
88
this.unsubscribeFn = undefined;
99

10-
this.handleVisibilityChange(
11-
configStore.getFeature("console").value,
12-
);
10+
this.handleVisibilityChange(configStore.getFeature("console").value);
1311

1412
configStore.addListener((event) => {
1513
if (event.key !== "features") {

src/location/location-tracker.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { debug, info } from "../utils";
22
import type { LocationPoint } from "./types";
33
import { Observable } from "../observable";
4-
import { getConfig } from "../config";
4+
import { getConfigStore } from "../config";
55

66
export class LocationTracker extends Observable<LocationPoint> {
7-
/**
8-
* Initialize position tracker and EventListener
9-
*/
107
constructor() {
11-
// TODO: move this out
128
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+
});
2117
}
22-
2318
/**
2419
* start tracking via navigator.geolocation
2520
*/
@@ -56,19 +51,20 @@ export class LocationTracker extends Observable<LocationPoint> {
5651
}
5752
};
5853

59-
private handleVisibilityChange = () => {
54+
public handleVisibilityChange = () => {
6055
debug("[LocationTracker] handleVisibilityChange");
6156
if (document.hidden) {
6257
this.stop();
6358
} else {
64-
// TODO: only if the user has hit the locate button
65-
this.start();
59+
if (getConfigStore().config.hasGrantedLocationAccess) {
60+
this.start();
61+
}
6662
}
6763
};
6864

69-
private handlePosition = (position: GeolocationPosition) => {
65+
public handlePosition = (position: GeolocationPosition) => {
7066
const { latitude, longitude, accuracy } = position.coords;
71-
const bounds = getConfig().getBounds();
67+
const bounds = getConfigStore().getBounds();
7268
if (bounds && !bounds.contains([latitude, longitude])) {
7369
info(
7470
"[LocationTracker] location outside bounds, stopping tracking",

src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { SettingsMenu } from "./settings";
1111
import { LocationStore, LocationTracker, OrientationTracker } from "./location";
1212
import { ConsoleTracker } from "./console";
13-
import { getConfig, initConfig } from "./config";
13+
import { getConfigStore, initConfig } from "./config";
1414

1515
import "./styles.css"; // TODO: remove tailwind and import normally
1616

@@ -25,7 +25,7 @@ import { LocationController } from "./location/location-controller";
2525
import { MapMovementController } from "./map/map-movement-controller";
2626

2727
initConfig();
28-
const configStore = getConfig();
28+
const configStore = getConfigStore();
2929

3030
new ConsoleTracker();
3131
new SettingsMenu();
@@ -119,6 +119,8 @@ document.addEventListener("visibilitychange", () => {
119119
debug("[main] handleVisibilityChange");
120120
// save location when the user minimizes
121121
locationStore.saveToStorage();
122+
123+
locationTracker.handleVisibilityChange();
122124
});
123125

124126
window.addEventListener("beforeunload", () => {

src/points/POI-bounds-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getConfig } from "../config";
1+
import { getConfigStore } from "../config";
22
import type { POI } from "../types";
33
import { debug } from "../utils";
44
import L from "leaflet";
@@ -17,7 +17,7 @@ const poiToBoundsLayer = (poi: POI) =>
1717

1818
export class POIBoundsController {
1919
constructor({ POIs }: POIBoundsControllerParams) {
20-
const configStore = getConfig();
20+
const configStore = getConfigStore();
2121

2222
configStore.addListener((event) => {
2323
if (event.key !== "features") {

src/settings/settings.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LocalStorageProvider } from "../storage";
22
import { getElementOrThrow } from "../utils";
3-
import { getConfig, type FeatureKey } from "../config";
3+
import { getConfigStore, type FeatureKey } from "../config";
44

55
export class SettingsMenu {
66
constructor() {
@@ -27,11 +27,10 @@ export class SettingsMenu {
2727
LocalStorageProvider.clear();
2828
});
2929

30-
const configStore = getConfig();
30+
const configStore = getConfigStore();
3131

3232
// TODO: remove partial and use .map
33-
const checkboxByKey: Partial<Record<FeatureKey, HTMLInputElement>> =
34-
{};
33+
const checkboxByKey: Partial<Record<FeatureKey, HTMLInputElement>> = {};
3534

3635
Object.entries(configStore.config.features).forEach(
3736
([key, feature]) => {

0 commit comments

Comments
 (0)