sideright: imperial weather

This commit is contained in:
2 * r + 2 * t 2025-01-27 22:08:44 +11:00
parent df3b51399c
commit e06e29eeab
3 changed files with 55 additions and 43 deletions

View file

@ -82,5 +82,5 @@ export const weather = {
interval: 600000, interval: 600000,
key: "assets/weather-api-key.txt", // Path to file containing api key relative to the base directory. To get a key, visit https://weatherapi.com/ key: "assets/weather-api-key.txt", // Path to file containing api key relative to the base directory. To get a key, visit https://weatherapi.com/
location: "", // Location as a string or empty to autodetect location: "", // Location as a string or empty to autodetect
// TODO: imperial imperial: false,
}; };

View file

@ -75,7 +75,7 @@ const Weather = () => {
/> />
<box vertical halign={Gtk.Align.CENTER} valign={Gtk.Align.CENTER} className="status"> <box vertical halign={Gtk.Align.CENTER} valign={Gtk.Align.CENTER} className="status">
<box halign={Gtk.Align.CENTER} className="temperature"> <box halign={Gtk.Align.CENTER} className="temperature">
<label label={bind(weather, "temperature").as(t => `${Math.round(t)}°C`)} /> <label label={bind(weather, "temperature")} />
<label <label
className={bind(weather, "tempColour").as(c => `temp-icon ${c}`)} className={bind(weather, "tempColour").as(c => `temp-icon ${c}`)}
label={bind(weather, "tempIcon")} label={bind(weather, "tempIcon")}
@ -84,8 +84,8 @@ const Weather = () => {
<label label={bind(weather, "condition").as(c => ellipsize(c, 16))} /> <label label={bind(weather, "condition").as(c => ellipsize(c, 16))} />
</box> </box>
<box vertical halign={Gtk.Align.END} valign={Gtk.Align.CENTER} className="other-data"> <box vertical halign={Gtk.Align.END} valign={Gtk.Align.CENTER} className="other-data">
<label xalign={0} label={bind(weather, "wind").as(w => `${Math.round(w)} kph`)} /> <label xalign={0} label={bind(weather, "wind").as(w => `${w}`)} />
<label xalign={0} label={bind(weather, "rainChance").as(r => `${r}%`)} /> <label xalign={0} label={bind(weather, "rainChance").as(r => `${r}`)} />
</box> </box>
</centerbox> </centerbox>
<box className="separator" /> <box className="separator" />
@ -101,12 +101,12 @@ const Weather = () => {
<label <label
className="icon" className="icon"
label={bind(weather, "raw").as(r => label={bind(weather, "raw").as(r =>
weather.getIcon(weather.forecast[getHoursFromUpdate(r, i + 1)]?.condition.text ?? "") weather.getIcon(weather.forecast[getHoursFromUpdate(r, i + 1)].condition.text)
)} )}
/> />
<label <label
label={bind(weather, "raw").as( label={bind(weather, "raw").as(r =>
r => `${Math.round(weather.forecast[getHoursFromUpdate(r, i + 1)]?.temp_c) ?? "-"}°C` weather.getTemp(weather.forecast[getHoursFromUpdate(r, i + 1)])
)} )}
/> />
</box> </box>

View file

@ -102,10 +102,7 @@ export interface WeatherData {
location: WeatherLocation; location: WeatherLocation;
} }
const DEFAULT: WeatherData = { const DEFAULT_STATE: _WeatherState = {
current: {
last_updated_epoch: 0,
last_updated: "",
temp_c: 0, temp_c: 0,
temp_f: 0, temp_f: 0,
is_day: 0, is_day: 0,
@ -133,6 +130,13 @@ const DEFAULT: WeatherData = {
uv: 0, uv: 0,
gust_mph: 0, gust_mph: 0,
gust_kph: 0, gust_kph: 0,
};
const DEFAULT: WeatherData = {
current: {
last_updated_epoch: 0,
last_updated: "",
...DEFAULT_STATE,
}, },
forecast: { forecast: {
forecastday: [ forecastday: [
@ -171,7 +175,11 @@ const DEFAULT: WeatherData = {
is_moon_up: 0, is_moon_up: 0,
is_sun_up: 0, is_sun_up: 0,
}, },
hour: [], hour: Array.from({ length: 24 }, () => ({
time_epoch: 0,
time: "",
...DEFAULT_STATE,
})),
}, },
], ],
}, },
@ -243,8 +251,6 @@ const STATUS_ICONS: Record<string, string> = {
moderate_or_heavy_snow_with_thunder: "󰼶", moderate_or_heavy_snow_with_thunder: "󰼶",
}; };
const TEMP_ICONS: Record<string, string> = {};
@register({ GTypeName: "Weather" }) @register({ GTypeName: "Weather" })
export default class Weather extends GObject.Object { export default class Weather extends GObject.Object {
static instance: Weather; static instance: Weather;
@ -284,19 +290,21 @@ export default class Weather extends GObject.Object {
return this.#data.current.condition.text; return this.#data.current.condition.text;
} }
@property(Number) @property(String)
get temperature() { get temperature() {
return this.#data.current.temp_c; return this.getTemp(this.#data.current);
} }
@property(Number) @property(String)
get wind() { get wind() {
return this.#data.current.wind_kph; return `${Math.round(this.#data.current[`wind_${config.imperial ? "m" : "k"}ph`])} ${
config.imperial ? "m" : "k"
}ph`;
} }
@property(Number) @property(String)
get rainChance() { get rainChance() {
return this.#data.forecast.forecastday[0].day.daily_chance_of_rain; return this.#data.forecast.forecastday[0].day.daily_chance_of_rain + "%";
} }
@property(String) @property(String)
@ -320,6 +328,10 @@ export default class Weather extends GObject.Object {
return STATUS_ICONS[query] ?? STATUS_ICONS.warning; return STATUS_ICONS[query] ?? STATUS_ICONS.warning;
} }
getTemp(data: _WeatherState) {
return `${Math.round(data[`temp_${config.imperial ? "f" : "c"}`])}°${config.imperial ? "F" : "C"}`;
}
getTempIcon(temp: number) { getTempIcon(temp: number) {
if (temp >= 40) return ""; if (temp >= 40) return "";
if (temp >= 30) return ""; if (temp >= 30) return "";