Skip to content

Commit 1beddf8

Browse files
committed
Ensure Item Load is Properly Cancelled on Navigations
1 parent 31cb3de commit 1beddf8

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

Files UWP/Filesystem/ItemViewModel.cs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public ItemViewModel()
7676
App.selectedTabInstance.LayoutItems.PropertyChanged += LayoutItems_PropertyChanged;
7777
App.selectedTabInstance.AlwaysPresentCommands.PropertyChanged += AlwaysPresentCommands_PropertyChanged;
7878

79+
_cancellationTokenSource = new CancellationTokenSource();
80+
7981
Universal.PropertyChanged += Universal_PropertyChanged;
8082
}
8183

@@ -233,13 +235,12 @@ public void RemoveFileOrFolder(ListedItem item)
233235

234236
public void CancelLoadAndClearFiles()
235237
{
236-
if (_cancellationTokenSource == null) { return; }
238+
if (isLoadingItems == false) { return; }
237239

238240
_cancellationTokenSource.Cancel();
239241
_filesAndFolders.Clear();
240-
241242
//_folderQueryResult.ContentsChanged -= FolderContentsChanged;
242-
if(_fileQueryResult != null)
243+
if (_fileQueryResult != null)
243244
{
244245
_fileQueryResult.ContentsChanged -= FileContentsChanged;
245246
}
@@ -264,7 +265,7 @@ public async void DisplayConsentDialog()
264265
{
265266
await App.selectedTabInstance.consentDialog.ShowAsync();
266267
}
267-
268+
bool isLoadingItems = false;
268269
public async void AddItemsToCollectionAsync(string path)
269270
{
270271
App.selectedTabInstance.RefreshButton.IsEnabled = false;
@@ -273,8 +274,7 @@ public async void AddItemsToCollectionAsync(string path)
273274
var instanceTabsView = rootFrame.Content as InstanceTabsView;
274275
instanceTabsView.SetSelectedTabInfo(new DirectoryInfo(path).Name, path);
275276
CancelLoadAndClearFiles();
276-
_cancellationTokenSource = new CancellationTokenSource();
277-
277+
isLoadingItems = true;
278278
if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(GenericFileBrowser))
279279
{
280280
(App.selectedTabInstance.accessibleContentFrame.Content as GenericFileBrowser).TextState.isVisible = Visibility.Collapsed;
@@ -377,8 +377,13 @@ public async void AddItemsToCollectionAsync(string path)
377377
{
378378
foreach (StorageFolder folder in storageFolders)
379379
{
380-
if (_cancellationTokenSource.IsCancellationRequested) { return; }
381-
await AddFolder(folder, _cancellationTokenSource.Token);
380+
if (_cancellationTokenSource.IsCancellationRequested)
381+
{
382+
_cancellationTokenSource = new CancellationTokenSource();
383+
isLoadingItems = false;
384+
return;
385+
}
386+
await AddFolder(folder);
382387
}
383388
index += _step;
384389
storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step);
@@ -393,8 +398,13 @@ public async void AddItemsToCollectionAsync(string path)
393398
{
394399
foreach (StorageFile file in storageFiles)
395400
{
396-
if (_cancellationTokenSource.IsCancellationRequested) { return; }
397-
await AddFile(file, _cancellationTokenSource.Token);
401+
if (_cancellationTokenSource.IsCancellationRequested)
402+
{
403+
_cancellationTokenSource = new CancellationTokenSource();
404+
isLoadingItems = false;
405+
return;
406+
}
407+
await AddFile(file);
398408
}
399409
index += _step;
400410
storageFiles = await _fileQueryResult.GetFilesAsync(index, _step);
@@ -403,12 +413,22 @@ public async void AddItemsToCollectionAsync(string path)
403413
{
404414
if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(GenericFileBrowser))
405415
{
406-
if (_cancellationTokenSource.IsCancellationRequested) { return; }
416+
if (_cancellationTokenSource.IsCancellationRequested)
417+
{
418+
_cancellationTokenSource = new CancellationTokenSource();
419+
isLoadingItems = false;
420+
return;
421+
}
407422
(App.selectedTabInstance.accessibleContentFrame.Content as GenericFileBrowser).TextState.isVisible = Visibility.Visible;
408423
}
409424
else if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(PhotoAlbum))
410425
{
411-
if (_cancellationTokenSource.IsCancellationRequested) { return; }
426+
if (_cancellationTokenSource.IsCancellationRequested)
427+
{
428+
_cancellationTokenSource = new CancellationTokenSource();
429+
isLoadingItems = false;
430+
return;
431+
}
412432
(App.selectedTabInstance.accessibleContentFrame.Content as PhotoAlbum).TextState.isVisible = Visibility.Visible;
413433
}
414434
}
@@ -426,6 +446,7 @@ public async void AddItemsToCollectionAsync(string path)
426446
{
427447
MessageDialog unsupportedDevice = new MessageDialog("This device may be unsupported. Please file an issue report containing what device we couldn't access. Technical information: " + e, "Unsupported Device");
428448
await unsupportedDevice.ShowAsync();
449+
isLoadingItems = false;
429450
return;
430451
}
431452
}
@@ -435,6 +456,7 @@ public async void AddItemsToCollectionAsync(string path)
435456
MessageDialog driveGone = new MessageDialog(e.Message, "Did you unplug this drive?");
436457
await driveGone.ShowAsync();
437458
rootContentFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
459+
isLoadingItems = false;
438460
return;
439461
}
440462
catch (FileNotFoundException)
@@ -443,30 +465,39 @@ public async void AddItemsToCollectionAsync(string path)
443465
MessageDialog folderGone = new MessageDialog("The folder you've navigated to was removed.", "Did you delete this folder?");
444466
await folderGone.ShowAsync();
445467
rootContentFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
468+
isLoadingItems = false;
446469
return;
447470
}
448471

