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 {
|
Repeater {
|
||||||
model: {
|
model: {
|
||||||
const forecast = Weather.forecast;
|
const forecast = Weather.hourlyForecast;
|
||||||
let count = root.width < 320 ? 3 : root.width < 400 ? 4 : 5;
|
const count = root.width < 320 ? 3 : root.width < 400 ? 4 : 5;
|
||||||
if (!forecast)
|
if (!forecast)
|
||||||
return Array.from({
|
return Array.from({
|
||||||
length: count
|
length: count
|
||||||
}, () => null);
|
}, () => null);
|
||||||
|
|
||||||
const hours = [];
|
return forecast.slice(0, count);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnLayout {
|
ColumnLayout {
|
||||||
|
|
@ -160,9 +143,7 @@ ColumnLayout {
|
||||||
StyledText {
|
StyledText {
|
||||||
Layout.fillWidth: true
|
Layout.fillWidth: true
|
||||||
text: {
|
text: {
|
||||||
if (!forecastHour.modelData)
|
const hour = forecastHour.modelData?.hour ?? 0;
|
||||||
return "00 AM";
|
|
||||||
const hour = parseInt(forecastHour.modelData.time, 10) / 100;
|
|
||||||
return hour > 12 ? `${(hour - 12).toString().padStart(2, "0")} PM` : `${hour.toString().padStart(2, "0")} AM`;
|
return hour > 12 ? `${(hour - 12).toString().padStart(2, "0")} PM` : `${hour.toString().padStart(2, "0")} AM`;
|
||||||
}
|
}
|
||||||
color: Colours.palette.m3outline
|
color: Colours.palette.m3outline
|
||||||
|
|
@ -172,7 +153,7 @@ ColumnLayout {
|
||||||
|
|
||||||
MaterialIcon {
|
MaterialIcon {
|
||||||
Layout.alignment: Qt.AlignHCenter
|
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.pointSize: Appearance.font.size.extraLarge * 1.5
|
||||||
font.weight: 500
|
font.weight: 500
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,13 @@ Singleton {
|
||||||
property string city
|
property string city
|
||||||
property string loc
|
property string loc
|
||||||
property var cc
|
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 icon: cc ? Icons.getWeatherIcon(cc.weatherCode) : "cloud_alert"
|
||||||
readonly property string description: cc?.weatherDesc ?? qsTr("No weather")
|
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 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 string feelsLike: Config.services.useFahrenheit ? `${cc?.feelsLikeF ?? 0}°F` : `${cc?.feelsLikeC ?? 0}°C`
|
||||||
readonly property int humidity: cc?.humidity ?? 0
|
readonly property int humidity: cc?.humidity ?? 0
|
||||||
readonly property real windSpeed: cc?.windSpeed ?? 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") : "--:--"
|
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;
|
return;
|
||||||
|
|
||||||
cc = {
|
cc = {
|
||||||
"weatherCode": String(json.current.weather_code),
|
weatherCode: json.current.weather_code,
|
||||||
"weatherDesc": getWeatherCondition(String(json.current.weather_code)),
|
weatherDesc: getWeatherCondition(json.current.weather_code),
|
||||||
"temp_C": Math.round(json.current.temperature_2m),
|
tempC: Math.round(json.current.temperature_2m),
|
||||||
"temp_F": Math.round(json.current.temperature_2m * 9 / 5 + 32),
|
tempF: Math.round(toFahrenheit(json.current.temperature_2m)),
|
||||||
"FeelsLikeC": Math.round(json.current.apparent_temperature),
|
feelsLikeC: Math.round(json.current.apparent_temperature),
|
||||||
"FeelsLikeF": Math.round(json.current.apparent_temperature * 9 / 5 + 32),
|
feelsLikeF: Math.round(toFahrenheit(json.current.apparent_temperature)),
|
||||||
"humidity": json.current.relative_humidity_2m,
|
humidity: json.current.relative_humidity_2m,
|
||||||
"windSpeed": json.current.wind_speed_10m,
|
windSpeed: json.current.wind_speed_10m,
|
||||||
"isDay": json.current.is_day,
|
isDay: json.current.is_day,
|
||||||
"sunrise": json.daily.sunrise[0],
|
sunrise: json.daily.sunrise[0],
|
||||||
"sunset": json.daily.sunset[0]
|
sunset: json.daily.sunset[0]
|
||||||
};
|
};
|
||||||
|
|
||||||
const forecastList = [];
|
const forecastList = [];
|
||||||
for (let i = 0; i < json.daily.time.length; i++)
|
for (let i = 0; i < json.daily.time.length; i++)
|
||||||
forecastList.push({
|
forecastList.push({
|
||||||
"date": json.daily.time[i],
|
date: json.daily.time[i],
|
||||||
"maxTempC": Math.round(json.daily.temperature_2m_max[i]),
|
maxTempC: Math.round(json.daily.temperature_2m_max[i]),
|
||||||
"maxTempF": Math.round(json.daily.temperature_2m_max[i] * 9 / 5 + 32),
|
maxTempF: Math.round(toFahrenheit(json.daily.temperature_2m_max[i])),
|
||||||
"minTempC": Math.round(json.daily.temperature_2m_min[i]),
|
minTempC: Math.round(json.daily.temperature_2m_min[i]),
|
||||||
"minTempF": Math.round(json.daily.temperature_2m_min[i] * 9 / 5 + 32),
|
minTempF: Math.round(toFahrenheit(json.daily.temperature_2m_min[i])),
|
||||||
"weatherCode": String(json.daily.weather_code[i]),
|
weatherCode: json.daily.weather_code[i],
|
||||||
"icon": Icons.getWeatherIcon(String(json.daily.weather_code[i]))
|
icon: Icons.getWeatherIcon(json.daily.weather_code[i])
|
||||||
});
|
});
|
||||||
|
|
||||||
forecast = forecastList;
|
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 {
|
function getWeatherUrl(): string {
|
||||||
if (!loc || loc.indexOf(",") === -1)
|
if (!loc || loc.indexOf(",") === -1)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
const [lat, lon] = loc.split(",");
|
const [lat, lon] = loc.split(",");
|
||||||
const baseUrl = "https://api.open-meteo.com/v1/forecast";
|
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("&");
|
return baseUrl + "?" + params.join("&");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue