From e06e29eeab642908c2c976637e46f582602a8e1b Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Mon, 27 Jan 2025 22:08:44 +1100 Subject: sideright: imperial weather --- config.ts | 2 +- src/modules/popdowns/sideright.tsx | 12 +++--- src/services/weather.ts | 84 ++++++++++++++++++++++---------------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/config.ts b/config.ts index f74c024..4bae082 100644 --- a/config.ts +++ b/config.ts @@ -82,5 +82,5 @@ export const weather = { 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/ location: "", // Location as a string or empty to autodetect - // TODO: imperial + imperial: false, }; diff --git a/src/modules/popdowns/sideright.tsx b/src/modules/popdowns/sideright.tsx index 86c0547..7dc1688 100644 --- a/src/modules/popdowns/sideright.tsx +++ b/src/modules/popdowns/sideright.tsx @@ -75,7 +75,7 @@ const Weather = () => { /> - - @@ -101,12 +101,12 @@ const Weather = () => { diff --git a/src/services/weather.ts b/src/services/weather.ts index bf595fa..0de8ca5 100644 --- a/src/services/weather.ts +++ b/src/services/weather.ts @@ -102,37 +102,41 @@ export interface WeatherData { location: WeatherLocation; } +const DEFAULT_STATE: _WeatherState = { + temp_c: 0, + temp_f: 0, + is_day: 0, + condition: { text: "", icon: "", code: 0 }, + wind_mph: 0, + wind_kph: 0, + wind_degree: 0, + wind_dir: "N", + pressure_mb: 0, + pressure_in: 0, + precip_mm: 0, + precip_in: 0, + humidity: 0, + cloud: 0, + feelslike_c: 0, + feelslike_f: 0, + windchill_c: 0, + windchill_f: 0, + heatindex_c: 0, + heatindex_f: 0, + dewpoint_c: 0, + dewpoint_f: 0, + vis_km: 0, + vis_miles: 0, + uv: 0, + gust_mph: 0, + gust_kph: 0, +}; + const DEFAULT: WeatherData = { current: { last_updated_epoch: 0, last_updated: "", - temp_c: 0, - temp_f: 0, - is_day: 0, - condition: { text: "", icon: "", code: 0 }, - wind_mph: 0, - wind_kph: 0, - wind_degree: 0, - wind_dir: "N", - pressure_mb: 0, - pressure_in: 0, - precip_mm: 0, - precip_in: 0, - humidity: 0, - cloud: 0, - feelslike_c: 0, - feelslike_f: 0, - windchill_c: 0, - windchill_f: 0, - heatindex_c: 0, - heatindex_f: 0, - dewpoint_c: 0, - dewpoint_f: 0, - vis_km: 0, - vis_miles: 0, - uv: 0, - gust_mph: 0, - gust_kph: 0, + ...DEFAULT_STATE, }, forecast: { forecastday: [ @@ -171,7 +175,11 @@ const DEFAULT: WeatherData = { is_moon_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 = { moderate_or_heavy_snow_with_thunder: "󰼶", }; -const TEMP_ICONS: Record = {}; - @register({ GTypeName: "Weather" }) export default class Weather extends GObject.Object { static instance: Weather; @@ -284,19 +290,21 @@ export default class Weather extends GObject.Object { return this.#data.current.condition.text; } - @property(Number) + @property(String) get temperature() { - return this.#data.current.temp_c; + return this.getTemp(this.#data.current); } - @property(Number) + @property(String) 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() { - return this.#data.forecast.forecastday[0].day.daily_chance_of_rain; + return this.#data.forecast.forecastday[0].day.daily_chance_of_rain + "%"; } @property(String) @@ -320,6 +328,10 @@ export default class Weather extends GObject.Object { 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) { if (temp >= 40) return ""; if (temp >= 30) return ""; -- cgit v1.2.3-freya