Skip to content

Commit e8a331d

Browse files
committed
windows: update weather alert bindings
1 parent b835fbb commit e8a331d

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

SimpleWeather.Common/Controls/WeatherAlertsViewModel.cs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
using SimpleWeather.WeatherData;
1010
using System;
1111
using System.Collections.Generic;
12+
using System.ComponentModel;
13+
using System.Linq;
1214
using System.Threading.Tasks;
1315

1416
namespace SimpleWeather.Common.Controls
1517
{
18+
[Bindable(true)]
1619
#if WINUI
1720
[WinRT.GeneratedBindableCustomProperty]
1821
#endif
@@ -25,11 +28,10 @@ public partial class WeatherAlertsViewModel : BaseViewModel, IDisposable
2528
private ObservableItem<ICollection<WeatherAlert>> currentAlertsData;
2629

2730
[ObservableProperty]
28-
public partial SimpleObservableList<WeatherAlertViewModel> Alerts { get; set; }
31+
public partial ICollection<WeatherAlertViewModel> Alerts { get; set; }
2932

3033
public WeatherAlertsViewModel()
3134
{
32-
Alerts = new SimpleObservableList<WeatherAlertViewModel>();
3335
currentAlertsData = new ObservableItem<ICollection<WeatherAlert>>();
3436
currentAlertsData.ItemValueChanged += CurrentAlertsData_ItemValueChanged;
3537
}
@@ -84,31 +86,12 @@ private void CurrentAlertsData_ItemValueChanged(object sender, ObservableItemCha
8486

8587
private void RefreshAlerts(ICollection<WeatherAlert> alertData)
8688
{
87-
Alerts.Clear();
88-
89-
if (alertData?.Count > 0)
89+
var now = DateTimeOffset.Now;
90+
// Skip if alert has expired
91+
Alerts = alertData?.WhereNot(alert => alert.ExpiresDate <= now || alert.Date > now)?.Select(alert =>
9092
{
91-
var now = DateTimeOffset.Now;
92-
Alerts.EnsureCapacity(alertData.Count);
93-
foreach (var alert in alertData)
94-
{
95-
// Skip if alert has expired
96-
if (alert.ExpiresDate <= now || alert.Date > now)
97-
continue;
98-
99-
Alerts.Add(new WeatherAlertViewModel(alert));
100-
}
101-
}
102-
103-
#if WINUI
104-
DispatcherQueue.EnqueueAsync(() =>
105-
#else
106-
Dispatcher.Dispatch(() =>
107-
#endif
108-
{
109-
Alerts.NotifyCollectionChanged();
110-
OnPropertyChanged(nameof(Alerts));
111-
});
93+
return new WeatherAlertViewModel(alert);
94+
}).ToList();
11295
}
11396

11497
private bool isDisposed;

SimpleWeather.Windows/Controls/WeatherAlertPanel.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public WeatherAlertPanel()
5656
}
5757

5858
MessageSpan.Inlines.Add(new LineBreak());
59+
60+
this.Bindings.Update();
5961
};
6062
}
6163
}

SimpleWeather.Windows/Main/WeatherNow.xaml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,29 @@
151151
HorizontalAlignment="Stretch"
152152
HorizontalContentAlignment="Stretch"
153153
x:Load="{x:Bind AlertsView.Alerts, Mode=OneWay, Converter={StaticResource collectionBooleanConverter}}"
154-
Background="OrangeRed"
154+
Background="{ThemeResource ErrorContainerColor}"
155155
Click="AlertButton_Click"
156156
Loaded="DeferedControl_Loaded">
157157
<Button.Resources>
158-
<SolidColorBrush x:Key="ButtonBackground" Color="OrangeRed" />
159-
<SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="#FFFF5A00" />
160-
<SolidColorBrush x:Key="ButtonBackgroundPressed" Color="#FFFF8A00" />
158+
<SolidColorBrush x:Key="ButtonBackground" Color="{ThemeResource ErrorContainerColor}" />
159+
<SolidColorBrush
160+
x:Key="ButtonBackgroundPointerOver"
161+
Opacity="0.9"
162+
Color="{ThemeResource ErrorContainerColor}" />
163+
<SolidColorBrush
164+
x:Key="ButtonBackgroundPressed"
165+
Opacity="0.8"
166+
Color="{ThemeResource ErrorContainerColor}" />
161167
<StaticResource x:Key="ButtonBackgroundDisabled" ResourceKey="ButtonDisabledBackgroundThemeBrush" />
162-
<SolidColorBrush x:Key="ButtonForeground" Color="White" />
163-
<StaticResource x:Key="ButtonForegroundPointerOver" ResourceKey="SystemControlBackgroundChromeWhiteBrush" />
164-
<StaticResource x:Key="ButtonForegroundPressed" ResourceKey="SystemControlBackgroundChromeWhiteBrush" />
168+
<SolidColorBrush x:Key="ButtonForeground" Color="{ThemeResource OnErrorContainerColor}" />
169+
<SolidColorBrush
170+
x:Key="ButtonForegroundPointerOver"
171+
Opacity="0.9"
172+
Color="{ThemeResource OnErrorContainerColor}" />
173+
<SolidColorBrush
174+
x:Key="ButtonForegroundPressed"
175+
Opacity="0.8"
176+
Color="{ThemeResource OnErrorContainerColor}" />
165177
<StaticResource x:Key="ButtonForegroundDisabled" ResourceKey="SystemControlDisabledBaseMediumLowBrush" />
166178
</Button.Resources>
167179
<Button.Content>

0 commit comments

Comments
 (0)