weather: use city instead of lat/long

Also lazy load
This commit is contained in:
2 * r + 2 * t 2025-08-24 20:42:43 +10:00
parent 61884c07a7
commit 748bfa9358

View file

@ -8,7 +8,7 @@ import QtQuick
Singleton { Singleton {
id: root id: root
property string loc property string city
property var cc property var cc
property var forecast property var forecast
readonly property string icon: cc ? Icons.getWeatherIcon(cc.weatherCode) : "cloud_alert" readonly property string icon: cc ? Icons.getWeatherIcon(cc.weatherCode) : "cloud_alert"
@ -19,22 +19,20 @@ Singleton {
function reload(): void { function reload(): void {
if (Config.services.weatherLocation) if (Config.services.weatherLocation)
loc = Config.services.weatherLocation; city = Config.services.weatherLocation;
else if (!loc || timer.elapsed() > 900) else if (!city || timer.elapsed() > 900)
Requests.get("https://ipinfo.io/json", text => { Requests.get("https://ipinfo.io/json", text => {
loc = JSON.parse(text).loc ?? ""; city = JSON.parse(text).city ?? "";
timer.restart(); timer.restart();
}); });
} }
onLocChanged: Requests.get(`https://wttr.in/${loc}?format=j1`, text => { onCityChanged: Requests.get(`https://wttr.in/${city}?format=j1`, text => {
const json = JSON.parse(text); const json = JSON.parse(text);
cc = json.current_condition[0]; cc = json.current_condition[0];
forecast = json.weather; forecast = json.weather;
}) })
Component.onCompleted: reload()
ElapsedTimer { ElapsedTimer {
id: timer id: timer
} }