Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Remove deleted tags from items #11309

Merged
Merged
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
}
}
}
}
}
8 changes: 5 additions & 3 deletions src/Files.App/ViewModels/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(() =>
{
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 1 addition & 3 deletions src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public sealed partial class DetailsLayoutBrowser : StandardViewBase

private ListedItem? _nextItemToSelect;

private IFileTagsSettingsService tagsSettingsService { get; } = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();

protected override uint IconSize => currentIconSize;

protected override ListViewBase ListViewBase => FileList;
Expand Down Expand Up @@ -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 })
Expand Down
8 changes: 4 additions & 4 deletions src/Files.App/Views/SettingsPages/Advanced.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down