11using AStar . Dev . File . App . Data ;
22using AStar . Dev . File . App . Services ;
3- using CommunityToolkit . Mvvm . ComponentModel ;
4- using CommunityToolkit . Mvvm . Input ;
53using Microsoft . EntityFrameworkCore ;
4+ using ReactiveUI ;
5+ using ReactiveUI . Fody . Helpers ;
66using System ;
77using System . Collections . ObjectModel ;
88using System . Linq ;
9+ using System . Reactive ;
910using System . Threading . Tasks ;
1011
1112namespace AStar . Dev . File . App . ViewModels ;
1213
13- public partial class DeletePendingViewModel : ViewModelBase
14+ public class DeletePendingViewModel : ViewModelBase
1415{
1516 private readonly IDbContextFactory < FileAppDbContext > _dbContextFactory ;
1617 private readonly IFileDeleteService _fileDeleteService ;
1718 private readonly IFileViewerService _fileViewerService ;
1819
19- [ ObservableProperty ]
20- private bool _isDeleting ;
21-
22- [ ObservableProperty ]
23- private int _pendingDeleteCount ;
24-
25- [ ObservableProperty ]
26- private string _statusMessage = string . Empty ;
20+ [ Reactive ] public bool IsDeleting { get ; set ; }
21+ [ Reactive ] public int PendingDeleteCount { get ; set ; }
22+ [ Reactive ] public string StatusMessage { get ; set ; } = string . Empty ;
2723
2824 public ObservableCollection < ScannedFileDisplayItem > PendingDeleteFiles { get ; } = [ ] ;
2925
3026 public event Action < ScannedFileDisplayItem > ? ViewFileRequested ;
3127
28+ public ReactiveCommand < ScannedFileDisplayItem ? , Unit > TogglePendingDeleteCommand { get ; }
29+ public ReactiveCommand < Unit , Unit > DeleteAllCommand { get ; }
30+ public ReactiveCommand < Unit , Unit > ClearMarkingsCommand { get ; }
31+ public ReactiveCommand < ScannedFileDisplayItem ? , Unit > ViewFileCommand { get ; }
32+
3233 public DeletePendingViewModel (
3334 IDbContextFactory < FileAppDbContext > dbContextFactory ,
3435 IFileDeleteService fileDeleteService ,
@@ -39,10 +40,18 @@ public DeletePendingViewModel(
3940 _fileViewerService = fileViewerService ;
4041 _fileViewerService . FileViewRequested += item => ViewFileRequested ? . Invoke ( item ) ;
4142
43+ TogglePendingDeleteCommand = ReactiveCommand . CreateFromTask < ScannedFileDisplayItem ? > ( TogglePendingDelete ) ;
44+
45+ var canDeleteAll = this . WhenAnyValue ( x => x . IsDeleting , x => x . PendingDeleteCount ,
46+ ( deleting , count ) => ! deleting && count > 0 ) ;
47+ DeleteAllCommand = ReactiveCommand . CreateFromTask ( DeleteAll , canDeleteAll ) ;
48+
49+ ClearMarkingsCommand = ReactiveCommand . CreateFromTask ( ClearMarkings ) ;
50+ ViewFileCommand = ReactiveCommand . CreateFromTask < ScannedFileDisplayItem ? > ( ViewFile ) ;
51+
4252 _ = LoadPendingFilesAsync ( ) ;
4353 }
4454
45- [ RelayCommand ]
4655 private async Task TogglePendingDelete ( ScannedFileDisplayItem ? item )
4756 {
4857 if ( item is null ) return ;
@@ -58,7 +67,6 @@ private async Task TogglePendingDelete(ScannedFileDisplayItem? item)
5867 await LoadPendingFilesAsync ( ) ;
5968 }
6069
61- [ RelayCommand ( CanExecute = nameof ( CanDeleteAll ) ) ]
6270 private async Task DeleteAll ( )
6371 {
6472 if ( PendingDeleteFiles . Count == 0 )
@@ -95,9 +103,6 @@ private async Task DeleteAll()
95103 }
96104 }
97105
98- private bool CanDeleteAll ( ) => ! IsDeleting && PendingDeleteFiles . Count > 0 ;
99-
100- [ RelayCommand ]
101106 private async Task ClearMarkings ( )
102107 {
103108 if ( PendingDeleteFiles . Count == 0 )
@@ -116,7 +121,6 @@ private async Task ClearMarkings()
116121 await LoadPendingFilesAsync ( ) ;
117122 }
118123
119- [ RelayCommand ]
120124 private async Task ViewFile ( ScannedFileDisplayItem ? item )
121125 {
122126 await _fileViewerService . ViewFileAsync ( item ) ;
@@ -137,7 +141,6 @@ private async Task LoadPendingFilesAsync()
137141 files . ForEach ( file => PendingDeleteFiles . Add ( new ScannedFileDisplayItem ( file ) ) ) ;
138142
139143 PendingDeleteCount = PendingDeleteFiles . Count ;
140- DeleteAllCommand . NotifyCanExecuteChanged ( ) ;
141144 }
142145 catch ( Exception ex )
143146 {
0 commit comments