Skip to content

Commit

Permalink
Fixed bulk operation
Browse files Browse the repository at this point in the history
  • Loading branch information
hishitetsu committed Jan 30, 2024
1 parent 41e57dd commit ec9839d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
65 changes: 45 additions & 20 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ void ClearDisplay()
// Note that both DataGrid and GridView don't support multi-items changes notification, so here
// we have to call BeginBulkOperation to suppress CollectionChanged and call EndBulkOperation
// in the end to fire a CollectionChanged event with NotifyCollectionChangedAction.Reset
FilesAndFolders.BeginBulkOperation();

// After calling BeginBulkOperation, ObservableCollection.CollectionChanged is suppressed
// so modifies to FilesAndFolders won't trigger UI updates, hence below operations can be
Expand Down Expand Up @@ -715,12 +714,32 @@ void UpdateUI()

if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && dispatcherQueue.HasThreadAccess)
{
await Task.Run(ApplyChanges);
FilesAndFolders.BeginBulkOperation();
try
{
await Task.Run(ApplyChanges);
}
catch (Exception)
{
// Ensure to end the bulk operation
FilesAndFolders.EndBulkOperation();
throw;
}
UpdateUI();
}
else
{
ApplyChanges();
FilesAndFolders.BeginBulkOperation();
try
{
ApplyChanges();
}
catch (Exception)
{
// Ensure to end the bulk operation
await dispatcherQueue.EnqueueOrInvokeAsync(FilesAndFolders.EndBulkOperation);
throw;
}
await dispatcherQueue.EnqueueOrInvokeAsync(UpdateUI);
}
}
Expand Down Expand Up @@ -821,29 +840,35 @@ public async Task GroupOptionsUpdatedAsync(CancellationToken token)
try
{
FilesAndFolders.BeginBulkOperation();
UpdateGroupOptions();

if (FilesAndFolders.IsGrouped)
try
{
await Task.Run(() =>
UpdateGroupOptions();

if (FilesAndFolders.IsGrouped)
{
await Task.Run(() =>
{
FilesAndFolders.ResetGroups(token);
if (token.IsCancellationRequested)
return;

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

if (token.IsCancellationRequested)
return;

OrderGroups();
});
}
else
finally
{
await OrderFilesAndFoldersAsync();
await dispatcherQueue.EnqueueOrInvokeAsync(
FilesAndFolders.EndBulkOperation);
}

if (token.IsCancellationRequested)
return;

await dispatcherQueue.EnqueueOrInvokeAsync(
FilesAndFolders.EndBulkOperation);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class BulkConcurrentObservableCollection<T> : INotifyCollectionChanged, I
protected bool isBulkOperationStarted;
private readonly object syncRoot = new object();
private readonly List<T> collection = new List<T>();
private readonly SemaphoreSlim bulkOperationSemaphore = new SemaphoreSlim(1, 1);

// When 'GroupOption' is set to 'None' or when a folder is opened, 'GroupedCollection' is assigned 'null' by 'ItemGroupKeySelector'
public BulkConcurrentObservableCollection<GroupedCollection<T>>? GroupedCollection { get; private set; }
Expand Down Expand Up @@ -118,6 +119,7 @@ public BulkConcurrentObservableCollection(IEnumerable<T> items)

public virtual void BeginBulkOperation()
{
bulkOperationSemaphore.WaitAsync();
lock (syncRoot)
{
isBulkOperationStarted = true;
Expand Down Expand Up @@ -226,19 +228,24 @@ private void RemoveItemsFromGroup(IEnumerable<T> items)

public virtual void EndBulkOperation()
{
lock (syncRoot)
try
{
if (!isBulkOperationStarted)
return;
lock (syncRoot)
{
if (!isBulkOperationStarted)
return;

isBulkOperationStarted = false;
GroupedCollection?.ForEach(gp => gp.EndBulkOperation());
GroupedCollection?.EndBulkOperation();
isBulkOperationStarted = false;
GroupedCollection?.ForEach(gp => gp.EndBulkOperation());
GroupedCollection?.EndBulkOperation();

OnCollectionChanged(EventArgsCache.ResetCollectionChanged);
OnCollectionChanged(EventArgsCache.ResetCollectionChanged);
}
}
finally
{
bulkOperationSemaphore.Release();
}

UpdateGroups(EventArgsCache.ResetCollectionChanged);
}

public void Add(T? item)
Expand Down

0 comments on commit ec9839d

Please sign in to comment.