lock: fix weather
This commit is contained in:
parent
ff087e23b3
commit
229934a986
2 changed files with 50 additions and 47 deletions
|
|
@ -122,31 +122,14 @@ ColumnLayout {
|
|||
|
||||
Repeater {
|
||||
model: {
|
||||
const forecast = Weather.forecast;
|
||||
let count = root.width < 320 ? 3 : root.width < 400 ? 4 : 5;
|
||||
const forecast = Weather.hourlyForecast;
|
||||
const count = root.width < 320 ? 3 : root.width < 400 ? 4 : 5;
|
||||
if (!forecast)
|
||||
return Array.from({
|
||||
length: count
|
||||
}, () => null);
|
||||
|
||||
const hours = [];
|
||||
const hour = new Date().getHours();
|
||||
|
||||
const today = forecast[0].hourly;
|
||||
const arr = [...today, ...forecast[1].hourly];
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
const time = parseInt(arr[i].time, 10) / 100;
|
||||
|
||||
if (i > today.length ? time < hour : time > hour) {
|
||||
hours.push(arr[i]);
|
||||
count--;
|
||||
}
|
||||
|
||||
if (count === 0)
|
||||
break;
|
||||
}
|
||||
|
||||
return hours;
|
||||
return forecast.slice(0, count);
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
|
|
@ -160,9 +143,7 @@ ColumnLayout {
|
|||
StyledText {
|
||||
Layout.fillWidth: true
|
||||
text: {
|
||||
if (!forecastHour.modelData)
|
||||
return "00 AM";
|
||||
const hour = parseInt(forecastHour.modelData.time, 10) / 100;
|
||||
const hour = forecastHour.modelData?.hour ?? 0;
|
||||
return hour > 12 ? `${(hour - 12).toString().padStart(2, "0")} PM` : `${hour.toString().padStart(2, "0")} AM`;
|
||||
}
|
||||
color: Colours.palette.m3outline
|
||||
|
|
@ -172,7 +153,7 @@ ColumnLayout {
|
|||
|
||||
MaterialIcon {
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
text: forecastHour.modelData ? Icons.getWeatherIcon(forecastHour.modelData.weatherCode) : "cloud_alert"
|
||||
text: forecastHour.modelData?.icon ?? "cloud_alert"
|
||||
font.pointSize: Appearance.font.size.extraLarge * 1.5
|
||||
font.weight: 500
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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("&");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue