Repository navigation
Expand file tree
/
Copy pathweather-app.ts
More file actions
56 lines (52 loc) · 1.71 KB
/
Copy pathweather-app.ts
File metadata and controls
56 lines (52 loc) · 1.71 KB
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
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
import { weatherBridge } from "../../native/weather";
import { WeatherLayer } from "../apps/weather";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const WEATHER_WINDOW_ID = "weather";
export const WEATHER_SURFACE_ID = "window:weather";
/** Local current conditions and forecast from the National Weather Service. */
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
let requestingPermission = false;
let unsubscribe: (() => void) | null = null;
let app: InProcessWindow;
const requestUpdate = () => {
if (requestingPermission) return;
if (hasLocationPermission()) {
weatherBridge.start();
return;
}
requestingPermission = true;
void ensureLocationPermission().then((granted) => {
requestingPermission = false;
if (granted) weatherBridge.start();
app.requestRender();
});
};
app = createInProcessWindow({
appId: "weather",
windowId: WEATHER_WINDOW_ID,
title: "Weather",
iconLetter: "W",
icon: "cloud-sun",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
unsubscribe?.();
unsubscribe = null;
weatherBridge.stop();
options.onClosed();
},
});
unsubscribe = weatherBridge.onStateChange(() => app.requestRender());
requestUpdate();
return app;
}