Exemple #1
0
        public async Task <ReturnResult> DeleteItemsAsync(IEnumerable <IStorageItemWithPath> source, bool showDialog, bool permanently, bool registerHistory)
        {
            source = await source.ToListAsync();

            var returnStatus = ReturnResult.InProgress;

            var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => recycleBinHelpers.IsPathUnderRecycleBin(path));
            var canBeSentToBin       = !deleteFromRecycleBin && await recycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path);

            if (((!permanently && !canBeSentToBin) || UserSettingsService.PreferencesSettingsService.ShowConfirmDeleteDialog) && showDialog) // Check if the setting to show a confirmation dialog is on
            {
                var incomingItems             = new List <BaseFileSystemDialogItemViewModel>();
                List <ShellFileItem> binItems = null;
                foreach (var src in source)
                {
                    if (recycleBinHelpers.IsPathUnderRecycleBin(src.Path))
                    {
                        binItems ??= await recycleBinHelpers.EnumerateRecycleBin();

                        if (!binItems.IsEmpty())                                                        // Might still be null because we're deserializing the list from Json
                        {
                            var matchingItem = binItems.FirstOrDefault(x => x.RecyclePath == src.Path); // Get original file name
                            incomingItems.Add(new FileSystemDialogDefaultItemViewModel()
                            {
                                SourcePath = src.Path, DisplayName = matchingItem?.FileName ?? src.Name
                            });
                        }
                    }
                    else
                    {
                        incomingItems.Add(new FileSystemDialogDefaultItemViewModel()
                        {
                            SourcePath = src.Path
                        });
                    }
                }

                var dialogViewModel = FileSystemDialogViewModel.GetDialogViewModel(
                    new() { IsInDeleteMode = true },
                    (canBeSentToBin ? permanently : true, canBeSentToBin),
                    FilesystemOperationType.Delete,
                    incomingItems,
                    new());

                var dialogService = Ioc.Default.GetRequiredService <IDialogService>();

                if (await dialogService.ShowDialogAsync(dialogViewModel) != DialogResult.Primary)
                {
                    return(ReturnResult.Cancelled); // Return if the result isn't delete
                }

                // Delete selected items if the result is Yes
                permanently = dialogViewModel.DeletePermanently;
            }

            // post the status banner
            var banner = PostBannerHelpers.PostBanner_Delete(source, returnStatus, permanently, false, 0);

            banner.ErrorCode.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.ToStatus() : returnStatus;

            var token = banner.CancellationToken;

            var sw = new Stopwatch();

            sw.Start();

            IStorageHistory history = await filesystemOperations.DeleteItemsAsync((IList <IStorageItemWithPath>) source, banner.Progress, banner.ErrorCode, permanently, token);

            ((IProgress <float>)banner.Progress).Report(100.0f);
            await Task.Yield();

            if (!permanently && registerHistory)
            {
                App.HistoryWrapper.AddHistory(history);
            }
            var itemsDeleted = history?.Source.Count() ?? 0;

            source.ForEach(x => App.JumpList.RemoveFolder(x.Path)); // Remove items from jump list

            banner.Remove();
            sw.Stop();

            PostBannerHelpers.PostBanner_Delete(source, returnStatus, permanently, token.IsCancellationRequested, itemsDeleted);

            return(returnStatus);
        }
Exemple #2
0
        public async Task <ReturnResult> DeleteItemsAsync(IEnumerable <IStorageItemWithPath> source, bool showDialog, bool permanently, bool registerHistory)
        {
            source = await source.ToListAsync();

            var returnStatus = ReturnResult.InProgress;

            var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => recycleBinHelpers.IsPathUnderRecycleBin(path));
            var canBeSentToBin       = !deleteFromRecycleBin && await recycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path);

            if (((!permanently && !canBeSentToBin) || UserSettingsService.PreferencesSettingsService.ShowConfirmDeleteDialog) && showDialog) // Check if the setting to show a confirmation dialog is on
            {
                List <FilesystemItemsOperationItemModel> incomingItems = new List <FilesystemItemsOperationItemModel>();

                foreach (var src in source)
                {
                    if (recycleBinHelpers.IsPathUnderRecycleBin(src.Path))
                    {
                        var binItems     = associatedInstance.FilesystemViewModel.FilesAndFolders;
                        var matchingItem = binItems.FirstOrDefault(x => x.ItemPath == src.Path); // Get original file name
                        incomingItems.Add(new FilesystemItemsOperationItemModel(FilesystemOperationType.Delete, src.Path, null, matchingItem?.ItemName));
                    }
                    else
                    {
                        incomingItems.Add(new FilesystemItemsOperationItemModel(FilesystemOperationType.Delete, src.Path, null));
                    }
                }

                var dialog = FilesystemOperationDialogViewModel.GetDialog(new FilesystemItemsOperationDataModel(
                                                                              FilesystemOperationType.Delete,
                                                                              false,
                                                                              canBeSentToBin ? permanently : true,
                                                                              canBeSentToBin,
                                                                              incomingItems,
                                                                              new List <FilesystemItemsOperationItemModel>()));

                if (await dialog.TryShowAsync() != DialogResult.Primary)
                {
                    return(ReturnResult.Cancelled); // Return if the result isn't delete
                }

                // Delete selected items if the result is Yes
                permanently = dialog.ViewModel.PermanentlyDelete;
            }

            // post the status banner
            var banner = PostBannerHelpers.PostBanner_Delete(source, returnStatus, permanently, false, 0);

            banner.ErrorCode.ProgressChanged += (s, e) => returnStatus = e.ToStatus();

            var token = banner.CancellationToken;

            var sw = new Stopwatch();

            sw.Start();

            IStorageHistory history = await filesystemOperations.DeleteItemsAsync((IList <IStorageItemWithPath>) source, banner.Progress, banner.ErrorCode, permanently, token);

            ((IProgress <float>)banner.Progress).Report(100.0f);
            await Task.Yield();

            if (!permanently && registerHistory)
            {
                App.HistoryWrapper.AddHistory(history);
            }
            var itemsDeleted = history?.Source.Count() ?? 0;

            source.ForEach(x => App.JumpList.RemoveFolder(x.Path)); // Remove items from jump list

            banner.Remove();
            sw.Stop();

            PostBannerHelpers.PostBanner_Delete(source, returnStatus, permanently, token.IsCancellationRequested, itemsDeleted);

            return(returnStatus);
        }