@@ -12,6 +12,8 @@ namespace Files.App.ViewModels.UserControls.Widgets
12
12
/// </summary>
13
13
public sealed partial class FileTagsWidgetViewModel : BaseWidgetViewModel , IWidgetViewModel
14
14
{
15
+ private CancellationTokenSource _updateCTS ;
16
+
15
17
// Properties
16
18
17
19
public ObservableCollection < WidgetFileTagsContainerItem > Containers { get ; } = [ ] ;
@@ -34,6 +36,8 @@ public FileTagsWidgetViewModel()
34
36
{
35
37
_ = InitializeWidget ( ) ;
36
38
39
+ FileTagsSettingsService . OnTagsUpdated += FileTagsSettingsService_OnTagsUpdated ;
40
+
37
41
PinToSidebarCommand = new AsyncRelayCommand < WidgetCardItem > ( ExecutePinToSidebarCommand ) ;
38
42
UnpinFromSidebarCommand = new AsyncRelayCommand < WidgetCardItem > ( ExecuteUnpinFromSidebarCommand ) ;
39
43
OpenFileLocationCommand = new RelayCommand < WidgetCardItem > ( ExecuteOpenFileLocationCommand ) ;
@@ -46,20 +50,44 @@ public async Task InitializeWidget()
46
50
{
47
51
await foreach ( var item in FileTagsService . GetTagsAsync ( ) )
48
52
{
49
- var container = new WidgetFileTagsContainerItem ( item . Uid )
50
- {
51
- Name = item . Name ,
52
- Color = item . Color
53
- } ;
53
+ CreateTagContainerItem ( item ) ;
54
+ }
55
+ }
56
+
57
+ public async Task RefreshWidgetAsync ( )
58
+ {
59
+ _updateCTS ? . Cancel ( ) ;
60
+ _updateCTS = new CancellationTokenSource ( ) ;
61
+ await foreach ( var item in FileTagsService . GetTagsAsync ( ) )
62
+ {
63
+ if ( _updateCTS . IsCancellationRequested )
64
+ break ;
54
65
55
- Containers . Add ( container ) ;
56
- _ = container . InitAsync ( ) ;
66
+ var matchingItem = Containers . First ( c => c . Uid == item . Uid ) ;
67
+ if ( matchingItem is null )
68
+ {
69
+ CreateTagContainerItem ( item ) ;
70
+ }
71
+ else
72
+ {
73
+ matchingItem . Name = item . Name ;
74
+ matchingItem . Color = item . Color ;
75
+ matchingItem . Tags . Clear ( ) ;
76
+ _ = matchingItem . InitAsync ( _updateCTS . Token ) ;
77
+ }
57
78
}
58
79
}
59
80
60
- public Task RefreshWidgetAsync ( )
81
+ private void CreateTagContainerItem ( TagViewModel tag )
61
82
{
62
- return Task . CompletedTask ;
83
+ var container = new WidgetFileTagsContainerItem ( tag . Uid )
84
+ {
85
+ Name = tag . Name ,
86
+ Color = tag . Color
87
+ } ;
88
+
89
+ Containers . Add ( container ) ;
90
+ _ = container . InitAsync ( ) ;
63
91
}
64
92
65
93
public override List < ContextMenuFlyoutItemViewModel > GetItemMenuItems ( WidgetCardItem item , bool isPinned , bool isFolder = false )
@@ -189,10 +217,16 @@ private void ExecuteOpenFileLocationCommand(WidgetCardItem? item)
189
217
} ) ;
190
218
}
191
219
220
+ private async void FileTagsSettingsService_OnTagsUpdated ( object ? sender , EventArgs e )
221
+ {
222
+ await RefreshWidgetAsync ( ) ;
223
+ }
224
+
192
225
// Disposer
193
226
194
227
public void Dispose ( )
195
228
{
229
+ FileTagsSettingsService . OnTagsUpdated -= FileTagsSettingsService_OnTagsUpdated ;
196
230
}
197
231
}
198
232
}
0 commit comments