Skip to content

Fix: Fixed ArgumentOutOfRangeException in ItemViewModel #14634

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

Merged
merged 3 commits into from
Feb 4, 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
118 changes: 25 additions & 93 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,67 +692,21 @@ void ClearDisplay()
var isSemaphoreReleased = false;
try
{
FilesAndFolders.BeginBulkOperation();

// After calling BeginBulkOperation, ObservableCollection.CollectionChanged is suppressed
// so modifies to FilesAndFolders won't trigger UI updates, hence below operations can be
// run safely without needs of dispatching to UI thread
void ApplyChanges()
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
var startIndex = -1;
var tempList = new List<ListedItem>();

void ApplyBulkInsertEntries()
try
{
if (startIndex != -1)
{
FilesAndFolders.ReplaceRange(startIndex, tempList);
startIndex = -1;
tempList.Clear();
}
}
FilesAndFolders.BeginBulkOperation();

for (var i = 0; i < filesAndFoldersLocal.Count; i++)
{
if (addFilesCTS.IsCancellationRequested)
return;

if (i < FilesAndFolders.Count)
{
if (FilesAndFolders[i] != filesAndFoldersLocal[i])
{
if (startIndex == -1)
startIndex = i;
FilesAndFolders.Clear();
FilesAndFolders.AddRange(filesAndFoldersLocal);
Comment on lines +704 to +705
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this time there is no problem and this simplifies the process.


tempList.Add(filesAndFoldersLocal[i]);
}
else
{
ApplyBulkInsertEntries();
}
}
else
{
ApplyBulkInsertEntries();
FilesAndFolders.InsertRange(i, filesAndFoldersLocal.Skip(i));
if (folderSettings.DirectoryGroupOption != GroupOption.None)
OrderGroups();

break;
}
}

ApplyBulkInsertEntries();

if (FilesAndFolders.Count > filesAndFoldersLocal.Count)
FilesAndFolders.RemoveRange(filesAndFoldersLocal.Count, FilesAndFolders.Count - filesAndFoldersLocal.Count);

if (folderSettings.DirectoryGroupOption != GroupOption.None)
OrderGroups();
}

void UpdateUI()
{
try
{
// Trigger CollectionChanged with NotifyCollectionChangedAction.Reset
// once loading is completed so that UI can be updated
FilesAndFolders.EndBulkOperation();
Expand All @@ -764,21 +718,10 @@ void UpdateUI()
isSemaphoreReleased = true;
bulkOperationSemaphore.Release();
}
}

if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && dispatcherQueue.HasThreadAccess)
{
await Task.Run(ApplyChanges);
UpdateUI();
}
else
{
ApplyChanges();
await dispatcherQueue.EnqueueOrInvokeAsync(UpdateUI);
});

// The semaphore will be released in UI thread
isSemaphoreReleased = true;
}
// The semaphore will be released in UI thread
isSemaphoreReleased = true;
}
finally
{
Expand Down Expand Up @@ -806,10 +749,6 @@ private Task RequestSelectionAsync(List<ListedItem> itemsToSelect)

private Task OrderFilesAndFoldersAsync()
{
// Sorting group contents is handled elsewhere
if (folderSettings.DirectoryGroupOption != GroupOption.None)
return Task.CompletedTask;

void OrderEntries()
{
if (filesAndFolders.Count == 0)
Expand Down Expand Up @@ -886,32 +825,25 @@ public async Task GroupOptionsUpdatedAsync(CancellationToken token)
var isSemaphoreReleased = false;
try
{
FilesAndFolders.BeginBulkOperation();
UpdateGroupOptions();

if (FilesAndFolders.IsGrouped)
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
await Task.Run(() =>
try
{
FilesAndFolders.ResetGroups(token);
if (token.IsCancellationRequested)
return;
FilesAndFolders.BeginBulkOperation();
UpdateGroupOptions();

OrderGroups();
});
}
else
{
await OrderFilesAndFoldersAsync();
}
if (FilesAndFolders.IsGrouped)
{
FilesAndFolders.ResetGroups(token);
if (token.IsCancellationRequested)
return;

if (token.IsCancellationRequested)
return;
OrderGroups();
}

if (token.IsCancellationRequested)
return;

await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
try
{
FilesAndFolders.EndBulkOperation();
}
finally
Expand Down Expand Up @@ -1043,7 +975,7 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
// Add the file icon to the DefaultIcons list
if
(
!DefaultIcons.ContainsKey(item.FileExtension.ToLowerInvariant()) &&
!DefaultIcons.ContainsKey(item.FileExtension.ToLowerInvariant()) &&
!string.IsNullOrEmpty(item.FileExtension) &&
!item.IsShortcut &&
!item.IsExecutable
Expand Down