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: Fixed COMException in ItemsAdded_CollectionChanged #14439

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ await DispatcherQueue.EnqueueOrInvokeAsync(async () =>
if (e.Reset)
{
// Find the intersection between the two lists and determine whether to remove or add
var itemsToRemove = ItemsAdded.Where(x => !e.Paths.Contains(x.Path)).ToList();
var itemsToAdd = e.Paths.Where(x => !ItemsAdded.Any(y => y.Path == x)).ToList();
var originalItemsAdded = ItemsAdded.ToList();
var itemsToRemove = originalItemsAdded.Where(x => !e.Paths.Contains(x.Path));
var itemsToAdd = e.Paths.Where(x => !originalItemsAdded.Any(y => y.Path == x));

// Remove items
foreach (var itemToRemove in itemsToRemove)
Expand All @@ -249,13 +250,14 @@ await DispatcherQueue.EnqueueOrInvokeAsync(async () =>
// Add items
foreach (var itemToAdd in itemsToAdd)
{
var interimItemsAdded = ItemsAdded.ToList();
var item = await App.QuickAccessManager.Model.CreateLocationItemFromPathAsync(itemToAdd);
var lastIndex = ItemsAdded.IndexOf(ItemsAdded.FirstOrDefault(x => !x.IsPinned));
var lastIndex = ItemsAdded.IndexOf(interimItemsAdded.FirstOrDefault(x => !x.IsPinned));
var isPinned = (bool?)e.Items.Where(x => x.FilePath == itemToAdd).FirstOrDefault()?.Properties["System.Home.IsPinned"] ?? false;
if (ItemsAdded.Any(x => x.Path == itemToAdd))
if (interimItemsAdded.Any(x => x.Path == itemToAdd))
continue;

ItemsAdded.Insert(isPinned && lastIndex >= 0 ? lastIndex : ItemsAdded.Count, new FolderCardItem(item, Path.GetFileName(item.Text), isPinned)
ItemsAdded.Insert(isPinned && lastIndex >= 0 ? Math.Min(lastIndex, ItemsAdded.Count) : ItemsAdded.Count, new FolderCardItem(item, Path.GetFileName(item.Text), isPinned)
{
Path = item.Path,
});
Expand All @@ -266,18 +268,19 @@ await DispatcherQueue.EnqueueOrInvokeAsync(async () =>
if (e.Reorder)
{
// Remove pinned items
foreach (var itemToRemove in ItemsAdded.Where(x => x.IsPinned).ToList())
foreach (var itemToRemove in ItemsAdded.ToList().Where(x => x.IsPinned))
ItemsAdded.Remove(itemToRemove);

// Add pinned items in the new order
foreach (var itemToAdd in e.Paths)
{
var interimItemsAdded = ItemsAdded.ToList();
var item = await App.QuickAccessManager.Model.CreateLocationItemFromPathAsync(itemToAdd);
var lastIndex = ItemsAdded.IndexOf(ItemsAdded.FirstOrDefault(x => !x.IsPinned));
if (ItemsAdded.Any(x => x.Path == itemToAdd))
var lastIndex = ItemsAdded.IndexOf(interimItemsAdded.FirstOrDefault(x => !x.IsPinned));
if (interimItemsAdded.Any(x => x.Path == itemToAdd))
continue;

ItemsAdded.Insert(lastIndex >= 0 ? lastIndex : ItemsAdded.Count, new FolderCardItem(item, Path.GetFileName(item.Text), true)
ItemsAdded.Insert(lastIndex >= 0 ? Math.Min(lastIndex, ItemsAdded.Count) : ItemsAdded.Count, new FolderCardItem(item, Path.GetFileName(item.Text), true)
{
Path = item.Path,
});
Expand All @@ -289,18 +292,19 @@ await DispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
foreach (var itemToAdd in e.Paths)
{
var interimItemsAdded = ItemsAdded.ToList();
var item = await App.QuickAccessManager.Model.CreateLocationItemFromPathAsync(itemToAdd);
var lastIndex = ItemsAdded.IndexOf(ItemsAdded.FirstOrDefault(x => !x.IsPinned));
if (ItemsAdded.Any(x => x.Path == itemToAdd))
var lastIndex = ItemsAdded.IndexOf(interimItemsAdded.FirstOrDefault(x => !x.IsPinned));
if (interimItemsAdded.Any(x => x.Path == itemToAdd))
continue;
ItemsAdded.Insert(e.Pin && lastIndex >= 0 ? lastIndex : ItemsAdded.Count, new FolderCardItem(item, Path.GetFileName(item.Text), e.Pin) // Add just after the Recent Folders
ItemsAdded.Insert(e.Pin && lastIndex >= 0 ? Math.Min(lastIndex, ItemsAdded.Count) : ItemsAdded.Count, new FolderCardItem(item, Path.GetFileName(item.Text), e.Pin) // Add just after the Recent Folders
{
Path = item.Path,
});
}
}
else
foreach (var itemToRemove in ItemsAdded.Where(x => e.Paths.Contains(x.Path)).ToList())
foreach (var itemToRemove in ItemsAdded.ToList().Where(x => e.Paths.Contains(x.Path)))
ItemsAdded.Remove(itemToRemove);
});
}
Expand Down Expand Up @@ -389,7 +393,7 @@ public override async Task PinToFavoritesAsync(WidgetCardItem item)
var items = (await QuickAccessService.GetPinnedFoldersAsync())
.Where(link => !((bool?)link.Properties["System.Home.IsPinned"] ?? false));

var recentItem = items.Where(x => !ItemsAdded.Select(y => y.Path).Contains(x.FilePath)).FirstOrDefault();
var recentItem = items.Where(x => !ItemsAdded.ToList().Select(y => y.Path).Contains(x.FilePath)).FirstOrDefault();
if (recentItem is not null)
{
ModifyItemAsync(this, new ModifyQuickAccessEventArgs(new[] { recentItem.FilePath }, true)
Expand Down