summaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
Diffstat (limited to 'services')
-rw-r--r--services/Requests.qml36
-rw-r--r--services/Weather.qml39
2 files changed, 50 insertions, 25 deletions
diff --git a/services/Requests.qml b/services/Requests.qml
new file mode 100644
index 0000000..36e9120
--- /dev/null
+++ b/services/Requests.qml
@@ -0,0 +1,36 @@
+pragma Singleton
+
+import "root:/config"
+import "root:/utils"
+import Quickshell
+
+Singleton {
+ id: root
+
+ function get(url: string, callback: var): void {
+ const xhr = new XMLHttpRequest();
+
+ const cleanup = () => {
+ xhr.abort();
+ xhr.onreadystatechange = null;
+ xhr.onerror = null;
+ };
+
+ xhr.open("GET", url, true);
+ xhr.onreadystatechange = () => {
+ if (xhr.readyState === XMLHttpRequest.DONE) {
+ if (xhr.status === 200)
+ callback(xhr.responseText);
+ else
+ console.warn(`[REQUESTS] GET request to ${url} failed with status ${xhr.status}`);
+ cleanup();
+ }
+ };
+ xhr.onerror = () => {
+ console.warn(`[REQUESTS] GET request to ${url} failed`);
+ cleanup();
+ };
+
+ xhr.send();
+ }
+}
diff --git a/services/Weather.qml b/services/Weather.qml
index 998fdb8..ad80e48 100644
--- a/services/Weather.qml
+++ b/services/Weather.qml
@@ -3,7 +3,6 @@ pragma Singleton
import "root:/config"
import "root:/utils"
import Quickshell
-import Quickshell.Io
import QtQuick
Singleton {
@@ -17,33 +16,23 @@ Singleton {
function reload(): void {
if (Config.dashboard.weatherLocation)
loc = Config.dashboard.weatherLocation;
- else
- ipProc.running = true;
+ else if (!loc || timer.elapsed() > 900)
+ Requests.get("https://ipinfo.io/json", text => {
+ loc = JSON.parse(text).loc ?? "";
+ timer.restart();
+ });
}
- onLocChanged: wttrProc.running = true
- Component.onCompleted: reload()
-
- Process {
- id: ipProc
+ onLocChanged: Requests.get(`https://wttr.in/${loc}?format=j1`, text => {
+ const json = JSON.parse(text).current_condition[0];
+ icon = Icons.getWeatherIcon(json.weatherCode);
+ description = json.weatherDesc[0].value;
+ temperature = parseFloat(json.temp_C);
+ })
- command: ["curl", "ipinfo.io"]
- stdout: StdioCollector {
- onStreamFinished: root.loc = JSON.parse(text).loc ?? ""
- }
- }
-
- Process {
- id: wttrProc
+ Component.onCompleted: reload()
- command: ["curl", `https://wttr.in/${root.loc}?format=j1`]
- stdout: StdioCollector {
- onStreamFinished: {
- const json = JSON.parse(text).current_condition[0];
- root.icon = Icons.getWeatherIcon(json.weatherCode);
- root.description = json.weatherDesc[0].value;
- root.temperature = parseFloat(json.temp_C);
- }
- }
+ ElapsedTimer {
+ id: timer
}
}