Skip to content

Commit d6e1eec

Browse files
committed
further refactor to reduce publicly exposed stuff; hookup content change event
1 parent 3191f66 commit d6e1eec

File tree

9 files changed

+161
-101
lines changed

9 files changed

+161
-101
lines changed

Files UWP/AddItem.xaml.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ private async void ListView_ItemClick(object sender, ItemClickEventArgs e)
4242
var userInput = GenericFileBrowser.inputForRename;
4343
if (userInput != null)
4444
{
45-
await folderToCreateItem.CreateFolderAsync(userInput, CreationCollisionOption.FailIfExists);
46-
App.ViewModel.FilesAndFolders.Add(new ListedItem(){ FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Collapsed, FolderImg = Visibility.Visible, FileIconVis = Visibility.Collapsed, FileType = "Folder", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput) });
45+
var folder = await folderToCreateItem.CreateFolderAsync(userInput, CreationCollisionOption.FailIfExists);
46+
App.ViewModel.AddFileOrFolder(new ListedItem(folder.FolderRelativeId){ FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Collapsed, FolderImg = Visibility.Visible, FileIconVis = Visibility.Collapsed, FileType = "Folder", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput) });
4747
}
4848
}
4949
else if ((e.ClickedItem as AddListItem).Header == "Text Document")
@@ -52,8 +52,8 @@ private async void ListView_ItemClick(object sender, ItemClickEventArgs e)
5252
var userInput = GenericFileBrowser.inputForRename;
5353
if (userInput != null)
5454
{
55-
await folderToCreateItem.CreateFileAsync(userInput + ".txt", CreationCollisionOption.FailIfExists);
56-
App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "Text Document", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".txt"), DotFileExtension = ".txt" });
55+
var folder = await folderToCreateItem.CreateFileAsync(userInput + ".txt", CreationCollisionOption.FailIfExists);
56+
App.ViewModel.AddFileOrFolder(new ListedItem(folder.FolderRelativeId) { FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "Text Document", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".txt"), DotFileExtension = ".txt" });
5757
}
5858
}
5959
else if ((e.ClickedItem as AddListItem).Header == "Bitmap Image")
@@ -62,9 +62,8 @@ private async void ListView_ItemClick(object sender, ItemClickEventArgs e)
6262
var userInput = GenericFileBrowser.inputForRename;
6363
if (userInput != null)
6464
{
65-
await folderToCreateItem.CreateFileAsync(userInput + ".bmp", CreationCollisionOption.FailIfExists);
66-
App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "BMP File", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".bmp"), DotFileExtension = ".bmp" });
67-
65+
var folder = await folderToCreateItem.CreateFileAsync(userInput + ".bmp", CreationCollisionOption.FailIfExists);
66+
App.ViewModel.AddFileOrFolder(new ListedItem(folder.FolderRelativeId) { FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "BMP File", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".bmp"), DotFileExtension = ".bmp" });
6867
}
6968
}
7069
}

Files UWP/Filesystem/ItemViewModel.cs

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,59 @@ namespace Files.Filesystem
2222
{
2323
public class ItemViewModel
2424
{
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+
3029
public UniversalPath Universal { get; } = new UniversalPath();
3130
public EmptyFolderTextState TextState { get; set; } = new EmptyFolderTextState();
3231
public BackState BS { get; set; } = new BackState();
3332
public ForwardState FS { get; set; } = new ForwardState();
3433
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;
3638

3739
private StorageFolderQueryResult _folderQueryResult;
3840
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;
3947

4048
public ItemViewModel()
4149
{
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;
4378
}
4479

4580
private async void DisplayConsentDialog()
@@ -49,17 +84,20 @@ private async void DisplayConsentDialog()
4984

5085
public async void AddItemsToCollectionAsync(string path, Page currentPage)
5186
{
52-
TokenSource.Cancel();
53-
TokenSource = new CancellationTokenSource();
54-
var tokenSourceCopy = TokenSource;
87+
CancelLoadAndClearFiles();
88+
89+
_cancellationTokenSource = new CancellationTokenSource();
90+
var tokenSourceCopy = _cancellationTokenSource;
5591

5692
TextState.isVisible = Visibility.Collapsed;
57-
93+
5894
var pageName = currentPage.Name;
5995
Universal.path = path;
6096

6197
if (!pageName.Contains("Classic"))
62-
FilesAndFolders.Clear();
98+
{
99+
_filesAndFolders.Clear();
100+
}
63101

64102
Stopwatch stopwatch = new Stopwatch();
65103
stopwatch.Start();
@@ -151,34 +189,35 @@ public async void AddItemsToCollectionAsync(string path, Page currentPage)
151189
}
152190

153191
uint index = 0;
154-
const uint step = 250;
155192
_folderQueryResult = rootFolder.CreateFolderQueryWithOptions(options);
193+
_folderQueryResult.ContentsChanged += FolderContentsChanged;
156194
uint NumFolItems = await _folderQueryResult.GetItemCountAsync();
157-
IReadOnlyList<StorageFolder> storageFolders = await _folderQueryResult.GetFoldersAsync(index, step);
195+
IReadOnlyList<StorageFolder> storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step);
158196
while (storageFolders.Count > 0)
159197
{
160198
foreach (StorageFolder folder in storageFolders)
161199
{
162200
if (tokenSourceCopy.IsCancellationRequested) { return; }
163201
await AddFolder(folder, pageName, tokenSourceCopy.Token);
164202
}
165-
index += step;
166-
storageFolders = await _folderQueryResult.GetFoldersAsync(index, step);
203+
index += _step;
204+
storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step);
167205
}
168206

