aboutsummaryrefslogtreecommitdiffstats
path: root/examples/CityTemperatures/Weather.cs
blob: 1f5441ef440a1a08b554252c54187a811492eb97 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using OpenMeteo;
using Qt.DotNet.Utils;

namespace CityTemperatures
{
    public class Weather : INotifyPropertyChanged
    {
        private LazyFactory lazy = new();

        public Weather()
        {
            lazy.PropertyChanged += OnPropertyChanged;
            StartRefreshLoop();
        }

        public string Location
        {
            get => lazy.Get(() => Location, () => "");
            set => lazy.Set(() => Location, value);
        }

        public bool IsValid
        {
            get => lazy.Get(() => IsValid, () => false);
            set => lazy.Set(() => IsValid, value);
        }

        public double Temperature
        {
            get => lazy.Get(() => Temperature, () => 0.0);
            set => lazy.Set(() => Temperature, value);
        }

        public string TemperatureUnits
        {
            get => lazy.Get(() => TemperatureUnits, () => "");
            set => lazy.Set(() => TemperatureUnits, value);
        }

        public event PropertyChangedEventHandler PropertyChanged
        {
            add => lazy.PropertyChanged += value;
            remove => lazy.PropertyChanged -= value;
        }

        private OpenMeteoClient Client { get; } = new();

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Location))
                Refresh();
        }

        private void StartRefreshLoop()
        {
            Refresh();
            _ = Task.Run(() =>
            {
                var refreshTimer = Stopwatch.StartNew();
                while (true) {
                    Thread.Sleep(100);
                    if (refreshTimer.Elapsed.TotalMinutes >= 2) {
                        Refresh();
                        refreshTimer.Restart();
                    }
                }
            });
        }

        public void Refresh()
        {
            var location = Location;
            if (!string.IsNullOrEmpty(location) && Client.Query(location) is { } result) {
                Temperature = result.Current.Temperature ?? 0;
                TemperatureUnits = result.CurrentUnits.Temperature;
                IsValid = true;
            } else {
                Temperature = 0;
                TemperatureUnits = "";
                IsValid = false;
            }
        }
    }
}