@@ -12,6 +12,8 @@ namespace Files.App.ViewModels.UserControls.Widgets
1212 /// </summary>
1313 public sealed partial class FileTagsWidgetViewModel : BaseWidgetViewModel , IWidgetViewModel
1414 {
15+ private CancellationTokenSource _updateCTS ;
16+
1517 // Properties
1618
1719 public ObservableCollection < WidgetFileTagsContainerItem > Containers { get ; } = [ ] ;
@@ -34,6 +36,8 @@ public FileTagsWidgetViewModel()
3436 {
3537 _ = InitializeWidget ( ) ;
3638
39+ FileTagsSettingsService . OnTagsUpdated += FileTagsSettingsService_OnTagsUpdated ;
40+
3741 PinToSidebarCommand = new AsyncRelayCommand < WidgetCardItem > ( ExecutePinToSidebarCommand ) ;
3842 UnpinFromSidebarCommand = new AsyncRelayCommand < WidgetCardItem > ( ExecuteUnpinFromSidebarCommand ) ;
3943 OpenFileLocationCommand = new RelayCommand < WidgetCardItem > ( ExecuteOpenFileLocationCommand ) ;
@@ -46,20 +50,44 @@ public async Task InitializeWidget()
4650 {
4751 await foreach ( var item in FileTagsService . GetTagsAsync ( ) )
4852 {
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 ;
5465
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+ }
5778 }
5879 }
5980
60- public Task RefreshWidgetAsync ( )
81+ private void CreateTagContainerItem ( TagViewModel tag )
6182 {
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 ( ) ;
6391 }
6492
6593 public override List < ContextMenuFlyoutItemViewModel > GetItemMenuItems ( WidgetCardItem item , bool isPinned , bool isFolder = false )
@@ -189,10 +217,16 @@ private void ExecuteOpenFileLocationCommand(WidgetCardItem? item)
189217 } ) ;
190218 }
191219
220+ private async void FileTagsSettingsService_OnTagsUpdated ( object ? sender , EventArgs e )
221+ {
222+ await RefreshWidgetAsync ( ) ;
223+ }
224+
192225 // Disposer
193226
194227 public void Dispose ( )
195228 {
229+ FileTagsSettingsService . OnTagsUpdated -= FileTagsSettingsService_OnTagsUpdated ;
196230 }
197231 }
198232}
0 commit comments