169207
index = 0;
170208
_fileQueryResult = rootFolder.CreateFileQueryWithOptions(options);
209+
_fileQueryResult.ContentsChanged += FileContentsChanged;
171210
uint NumFileItems = await _fileQueryResult.GetItemCountAsync();
172-
IReadOnlyList<StorageFile> storageFiles = await _fileQueryResult.GetFilesAsync(index, step);
211+
IReadOnlyList<StorageFile> storageFiles = await _fileQueryResult.GetFilesAsync(index, _step);
173212
while (storageFiles.Count > 0)
174213
{
175214
foreach (StorageFile file in storageFiles)
176215
{
177216
if (tokenSourceCopy.IsCancellationRequested) { return; }
178217
await AddFile(file, pageName, tokenSourceCopy.Token);
179218
}
180-
index += step;
181-
storageFiles = await _fileQueryResult.GetFilesAsync(index, step);
219+
index += _step;
220+
storageFiles = await _fileQueryResult.GetFilesAsync(index, _step);
182221
}
183222
if (NumFolItems + NumFileItems == 0)
184223
{
@@ -405,7 +444,7 @@ private async Task AddFolder(StorageFolder folder, string pageName, Cancellation
405444
{
406445
if (token.IsCancellationRequested) { return; }
407446

408-
FilesAndFolders.Add(new ListedItem()
447+
_filesAndFolders.Add(new ListedItem(folder.FolderRelativeId)
409448
{
410449
FileName = folder.Name,
411450
FileDate = GetFriendlyDate(basicProperties.ItemDate.LocalDateTime),
@@ -422,7 +461,7 @@ private async Task AddFolder(StorageFolder folder, string pageName, Cancellation
422461
{
423462
if (token.IsCancellationRequested) { return; }
424463

425-
ClassicFolderList.Add(new Classic_ListedFolderItem()
464+
_classicFolderList.Add(new Classic_ListedFolderItem()
426465
{
427466
FileName = folder.Name,
428467
FileDate = GetFriendlyDate(basicProperties.ItemDate.LocalDateTime),
@@ -504,7 +543,7 @@ private async Task AddFile(StorageFile file, string pageName, CancellationToken
504543
{
505544
if (token.IsCancellationRequested) { return; }
506545

507-
FilesAndFolders.Add(new ListedItem()
546+
_filesAndFolders.Add(new ListedItem(file.FolderRelativeId)
508547
{
509548
DotFileExtension = itemFileExtension,
510549
EmptyImgVis = itemEmptyImgVis,
@@ -522,7 +561,7 @@ private async Task AddFile(StorageFile file, string pageName, CancellationToken
522561
{
523562
if (token.IsCancellationRequested) { return; }
524563

525-
ClassicFileList.Add(new ListedItem()
564+
_classicFileList.Add(new ListedItem(file.FolderRelativeId)
526565
{
527566
FileImg = icon,
528567
FileIconVis = itemThumbnailImgVis,
@@ -534,5 +573,19 @@ private async Task AddFile(StorageFile file, string pageName, CancellationToken
534573
});
535574
}
536575
}
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+
}
537590
}
538591
}

Files UWP/Filesystem/ListedItem.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Files.Filesystem
55
{
66
public class ListedItem
77
{
8+
public string FolderRelativeId { get; }
9+
810
public Visibility FolderImg { get; set; }
911
public Visibility FileIconVis { get; set; }
1012
public Visibility EmptyImgVis { get; set; }
@@ -15,9 +17,10 @@ public class ListedItem
1517
public string DotFileExtension { get; set; }
1618
public string FilePath { get; set; }
1719
public string FileSize { get; set; }
18-
public ListedItem()
19-
{
2020

21+
public ListedItem(string folderRelativeId)
22+
{
23+
FolderRelativeId = folderRelativeId;
2124
}
2225
}
2326
}

Files UWP/GenericFileBrowser.xaml.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
112112
{
113113
base.OnNavigatedTo(eventArgs);
114114
var parameters = (string)eventArgs.Parameter;
115-
App.ViewModel.FilesAndFolders.Clear();
115+
App.ViewModel.CancelLoadAndClearFiles();
116116
App.ViewModel.Universal.path = parameters;
117117
App.ViewModel.AddItemsToCollectionAsync(App.ViewModel.Universal.path, GenericItemView);
118118
if (parameters.Equals(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)))
@@ -332,11 +332,7 @@ private async void VisiblePath_TextChanged(object sender, KeyRoutedEventArgs e)
332332
var CurrentInput = PathBox.Text;
333333
if (CurrentInput != App.ViewModel.Universal.path)
334334
{
335-
if (!App.ViewModel.TokenSource.IsCancellationRequested)
336-
{
337-
App.ViewModel.TokenSource.Cancel();
338-
App.ViewModel.FilesAndFolders.Clear();
339-
}
335+
App.ViewModel.CancelLoadAndClearFiles();
340336

341337
if (CurrentInput == "Home" || CurrentInput == "home")
342338
{

0 commit comments

Comments
 (0)