This repository was archived by the owner on Apr 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeletePendingViewModel.cs
More file actions
150 lines (123 loc) · 5.07 KB
/
Copy pathDeletePendingViewModel.cs
File metadata and controls
150 lines (123 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using AStar.Dev.File.App.Data;
using AStar.Dev.File.App.Services;
using Microsoft.EntityFrameworkCore;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Threading.Tasks;
namespace AStar.Dev.File.App.ViewModels;
public class DeletePendingViewModel : ViewModelBase
{
private readonly IDbContextFactory<FileAppDbContext> _dbContextFactory;
private readonly IFileDeleteService _fileDeleteService;
private readonly IFileViewerService _fileViewerService;
[Reactive] public bool IsDeleting { get; set; }
[Reactive] public int PendingDeleteCount { get; set; }
[Reactive] public string StatusMessage { get; set; } = string.Empty;
public ObservableCollection<ScannedFileDisplayItem> PendingDeleteFiles { get; } = [];
public event Action<ScannedFileDisplayItem>? ViewFileRequested;
public ReactiveCommand<ScannedFileDisplayItem?, Unit> TogglePendingDeleteCommand { get; }
public ReactiveCommand<Unit, Unit> DeleteAllCommand { get; }
public ReactiveCommand<Unit, Unit> ClearMarkingsCommand { get; }
public ReactiveCommand<ScannedFileDisplayItem?, Unit> ViewFileCommand { get; }
public DeletePendingViewModel(
IDbContextFactory<FileAppDbContext> dbContextFactory,
IFileDeleteService fileDeleteService,
IFileViewerService fileViewerService)
{
_dbContextFactory = dbContextFactory;
_fileDeleteService = fileDeleteService;
_fileViewerService = fileViewerService;
_fileViewerService.FileViewRequested += item => ViewFileRequested?.Invoke(item);
TogglePendingDeleteCommand = ReactiveCommand.CreateFromTask<ScannedFileDisplayItem?>(TogglePendingDelete);
var canDeleteAll = this.WhenAnyValue(x => x.IsDeleting, x => x.PendingDeleteCount,
(deleting, count) => !deleting && count > 0);
DeleteAllCommand = ReactiveCommand.CreateFromTask(DeleteAll, canDeleteAll);
ClearMarkingsCommand = ReactiveCommand.CreateFromTask(ClearMarkings);
ViewFileCommand = ReactiveCommand.CreateFromTask<ScannedFileDisplayItem?>(ViewFile);
_ = LoadPendingFilesAsync();
}
private async Task TogglePendingDelete(ScannedFileDisplayItem? item)
{
if (item is null) return;
await using var db = await _dbContextFactory.CreateDbContextAsync();
var file = await db.ScannedFiles.FindAsync(item.Id);
if (file is not null)
{
file.PendingDelete = !file.PendingDelete;
await db.SaveChangesAsync();
}
await LoadPendingFilesAsync();
}
private async Task DeleteAll()
{
if (PendingDeleteFiles.Count == 0)
return;
IsDeleting = true;
StatusMessage = "Deleting files...";
try
{
var filePaths = PendingDeleteFiles.Select(f => f.FullPath).ToList();
await _fileDeleteService.DeleteFilesAsync(filePaths, moveToRecycleBin: true);
await using var db = await _dbContextFactory.CreateDbContextAsync();
var ids = PendingDeleteFiles.Select(f => f.Id).ToList();
var filesToRemove = await db.ScannedFiles.Where(f => ids.Contains(f.Id)).ToListAsync();
foreach (var file in filesToRemove)
{
db.ScannedFiles.Remove(file);
}
await db.SaveChangesAsync();
StatusMessage = $"Successfully deleted {filePaths.Count} file(s) to recycle bin.";
await LoadPendingFilesAsync();
}
catch (Exception ex)
{
StatusMessage = $"Error deleting files: {ex.Message}";
}
finally
{
IsDeleting = false;
}
}
private async Task ClearMarkings()
{
if (PendingDeleteFiles.Count == 0)
return;
await using var db = await _dbContextFactory.CreateDbContextAsync();
var ids = PendingDeleteFiles.Select(f => f.Id).ToList();
var files = await db.ScannedFiles.Where(f => ids.Contains(f.Id)).ToListAsync();
foreach (var file in files)
{
file.PendingDelete = false;
}
await db.SaveChangesAsync();
StatusMessage = "All delete markings cleared.";
await LoadPendingFilesAsync();
}
private async Task ViewFile(ScannedFileDisplayItem? item)
{
await _fileViewerService.ViewFileAsync(item);
}
private async Task LoadPendingFilesAsync()
{
try
{
await using var db = await _dbContextFactory.CreateDbContextAsync();
var files = await db.ScannedFiles
.Where(f => f.PendingDelete)
.OrderBy(f => f.FolderPath)
.ThenBy(f => f.FileName)
.ToListAsync();
PendingDeleteFiles.Clear();
files.ForEach(file => PendingDeleteFiles.Add(new ScannedFileDisplayItem(file)));
PendingDeleteCount = PendingDeleteFiles.Count;
}
catch (Exception ex)
{
StatusMessage = $"Error loading pending files: {ex.Message}";
}
}
}