summaryrefslogtreecommitdiff
path: root/services/Weather.qml
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2026-01-04 00:15:40 +1100
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2026-01-04 00:15:40 +1100
commit229934a986db799cebc2a39e0840c629b6d16d84 (patch)
tree5200180f8397bd4fc95b04daa5e47824296ae444 /services/Weather.qml
parentweather: better padding for forecast label (diff)
downloadcaelestia-shell-229934a986db799cebc2a39e0840c629b6d16d84.tar.gz
caelestia-shell-229934a986db799cebc2a39e0840c629b6d16d84.tar.bz2
caelestia-shell-229934a986db799cebc2a39e0840c629b6d16d84.zip
lock: fix weather
Diffstat (limited to 'services/Weather.qml')
-rw-r--r--services/Weather.qml68
1 files changed, 45 insertions, 23 deletions
diff --git a/services/Weather.qml b/services/Weather.qml
index 942695e..a309542 100644
--- a/services/Weather.qml
+++ b/services/Weather.qml
@@ -12,12 +12,13 @@ Singleton {
property string city
property string loc
property var cc
- property var forecast
+ property list<var> forecast
+ property list<var> hourlyForecast
readonly property string icon: cc ? Icons.getWeatherIcon(cc.weatherCode) : "cloud_alert"
readonly property string description: cc?.weatherDesc ?? qsTr("No weather")
- readonly property string temp: Config.services.useFahrenheit ? `${cc?.temp_F ?? 0}°F` : `${cc?.temp_C ?? 0}°C`
- readonly property string feelsLike: Config.services.useFahrenheit ? `${cc?.FeelsLikeF ?? 0}°F` : `${cc?.FeelsLikeC ?? 0}°C`
+ readonly property string temp: Config.services.useFahrenheit ? `${cc?.tempF ?? 0}°F` : `${cc?.tempC ?? 0}°C`
+ readonly property string feelsLike: Config.services.useFahrenheit ? `${cc?.feelsLikeF ?? 0}°F` : `${cc?.feelsLikeC ?? 0}°C`
readonly property int humidity: cc?.humidity ?? 0
readonly property real windSpeed: cc?.windSpeed ?? 0
readonly property string sunrise: cc ? Qt.formatDateTime(new Date(cc.sunrise), Config.services.useTwelveHourClock ? "h:mm A" : "h:mm") : "--:--"
@@ -94,42 +95,63 @@ Singleton {
return;
cc = {
- "weatherCode": String(json.current.weather_code),
- "weatherDesc": getWeatherCondition(String(json.current.weather_code)),
- "temp_C": Math.round(json.current.temperature_2m),
- "temp_F": Math.round(json.current.temperature_2m * 9 / 5 + 32),
- "FeelsLikeC": Math.round(json.current.apparent_temperature),
- "FeelsLikeF": Math.round(json.current.apparent_temperature * 9 / 5 + 32),
- "humidity": json.current.relative_humidity_2m,
- "windSpeed": json.current.wind_speed_10m,
- "isDay": json.current.is_day,
- "sunrise": json.daily.sunrise[0],
- "sunset": json.daily.sunset[0]
+ weatherCode: json.current.weather_code,
+ weatherDesc: getWeatherCondition(json.current.weather_code),
+ tempC: Math.round(json.current.temperature_2m),
+ tempF: Math.round(toFahrenheit(json.current.temperature_2m)),
+ feelsLikeC: Math.round(json.current.apparent_temperature),
+ feelsLikeF: Math.round(toFahrenheit(json.current.apparent_temperature)),
+ humidity: json.current.relative_humidity_2m,
+ windSpeed: json.current.wind_speed_10m,
+ isDay: json.current.is_day,
+ sunrise: json.daily.sunrise[0],
+ sunset: json.daily.sunset[0]
};
const forecastList = [];
for (let i = 0; i < json.daily.time.length; i++)
forecastList.push({
- "date": json.daily.time[i],
- "maxTempC": Math.round(json.daily.temperature_2m_max[i]),
- "maxTempF": Math.round(json.daily.temperature_2m_max[i] * 9 / 5 + 32),
- "minTempC": Math.round(json.daily.temperature_2m_min[i]),
- "minTempF": Math.round(json.daily.temperature_2m_min[i] * 9 / 5 + 32),
- "weatherCode": String(json.daily.weather_code[i]),
- "icon": Icons.getWeatherIcon(String(json.daily.weather_code[i]))
+ date: json.daily.time[i],
+ maxTempC: Math.round(json.daily.temperature_2m_max[i]),
+ maxTempF: Math.round(toFahrenheit(json.daily.temperature_2m_max[i])),
+ minTempC: Math.round(json.daily.temperature_2m_min[i]),
+ minTempF: Math.round(toFahrenheit(json.daily.temperature_2m_min[i])),
+ weatherCode: json.daily.weather_code[i],
+ icon: Icons.getWeatherIcon(json.daily.weather_code[i])
});
-
forecast = forecastList;
+
+ const hourlyList = [];
+ const now = new Date();
+ for (let i = 0; i < json.hourly.time.length; i++) {
+ const time = new Date(json.hourly.time[i]);
+ if (time < now)
+ continue;
+
+ hourlyList.push({
+ timestamp: json.hourly.time[i],
+ hour: time.getHours(),
+ tempC: Math.round(json.hourly.temperature_2m[i]),
+ tempF: Math.round(toFahrenheit(json.hourly.temperature_2m[i])),
+ weatherCode: json.hourly.weather_code[i],
+ icon: Icons.getWeatherIcon(json.hourly.weather_code[i])
+ });
+ }
+ hourlyForecast = hourlyList;
});
}
+ function toFahrenheit(celcius: real): real {
+ return celcius * 9 / 5 + 32;
+ }
+
function getWeatherUrl(): string {
if (!loc || loc.indexOf(",") === -1)
return "";
const [lat, lon] = loc.split(",");
const baseUrl = "https://api.open-meteo.com/v1/forecast";
- const params = ["latitude=" + lat, "longitude=" + lon, "daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset", "current=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,weather_code,wind_speed_10m", "timezone=auto", "forecast_days=7"];
+ const params = ["latitude=" + lat, "longitude=" + lon, "hourly=weather_code,temperature_2m", "daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset", "current=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,weather_code,wind_speed_10m", "timezone=auto", "forecast_days=7"];
return baseUrl + "?" + params.join("&");
}