summaryrefslogtreecommitdiff
path: root/services/Weather.qml
blob: f3147a818c3ab19f77c97a4940bc7467c2c46a79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pragma Singleton

import qs.config
import qs.utils
import Quickshell
import QtQuick

Singleton {
    id: root

    property string city
    property var cc
    property var forecast
    readonly property string icon: cc ? Icons.getWeatherIcon(cc.weatherCode) : "cloud_alert"
    readonly property string description: cc?.weatherDesc[0].value ?? 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 int humidity: cc?.humidity ?? 0

    function reload(): void {
        if (Config.services.weatherLocation)
            city = Config.services.weatherLocation;
        else if (!city || timer.elapsed() > 900)
            Requests.get("https://ipinfo.io/json", text => {
                city = JSON.parse(text).city ?? "";
                timer.restart();
            });
    }

    onCityChanged: Requests.get(`https://wttr.in/${city}?format=j1`, text => {
        const json = JSON.parse(text);
        cc = json.current_condition[0];
        forecast = json.weather;
    })

    ElapsedTimer {
        id: timer
    }
}