@@ -22,24 +22,59 @@ namespace Files.Filesystem
22
22
{
23
23
public class ItemViewModel
24
24
{
25
- public ObservableCollection < Classic_ListedFolderItem > ClassicFolderList { get ; set ; } = new ObservableCollection < Classic_ListedFolderItem > ( ) ;
26
- public ObservableCollection < ListedItem > ClassicFileList { get ; set ; } = new ObservableCollection < ListedItem > ( ) ;
27
- public ObservableCollection < ListedItem > FilesAndFolders { get ; set ; } = new ObservableCollection < ListedItem > ( ) ;
28
- public ObservableCollection < Classic_ListedFolderItem > ChildrenList ;
29
- public ListedItem LI { get ; } = new ListedItem ( ) ;
25
+ public ReadOnlyObservableCollection < ListedItem > FilesAndFolders { get ; }
26
+ public ReadOnlyObservableCollection < Classic_ListedFolderItem > ClassicFolderList { get ; }
27
+ public ReadOnlyObservableCollection < ListedItem > ClassicFileList { get ; }
28
+
30
29
public UniversalPath Universal { get ; } = new UniversalPath ( ) ;
31
30
public EmptyFolderTextState TextState { get ; set ; } = new EmptyFolderTextState ( ) ;
32
31
public BackState BS { get ; set ; } = new BackState ( ) ;
33
32
public ForwardState FS { get ; set ; } = new ForwardState ( ) ;
34
33
public ProgressUIVisibility PVIS { get ; set ; } = new ProgressUIVisibility ( ) ;
35
- public CancellationTokenSource TokenSource { get ; private set ; }
34
+
35
+ private ObservableCollection < ListedItem > _filesAndFolders ;
36
+ private ObservableCollection < ListedItem > _classicFileList ;
37
+ private ObservableCollection < Classic_ListedFolderItem > _classicFolderList ;
36
38
37
39
private StorageFolderQueryResult _folderQueryResult ;
38
40
private StorageFileQueryResult _fileQueryResult ;
41
+ private CancellationTokenSource _cancellationTokenSource ;
42
+
43
+ private volatile bool _filesRefreshing ;
44
+ private volatile bool _foldersRefreshing ;
45
+
46
+ private const int _step = 250 ;
39
47
40
48
public ItemViewModel ( )
41
49
{
42
- TokenSource = new CancellationTokenSource ( ) ;
50
+ _filesAndFolders = new ObservableCollection < ListedItem > ( ) ;
51
+ _classicFileList = new ObservableCollection < ListedItem > ( ) ;
52
+ _classicFolderList = new ObservableCollection < Classic_ListedFolderItem > ( ) ;
53
+
54
+ FilesAndFolders = new ReadOnlyObservableCollection < ListedItem > ( _filesAndFolders ) ;
55
+ ClassicFileList = new ReadOnlyObservableCollection < ListedItem > ( _classicFileList ) ;
56
+ ClassicFolderList = new ReadOnlyObservableCollection < Classic_ListedFolderItem > ( _classicFolderList ) ;
57
+ }
58
+
59
+ public void AddFileOrFolder ( ListedItem item )
60
+ {
61
+ _filesAndFolders . Add ( item ) ;
62
+ }
63
+
64
+ public void RemoveFileOrFolder ( ListedItem item )
65
+ {
66
+ _filesAndFolders . Remove ( item ) ;
67
+ }
68
+
69
+ public void CancelLoadAndClearFiles ( )
70
+ {
71
+ if ( _cancellationTokenSource == null ) { return ; }
72
+
73
+ _cancellationTokenSource . Cancel ( ) ;
74
+ _filesAndFolders . Clear ( ) ;
75
+
76
+ _folderQueryResult . ContentsChanged -= FolderContentsChanged ;
77
+ _fileQueryResult . ContentsChanged -= FileContentsChanged ;
43
78
}
44
79
45
80
private async void DisplayConsentDialog ( )
@@ -49,17 +84,20 @@ private async void DisplayConsentDialog()
49
84
50
85
public async void AddItemsToCollectionAsync ( string path , Page currentPage )
51
86
{
52
- TokenSource . Cancel ( ) ;
53
- TokenSource = new CancellationTokenSource ( ) ;
54
- var tokenSourceCopy = TokenSource ;
87
+ CancelLoadAndClearFiles ( ) ;
88
+
89
+ _cancellationTokenSource = new CancellationTokenSource ( ) ;
90
+ var tokenSourceCopy = _cancellationTokenSource ;
55
91
56
92
TextState . isVisible = Visibility . Collapsed ;
57
-
93
+
58
94
var pageName = currentPage . Name ;
59
95
Universal . path = path ;
60
96
61
97
if ( ! pageName . Contains ( "Classic" ) )
62
- FilesAndFolders . Clear ( ) ;
98
+ {
99
+ _filesAndFolders . Clear ( ) ;
100
+ }
63
101
64
102
Stopwatch stopwatch = new Stopwatch ( ) ;
65
103
stopwatch . Start ( ) ;
@@ -151,34 +189,35 @@ public async void AddItemsToCollectionAsync(string path, Page currentPage)
151
189
}
152
190
153
191
uint index = 0 ;
154
- const uint step = 250 ;
155
192
_folderQueryResult = rootFolder . CreateFolderQueryWithOptions ( options ) ;
193
+ _folderQueryResult . ContentsChanged += FolderContentsChanged ;
156
194
uint NumFolItems = await _folderQueryResult . GetItemCountAsync ( ) ;
157
- IReadOnlyList < StorageFolder > storageFolders = await _folderQueryResult . GetFoldersAsync ( index , step ) ;
195
+ IReadOnlyList < StorageFolder > storageFolders = await _folderQueryResult . GetFoldersAsync ( index , _step ) ;
158
196
while ( storageFolders . Count > 0 )
159
197
{
160
198
foreach ( StorageFolder folder in storageFolders )
161
199
{
162
200
if ( tokenSourceCopy . IsCancellationRequested ) { return ; }
163
201
await AddFolder ( folder , pageName , tokenSourceCopy . Token ) ;
164
202
}
165
- index += step ;
166
- storageFolders = await _folderQueryResult . GetFoldersAsync ( index , step ) ;
203
+ index += _step ;
204
+ storageFolders = await _folderQueryResult . GetFoldersAsync ( index , _step ) ;
167
205
}
168
206
169
207
index = 0 ;
170
208
_fileQueryResult = rootFolder . CreateFileQueryWithOptions ( options ) ;
209
+ _fileQueryResult . ContentsChanged += FileContentsChanged ;
171
210
uint NumFileItems = await _fileQueryResult . GetItemCountAsync ( ) ;
172
- IReadOnlyList < StorageFile > storageFiles = await _fileQueryResult . GetFilesAsync ( index , step ) ;
211
+ IReadOnlyList < StorageFile > storageFiles = await _fileQueryResult . GetFilesAsync ( index , _step ) ;
173
212
while ( storageFiles . Count > 0 )
174
213
{
175
214
foreach ( StorageFile file in storageFiles )
176
215
{
177
216
if ( tokenSourceCopy . IsCancellationRequested ) { return ; }
178
217
await AddFile ( file , pageName , tokenSourceCopy . Token ) ;
179
218
}
180
- index += step ;
181
- storageFiles = await _fileQueryResult . GetFilesAsync ( index , step ) ;
219
+ index += _step ;
220
+ storageFiles = await _fileQueryResult . GetFilesAsync ( index , _step ) ;
182
221
}
183
222
if ( NumFolItems + NumFileItems == 0 )
184
223
{
@@ -405,7 +444,7 @@ private async Task AddFolder(StorageFolder folder, string pageName, Cancellation
405
444
{
406
445
if ( token . IsCancellationRequested ) { return ; }
407
446
408
- FilesAndFolders . Add ( new ListedItem ( )
447
+ _filesAndFolders . Add ( new ListedItem ( folder . FolderRelativeId )
409
448
{
410
449
FileName = folder . Name ,
411
450
FileDate = GetFriendlyDate ( basicProperties . ItemDate . LocalDateTime ) ,
@@ -422,7 +461,7 @@ private async Task AddFolder(StorageFolder folder, string pageName, Cancellation
422
461
{
423
462
if ( token . IsCancellationRequested ) { return ; }
424
463
425
- ClassicFolderList . Add ( new Classic_ListedFolderItem ( )
464
+ _classicFolderList . Add ( new Classic_ListedFolderItem ( )
426
465
{
427
466
FileName = folder . Name ,
428
467
FileDate = GetFriendlyDate ( basicProperties . ItemDate . LocalDateTime ) ,
@@ -504,7 +543,7 @@ private async Task AddFile(StorageFile file, string pageName, CancellationToken
504
543
{
505
544
if ( token . IsCancellationRequested ) { return ; }
506
545
507
- FilesAndFolders . Add ( new ListedItem ( )
546
+ _filesAndFolders . Add ( new ListedItem ( file . FolderRelativeId )
508
547
{
509
548
DotFileExtension = itemFileExtension ,
510
549
EmptyImgVis = itemEmptyImgVis ,
@@ -522,7 +561,7 @@ private async Task AddFile(StorageFile file, string pageName, CancellationToken
522
561
{
523
562
if ( token . IsCancellationRequested ) { return ; }
524
563
525
- ClassicFileList . Add ( new ListedItem ( )
564
+ _classicFileList . Add ( new ListedItem ( file . FolderRelativeId )
526
565
{
527
566
FileImg = icon ,
528
567
FileIconVis = itemThumbnailImgVis ,
@@ -534,5 +573,19 @@ private async Task AddFile(StorageFile file, string pageName, CancellationToken
534
573
} ) ;
535
574
}
536
575
}
576
+
577
+ private void FileContentsChanged ( IStorageQueryResultBase sender , object args )
578
+ {
579
+ if ( _filesRefreshing ) { return ; }
580
+
581
+ Debug . WriteLine ( "File changed " + sender . Folder . DisplayName ) ;
582
+ }
583
+
584
+ private void FolderContentsChanged ( IStorageQueryResultBase sender , object args )
585
+ {
586
+ if ( _foldersRefreshing ) { return ; }
587
+
588
+ Debug . WriteLine ( "Folder changed " + sender . Folder . DisplayName ) ;
589
+ }
537
590
}
538
591
}
0 commit comments