Repository navigation
Expand file tree
/
Copy pathweather.ts
More file actions
191 lines (175 loc) · 7.46 KB
/
Copy pathweather.ts
File metadata and controls
191 lines (175 loc) · 7.46 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../layers";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../menu";
const PAGE_X = 18;
const HEADER_Y = 8;
const CURRENT_TOP = 34;
const CURRENT_TEXT_X = 112;
const FORECAST_HEADER_Y = 105;
const FORECAST_TOP = 124;
const FORECAST_ROW_HEIGHT = 23;
const FOOTER_HEIGHT = 18;
const FORECAST_NAME_WIDTH = 92;
const FORECAST_TEMP_X = 126;
const FORECAST_PRECIP_X = 184;
const FORECAST_SUMMARY_X = 244;
/** Weather's current-conditions summary and scrollable 12-hour forecast. */
export class WeatherLayer implements Layer {
private selectedIndex = 0;
private scrollRow = 0;
constructor(
private readonly state: () => WeatherState,
private readonly requestUpdate: () => void,
) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const weather = this.state();
image.drawText(font, PAGE_X, HEADER_Y, "Weather", 225);
if (weather.locationName) {
image.drawText(
font,
width - PAGE_X - font.measureText(truncateText(font, weather.locationName, 220)),
HEADER_Y,
truncateText(font, weather.locationName, 220),
150,
);
}
if (weather.phase === "permission-required") {
this.drawMessage(
image,
font,
width,
height,
"Allow approximate location on your phone to get local weather.",
`${GESTURE_CLICK} request ${GESTURE_DOUBLE_CLICK} back`,
);
return image;
}
if (weather.phase === "locating" || weather.phase === "loading") {
this.drawMessage(image, font, width, height, weather.status, `${GESTURE_DOUBLE_CLICK} back`);
return image;
}
if (weather.phase === "error") {
this.drawMessage(
image,
font,
width,
height,
weather.status,
`${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`,
);
return image;
}
this.drawCurrent(image, weather, width);
this.drawForecast(image, weather.forecast, ctx, width, height);
return image;
}
handleInput(event: DashboardInputEvent): void {
const weather = this.state();
if (event.type === "click") {
this.requestUpdate();
return;
}
if (!weather.forecast.length || weather.phase !== "ready") return;
if (event.type === "scroll-up") {
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
} else if (event.type === "scroll-down") {
this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1);
}
}
private drawCurrent(image: GrayImage, weather: WeatherState, width: number): void {
const current = weather.current;
if (!current) return;
const large = getDefaultLargeFont();
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`;
image.drawText(large, PAGE_X, CURRENT_TOP + 8, temperature, 245);
image.drawText(
medium,
CURRENT_TEXT_X,
CURRENT_TOP + 5,
truncateText(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
220,
);
const details: string[] = [];
if (current.humidityPercent !== null) details.push(`Humidity ${Math.round(current.humidityPercent)}%`);
if (current.windSpeedMph !== null) {
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${Math.round(current.windSpeedMph)} mph`);
}
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 35, details.join(" ") || "Current forecast", 170);
const source = current.observed ? "Observed" : "Forecast";
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 54, `${source}${age ? ` ${age}` : ""}`, 105);
image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40);
}
private drawForecast(
image: GrayImage,
forecast: ForecastPeriod[],
ctx: LayerContext,
width: number,
height: number,
): void {
const font = getDefaultSmallFont();
if (!forecast.length) {
image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160);
return;
}
this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1);
const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / FORECAST_ROW_HEIGHT));
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length);
image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150);
image.drawText(font, FORECAST_TEMP_X, FORECAST_HEADER_Y, "Temp", 105);
image.drawText(font, FORECAST_PRECIP_X, FORECAST_HEADER_Y, "Rain", 105);
image.drawText(font, width - PAGE_X - font.measureText(`${this.selectedIndex + 1}/${forecast.length}`), FORECAST_HEADER_Y, `${this.selectedIndex + 1}/${forecast.length}`, 105);
const last = Math.min(forecast.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < last; index++) {
const period = forecast[index]!;
const y = FORECAST_TOP + (index - this.scrollRow) * FORECAST_ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), FORECAST_ROW_HEIGHT - 2, ctx.stack.isFocused(), 5);
}
const shade = selected ? 245 : 190;
image.drawText(font, PAGE_X, y + 2, truncateText(font, period.name, FORECAST_NAME_WIDTH), shade);
image.drawText(font, FORECAST_TEMP_X, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
image.drawText(font, FORECAST_PRECIP_X, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
image.drawText(font, FORECAST_SUMMARY_X, y + 2, truncateText(font, period.shortForecast, width - FORECAST_SUMMARY_X - PAGE_X), selected ? 220 : 165);
}
image.drawText(
font,
PAGE_X,
height - 15,
`${GESTURE_SCROLL} forecast ${GESTURE_CLICK} refresh ${GESTURE_DOUBLE_CLICK} back`,
105,
);
}
private drawMessage(
image: GrayImage,
font: BdfFont,
width: number,
height: number,
message: string,
footer: string,
): void {
const lines = wrapText(font, message, width - 2 * (PAGE_X + 6));
const top = Math.max(52, Math.round((height - lines.length * 15) / 2) - 8);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, PAGE_X + 6, top + index * 15, lines[index]!, 185);
}
image.drawText(font, PAGE_X, height - 20, footer, 110);
}
}
function formatAge(ageMs: number): string {
const minutes = Math.max(0, Math.round(ageMs / 60_000));
if (minutes < 1) return "just now";
if (minutes < 60) return `${minutes}m ago`;
return `${Math.round(minutes / 60)}h ago`;
}