diff --git a/src/Files.App/ServicesImplementation/Settings/FileTagsSettingsService.cs b/src/Files.App/ServicesImplementation/Settings/FileTagsSettingsService.cs index d692b7c02c10..07cb0fa9548b 100644 --- a/src/Files.App/ServicesImplementation/Settings/FileTagsSettingsService.cs +++ b/src/Files.App/ServicesImplementation/Settings/FileTagsSettingsService.cs @@ -1,5 +1,5 @@ using Files.App.Extensions; -using Files.App.Helpers; +using Files.App.Filesystem; using Files.App.Serialization; using Files.App.Serialization.Implementation; using Files.Backend.Services.Settings; @@ -114,6 +114,8 @@ public void DeleteTag(string uid) var oldTags = FileTagList.ToList(); oldTags.RemoveAt(index); FileTagList = oldTags; + UntagAllFiles(uid); + OnTagsUpdated.Invoke(this, EventArgs.Empty); } @@ -163,5 +165,20 @@ public override object ExportSettings() return (tag, index); } + + private void UntagAllFiles(string uid) + { + var tagDoDelete = new string [] { uid }; + + foreach (var item in FileTagsHelper.GetDbInstance().GetAll()) + { + if (item.Tags.Contains(uid)) + { + FileTagsHelper.WriteFileTag( + item.FilePath, + item.Tags.Except(tagDoDelete).ToArray()); + } + } + } } } diff --git a/src/Files.App/ViewModels/ItemViewModel.cs b/src/Files.App/ViewModels/ItemViewModel.cs index 265d415578f0..cc87a41d00bc 100644 --- a/src/Files.App/ViewModels/ItemViewModel.cs +++ b/src/Files.App/ViewModels/ItemViewModel.cs @@ -376,7 +376,8 @@ public ItemViewModel(FolderSettingsViewModel folderSettingsViewModel) dispatcherQueue = DispatcherQueue.GetForCurrentThread(); UserSettingsService.OnSettingChangedEvent += UserSettingsService_OnSettingChangedEvent; - fileTagsSettingsService.OnSettingImportedEvent += FileTagsSettingsService_OnSettingImportedEvent; + fileTagsSettingsService.OnSettingImportedEvent += FileTagsSettingsService_OnSettingUpdated; + fileTagsSettingsService.OnTagsUpdated += FileTagsSettingsService_OnSettingUpdated; folderSizeProvider.SizeChanged += FolderSizeProvider_SizeChanged; RecycleBinManager.Default.RecycleBinItemCreated += RecycleBinItemCreated; RecycleBinManager.Default.RecycleBinItemDeleted += RecycleBinItemDeleted; @@ -473,7 +474,7 @@ await dispatcherQueue.EnqueueAsync(() => } } - private async void FileTagsSettingsService_OnSettingImportedEvent(object? sender, EventArgs e) + private async void FileTagsSettingsService_OnSettingUpdated(object? sender, EventArgs e) { await dispatcherQueue.EnqueueAsync(() => { @@ -2281,7 +2282,8 @@ public void Dispose() RecycleBinManager.Default.RecycleBinItemDeleted -= RecycleBinItemDeleted; RecycleBinManager.Default.RecycleBinRefreshRequested -= RecycleBinRefreshRequested; UserSettingsService.OnSettingChangedEvent -= UserSettingsService_OnSettingChangedEvent; - fileTagsSettingsService.OnSettingImportedEvent -= FileTagsSettingsService_OnSettingImportedEvent; + fileTagsSettingsService.OnSettingImportedEvent -= FileTagsSettingsService_OnSettingUpdated; + fileTagsSettingsService.OnTagsUpdated -= FileTagsSettingsService_OnSettingUpdated; folderSizeProvider.SizeChanged -= FolderSizeProvider_SizeChanged; DefaultIcons.Clear(); } diff --git a/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs b/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs index 2f5ad39e0dbe..e0e469d96e6c 100644 --- a/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs +++ b/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs @@ -40,8 +40,6 @@ public sealed partial class DetailsLayoutBrowser : StandardViewBase private ListedItem? _nextItemToSelect; - private IFileTagsSettingsService tagsSettingsService { get; } = Ioc.Default.GetRequiredService(); - protected override uint IconSize => currentIconSize; protected override ListViewBase ListViewBase => FileList; @@ -757,7 +755,7 @@ private void TagIcon_Tapped(object sender, TappedRoutedEventArgs e) if (tagName is null || parent?.DataContext is not ListedItem item) return; - var tagId = tagsSettingsService.GetTagsByName(tagName).FirstOrDefault()?.Uid; + var tagId = FileTagsSettingsService.GetTagsByName(tagName).FirstOrDefault()?.Uid; item.FileTags = item.FileTags .Except(new string[] { tagId }) diff --git a/src/Files.App/Views/SettingsPages/Advanced.xaml.cs b/src/Files.App/Views/SettingsPages/Advanced.xaml.cs index 6734409ecde6..9aa754418e12 100644 --- a/src/Files.App/Views/SettingsPages/Advanced.xaml.cs +++ b/src/Files.App/Views/SettingsPages/Advanced.xaml.cs @@ -100,16 +100,16 @@ private void NewTagTextBox_TextChanged(object sender, TextChangedEventArgs e) private bool IsNameValid(string name) { return !( - string.IsNullOrWhiteSpace(name) || - name.StartsWith('.') || - name.EndsWith('.') || + string.IsNullOrWhiteSpace(name) || + name.StartsWith('.') || + name.EndsWith('.') || ViewModel.Tags.Any(tag => name == tag.Tag.Name) ); } private void KeyboardAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { - if (args.KeyboardAccelerator.Key is VirtualKey.Escape) + if (args.KeyboardAccelerator.Key is VirtualKey.Escape && editingTag is not null) { CloseEdit(); args.Handled = true;