| Name | Message | Date |
|---|---|---|
| 📄 DateTimePanelViewModel.cs | 1 month ago | |
| 📄 ServiceCollectionExtensions.cs | 27 days ago | |
| 📄 VasttrafikPanelViewModel.cs | 27 days ago | |
| 📄 WeatherPanelViewModel.cs | 27 days ago |
📄
src/App/ViewModels/Panels/WeatherPanelViewModel.cs
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
using System; using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.Extensions.DependencyInjection; using MMirror.Integrations.HomeAssistant; using MMirror.Integrations.Smhi; namespace MMirror.App.ViewModels.Panels; public partial class WeatherPanelViewModel( TimeProvider timeProvider, SmhiClient smhiClient, HomeAssistantClient homeAssistantClient ) : ViewModelBase { private readonly TimeProvider timeProvider = timeProvider; private readonly SmhiClient smhiClient = smhiClient; private readonly HomeAssistantClient homeAssistantClient = homeAssistantClient; private CancellationTokenSource cts = new(); [ObservableProperty] public partial PointForecast? Forecast { get; private set; } [ObservableProperty] public required partial string LocationName { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public void SubscribeToUpdates() { Task.Run(() => FetchLoop(cts.Token)); } private async Task FetchLoop(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var newForecastTask = smhiClient.GetPointForecast(Longitude, Latitude, cancellationToken); try { var localTemperature = await homeAssistantClient.GetTemperatures(cancellationToken); var newForecast = await newForecastTask; Forecast = newForecast with { Now = newForecast.Now with { Forecast = newForecast.Now.Forecast with { TemperatureCelsius = localTemperature.OutsideCelsius, InsideTemperatureCelsius = localTemperature.InsideCelsius, }, }, }; } catch { Forecast = await newForecastTask; } await Task.Delay(TimeSpan.FromMinutes(15), timeProvider, cancellationToken); } } public void UnsubscribeFromUpdates() { cts.Cancel(); cts = new(); } } internal static class WeatherPanelViewModelServiceCollectionExtensions { extension(IServiceCollection services) { public IServiceCollection AddWeatherPanelViewModel() { services.AddTransient<WeatherPanelViewModel>(); return services; } } }