449472
if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(GenericFileBrowser))
450473
{
451-
if (_cancellationTokenSource.IsCancellationRequested) { return; }
474+
if (_cancellationTokenSource.IsCancellationRequested)
475+
{
476+
_cancellationTokenSource = new CancellationTokenSource();
477+
isLoadingItems = false;
478+
return;
479+
}
452480
(App.selectedTabInstance.accessibleContentFrame.Content as GenericFileBrowser).progressBar.Visibility = Visibility.Collapsed;
453481
}
454482
else if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(PhotoAlbum))
455483
{
456-
if (_cancellationTokenSource.IsCancellationRequested) { return; }
484+
if (_cancellationTokenSource.IsCancellationRequested)
485+
{
486+
_cancellationTokenSource = new CancellationTokenSource();
487+
isLoadingItems = false;
488+
return;
489+
}
457490
(App.selectedTabInstance.accessibleContentFrame.Content as PhotoAlbum).progressBar.Visibility = Visibility.Collapsed;
458491
}
492+
isLoadingItems = false;
459493
}
460494

461-
private async Task AddFolder(StorageFolder folder, CancellationToken token)
495+
private async Task AddFolder(StorageFolder folder)
462496
{
463-
if (token.IsCancellationRequested) { return; }
464-
465497
var basicProperties = await folder.GetBasicPropertiesAsync();
466498

467499
if ((App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(GenericFileBrowser)) || (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(PhotoAlbum)))
468500
{
469-
if (token.IsCancellationRequested) { return; }
470501

471502
_filesAndFolders.Add(new ListedItem(folder.FolderRelativeId)
472503
{
@@ -492,10 +523,8 @@ private async Task AddFolder(StorageFolder folder, CancellationToken token)
492523
}
493524
}
494525

495-
private async Task AddFile(StorageFile file, CancellationToken token)
526+
private async Task AddFile(StorageFile file)
496527
{
497-
if (token.IsCancellationRequested) { return; }
498-
499528
var basicProperties = await file.GetBasicPropertiesAsync();
500529

501530
var itemName = file.DisplayName;
@@ -564,8 +593,6 @@ private async Task AddFile(StorageFile file, CancellationToken token)
564593
}
565594
}
566595

567-
if (token.IsCancellationRequested) { return; }
568-
569596
_filesAndFolders.Add(new ListedItem(file.FolderRelativeId)
570597
{
571598
DotFileExtension = itemFileExtension,
@@ -647,7 +674,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
647674
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
648675
async () =>
649676
{
650-
await AddFile(toAdd, cancellationTokenSourceCopy.Token);
677+
await AddFile(toAdd);
651678
});
652679
}
653680
foreach (var folder in addedFolders)
@@ -656,7 +683,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
656683
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
657684
async () =>
658685
{
659-
await AddFolder(toAdd, cancellationTokenSourceCopy.Token);
686+
await AddFolder(toAdd);
660687
});
661688
}
662689
foreach (var item in removedFilesAndFolders)

0 commit comments

Comments
 (0)