Skip to content

Commit f64c971

Browse files
authored
Fix: Fixed ArgumentOutOfRangeException in ItemViewModel (#14634)
1 parent 21cb4f2 commit f64c971

File tree

1 file changed

+25
-93
lines changed

1 file changed

+25
-93
lines changed

src/Files.App/Data/Models/ItemViewModel.cs

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -692,67 +692,21 @@ void ClearDisplay()
692692
var isSemaphoreReleased = false;
693693
try
694694
{
695-
FilesAndFolders.BeginBulkOperation();
696-
697-
// After calling BeginBulkOperation, ObservableCollection.CollectionChanged is suppressed
698-
// so modifies to FilesAndFolders won't trigger UI updates, hence below operations can be
699-
// run safely without needs of dispatching to UI thread
700-
void ApplyChanges()
695+
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
701696
{
702-
var startIndex = -1;
703-
var tempList = new List<ListedItem>();
704-
705-
void ApplyBulkInsertEntries()
697+
try
706698
{
707-
if (startIndex != -1)
708-
{
709-
FilesAndFolders.ReplaceRange(startIndex, tempList);
710-
startIndex = -1;
711-
tempList.Clear();
712-
}
713-
}
699+
FilesAndFolders.BeginBulkOperation();
714700

715-
for (var i = 0; i < filesAndFoldersLocal.Count; i++)
716-
{
717701
if (addFilesCTS.IsCancellationRequested)
718702
return;
719703

720-
if (i < FilesAndFolders.Count)
721-
{
722-
if (FilesAndFolders[i] != filesAndFoldersLocal[i])
723-
{
724-
if (startIndex == -1)
725-
startIndex = i;
704+
FilesAndFolders.Clear();
705+
FilesAndFolders.AddRange(filesAndFoldersLocal);
726706

727-
tempList.Add(filesAndFoldersLocal[i]);
728-
}
729-
else
730-
{
731-
ApplyBulkInsertEntries();
732-
}
733-
}
734-
else
735-
{
736-
ApplyBulkInsertEntries();
737-
FilesAndFolders.InsertRange(i, filesAndFoldersLocal.Skip(i));
707+
if (folderSettings.DirectoryGroupOption != GroupOption.None)
708+
OrderGroups();
738709

739-
break;
740-
}
741-
}
742-
743-
ApplyBulkInsertEntries();
744-
745-
if (FilesAndFolders.Count > filesAndFoldersLocal.Count)
746-
FilesAndFolders.RemoveRange(filesAndFoldersLocal.Count, FilesAndFolders.Count - filesAndFoldersLocal.Count);
747-
748-
if (folderSettings.DirectoryGroupOption != GroupOption.None)
749-
OrderGroups();
750-
}
751-
752-
void UpdateUI()
753-
{
754-
try
755-
{
756710
// Trigger CollectionChanged with NotifyCollectionChangedAction.Reset
757711
// once loading is completed so that UI can be updated
758712
FilesAndFolders.EndBulkOperation();
@@ -764,21 +718,10 @@ void UpdateUI()
764718
isSemaphoreReleased = true;
765719
bulkOperationSemaphore.Release();
766720
}
767-
}
768-
769-
if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && dispatcherQueue.HasThreadAccess)
770-
{
771-
await Task.Run(ApplyChanges);
772-
UpdateUI();
773-
}
774-
else
775-
{
776-
ApplyChanges();
777-
await dispatcherQueue.EnqueueOrInvokeAsync(UpdateUI);
721+
});
778722

779-
// The semaphore will be released in UI thread
780-
isSemaphoreReleased = true;
781-
}
723+
// The semaphore will be released in UI thread
724+
isSemaphoreReleased = true;
782725
}
783726
finally
784727
{
@@ -806,10 +749,6 @@ private Task RequestSelectionAsync(List<ListedItem> itemsToSelect)
806749

807750
private Task OrderFilesAndFoldersAsync()
808751
{
809-
// Sorting group contents is handled elsewhere
810-
if (folderSettings.DirectoryGroupOption != GroupOption.None)
811-
return Task.CompletedTask;
812-
813752
void OrderEntries()
814753
{
815754
if (filesAndFolders.Count == 0)
@@ -886,32 +825,25 @@ public async Task GroupOptionsUpdatedAsync(CancellationToken token)
886825
var isSemaphoreReleased = false;
887826
try
888827
{
889-
FilesAndFolders.BeginBulkOperation();
890-
UpdateGroupOptions();
891-
892-
if (FilesAndFolders.IsGrouped)
828+
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
893829
{
894-
await Task.Run(() =>
830+
try
895831
{
896-
FilesAndFolders.ResetGroups(token);
897-
if (token.IsCancellationRequested)
898-
return;
832+
FilesAndFolders.BeginBulkOperation();
833+
UpdateGroupOptions();
899834

900-
OrderGroups();
901-
});
902-
}
903-
else
904-
{
905-
await OrderFilesAndFoldersAsync();
906-
}
835+
if (FilesAndFolders.IsGrouped)
836+
{
837+
FilesAndFolders.ResetGroups(token);
838+
if (token.IsCancellationRequested)
839+
return;
907840

908-
if (token.IsCancellationRequested)
909-
return;
841+
OrderGroups();
842+
}
843+
844+
if (token.IsCancellationRequested)
845+
return;
910846

911-
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
912-
{
913-
try
914-
{
915847
FilesAndFolders.EndBulkOperation();
916848
}
917849
finally
@@ -1043,7 +975,7 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
1043975
// Add the file icon to the DefaultIcons list
1044976
if
1045977
(
1046-
!DefaultIcons.ContainsKey(item.FileExtension.ToLowerInvariant()) &&
978+
!DefaultIcons.ContainsKey(item.FileExtension.ToLowerInvariant()) &&
1047979
!string.IsNullOrEmpty(item.FileExtension) &&
1048980
!item.IsShortcut &&
1049981
!item.IsExecutable

0 commit comments

Comments
 (0)