|
17 | 17 | using Windows.Storage.Search;
|
18 | 18 | using TreeView = Microsoft.UI.Xaml.Controls.TreeView;
|
19 | 19 | using Windows.UI.Core;
|
| 20 | +using Microsoft.Toolkit.Collections; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using System.Linq; |
20 | 23 |
|
21 | 24 | namespace Files.Filesystem
|
22 | 25 | {
|
23 |
| - public class ItemViewModel |
| 26 | + public class ItemViewModel : IIncrementalSource<ListedItem> |
24 | 27 | {
|
25 | 28 | public ObservableCollection<Classic_ListedFolderItem> ClassicFolderList { get; set; } = new ObservableCollection<Classic_ListedFolderItem>();
|
26 | 29 | public ObservableCollection<ListedItem> ClassicFileList { get; set; } = new ObservableCollection<ListedItem>();
|
@@ -62,6 +65,224 @@ private async void DisplayConsentDialog()
|
62 | 65 | {
|
63 | 66 | await MainPage.permissionBox.ShowAsync();
|
64 | 67 | }
|
| 68 | + |
| 69 | + private List<ListedItem> ListedItems = new List<ListedItem>(); |
| 70 | + |
| 71 | + public async void GetFilesystemItemsAsync(string path, Page passedPage) |
| 72 | + { |
| 73 | + TextState.isVisible = Visibility.Collapsed; |
| 74 | + tokenSource = new CancellationTokenSource(); |
| 75 | + CancellationToken token = App.ViewModel.tokenSource.Token; |
| 76 | + pageName = passedPage.Name; |
| 77 | + Universal.path = path; |
| 78 | + // Personalize retrieved items for view they are displayed in |
| 79 | + switch (pageName) |
| 80 | + { |
| 81 | + case "GenericItemView": |
| 82 | + isPhotoAlbumMode = false; |
| 83 | + break; |
| 84 | + case "PhotoAlbumViewer": |
| 85 | + isPhotoAlbumMode = true; |
| 86 | + break; |
| 87 | + } |
| 88 | + FilesAndFolders.Clear(); |
| 89 | + Stopwatch stopwatch = new Stopwatch(); |
| 90 | + stopwatch.Start(); |
| 91 | + Universal.path = path; // Set visible path to reflect new navigation |
| 92 | + try |
| 93 | + { |
| 94 | + |
| 95 | + PVIS.isVisible = Visibility.Visible; |
| 96 | + TextState.isVisible = Visibility.Collapsed; |
| 97 | + folder = await StorageFolder.GetFolderFromPathAsync(path); |
| 98 | + QueryOptions options = new QueryOptions() |
| 99 | + { |
| 100 | + FolderDepth = FolderDepth.Shallow, |
| 101 | + IndexerOption = IndexerOption.UseIndexerWhenAvailable |
| 102 | + }; |
| 103 | + string sort = "By_Name"; |
| 104 | + if (sort == "By_Name") |
| 105 | + { |
| 106 | + SortEntry entry = new SortEntry() |
| 107 | + { |
| 108 | + AscendingOrder = true, |
| 109 | + PropertyName = "System.FileName" |
| 110 | + }; |
| 111 | + options.SortOrder.Add(entry); |
| 112 | + } |
| 113 | + |
| 114 | + uint index = 0; |
| 115 | + const uint step = 250; |
| 116 | + if (!folder.AreQueryOptionsSupported(options)) |
| 117 | + { |
| 118 | + options.SortOrder.Clear(); |
| 119 | + } |
| 120 | + |
| 121 | + folderQueryResult = folder.CreateFolderQueryWithOptions(options); |
| 122 | + IReadOnlyList<StorageFolder> folders = await folderQueryResult.GetFoldersAsync(index, step); |
| 123 | + int foldersCountSnapshot = folders.Count; |
| 124 | + while (folders.Count != 0) |
| 125 | + { |
| 126 | + foreach (StorageFolder folder in folders) |
| 127 | + { |
| 128 | + if (token.IsCancellationRequested) |
| 129 | + { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + gotFolName = folder.Name.ToString(); |
| 134 | + gotFolDate = folder.DateCreated.ToString(); |
| 135 | + gotFolPath = folder.Path.ToString(); |
| 136 | + gotFolType = "Folder"; |
| 137 | + gotFolImg = Visibility.Visible; |
| 138 | + gotFileImgVis = Visibility.Collapsed; |
| 139 | + gotEmptyImgVis = Visibility.Collapsed; |
| 140 | + |
| 141 | + ListedItems.Add(new ListedItem() { EmptyImgVis = gotEmptyImgVis, ItemIndex = FilesAndFolders.Count, FileImg = null, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath }); |
| 142 | + } |
| 143 | + index += step; |
| 144 | + folders = await folderQueryResult.GetFoldersAsync(index, step); |
| 145 | + } |
| 146 | + |
| 147 | + index = 0; |
| 148 | + fileQueryResult = folder.CreateFileQueryWithOptions(options); |
| 149 | + IReadOnlyList<StorageFile> files = await fileQueryResult.GetFilesAsync(index, step); |
| 150 | + int filesCountSnapshot = files.Count; |
| 151 | + while (files.Count != 0) |
| 152 | + { |
| 153 | + foreach (StorageFile file in files) |
| 154 | + { |
| 155 | + if (token.IsCancellationRequested) |
| 156 | + { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + gotName = file.DisplayName.ToString(); |
| 161 | + gotDate = file.DateCreated.ToString(); // In the future, parse date to human readable format |
| 162 | + if (file.FileType.ToString() == ".exe") |
| 163 | + { |
| 164 | + gotType = "Executable"; |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + gotType = file.DisplayType; |
| 169 | + } |
| 170 | + gotPath = file.Path.ToString(); |
| 171 | + gotFolImg = Visibility.Collapsed; |
| 172 | + gotDotFileExtension = file.FileType; |
| 173 | + if (isPhotoAlbumMode == false) |
| 174 | + { |
| 175 | + const uint requestedSize = 20; |
| 176 | + const ThumbnailMode thumbnailMode = ThumbnailMode.ListView; |
| 177 | + const ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale; |
| 178 | + try |
| 179 | + { |
| 180 | + gotFileImg = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions); |
| 181 | + BitmapImage icon = new BitmapImage(); |
| 182 | + if (gotFileImg != null) |
| 183 | + { |
| 184 | + gotEmptyImgVis = Visibility.Collapsed; |
| 185 | + icon.SetSource(gotFileImg.CloneStream()); |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + gotEmptyImgVis = Visibility.Visible; |
| 190 | + } |
| 191 | + gotFileImgVis = Visibility.Visible; |
| 192 | + |
| 193 | + if (pageName == "ClassicModePage") |
| 194 | + { |
| 195 | + ClassicFileList.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); |
| 196 | + } |
| 197 | + else |
| 198 | + { |
| 199 | + FilesAndFolders.Add(new ListedItem() { DotFileExtension = gotDotFileExtension, EmptyImgVis = gotEmptyImgVis, FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); |
| 200 | + } |
| 201 | + } |
| 202 | + catch |
| 203 | + { |
| 204 | + // Silent catch here to avoid crash |
| 205 | + // TODO maybe some logging could be added in the future... |
| 206 | + } |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + const uint requestedSize = 275; |
| 211 | + const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView; |
| 212 | + const ThumbnailOptions thumbnailOptions = ThumbnailOptions.ResizeThumbnail; |
| 213 | + try |
| 214 | + { |
| 215 | + gotFileImg = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions); |
| 216 | + BitmapImage icon = new BitmapImage(); |
| 217 | + if (gotFileImg != null) |
| 218 | + { |
| 219 | + gotEmptyImgVis = Visibility.Collapsed; |
| 220 | + icon.SetSource(gotFileImg.CloneStream()); |
| 221 | + } |
| 222 | + else |
| 223 | + { |
| 224 | + gotEmptyImgVis = Visibility.Visible; |
| 225 | + } |
| 226 | + gotFileImgVis = Visibility.Visible; |
| 227 | + ListedItems.Add(new ListedItem() { DotFileExtension = gotDotFileExtension, EmptyImgVis = gotEmptyImgVis, FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); |
| 228 | + } |
| 229 | + catch |
| 230 | + { |
| 231 | + // Silent catch here to avoid crash |
| 232 | + // TODO maybe some logging could be added in the future... |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + |
| 237 | + } |
| 238 | + index += step; |
| 239 | + files = await fileQueryResult.GetFilesAsync(index, step); |
| 240 | + } |
| 241 | + if (foldersCountSnapshot + filesCountSnapshot == 0) |
| 242 | + { |
| 243 | + TextState.isVisible = Visibility.Visible; |
| 244 | + } |
| 245 | + PVIS.isVisible = Visibility.Collapsed; |
| 246 | + stopwatch.Stop(); |
| 247 | + Debug.WriteLine("Loading of: " + path + " completed in " + stopwatch.ElapsedMilliseconds + " Milliseconds."); |
| 248 | + } |
| 249 | + catch (UnauthorizedAccessException e) |
| 250 | + { |
| 251 | + if (path.Contains(@"C:\")) |
| 252 | + { |
| 253 | + DisplayConsentDialog(); |
| 254 | + } |
| 255 | + else |
| 256 | + { |
| 257 | + MessageDialog unsupportedDevice = new MessageDialog("This device may be unsupported. Please file an issue report in Settings - About containing what device we couldn't access. Technical information: " + e, "Unsupported Device"); |
| 258 | + await unsupportedDevice.ShowAsync(); |
| 259 | + } |
| 260 | + stopwatch.Stop(); |
| 261 | + Debug.WriteLine("Loading of: " + path + " failed in " + stopwatch.ElapsedMilliseconds + " Milliseconds."); |
| 262 | + } |
| 263 | + catch (COMException e) |
| 264 | + { |
| 265 | + stopwatch.Stop(); |
| 266 | + Debug.WriteLine("Loading of: " + path + " failed in " + stopwatch.ElapsedMilliseconds + " Milliseconds."); |
| 267 | + Frame rootFrame = Window.Current.Content as Frame; |
| 268 | + MessageDialog driveGone = new MessageDialog(e.Message, "Drive Unplugged"); |
| 269 | + await driveGone.ShowAsync(); |
| 270 | + rootFrame.Navigate(typeof(MainPage), new SuppressNavigationTransitionInfo()); |
| 271 | + } |
| 272 | + |
| 273 | + History.AddToHistory(Universal.path); |
| 274 | + |
| 275 | + if (History.HistoryList.Count == 1) |
| 276 | + { |
| 277 | + BS.isEnabled = false; |
| 278 | + } |
| 279 | + else if (History.HistoryList.Count > 1) |
| 280 | + { |
| 281 | + BS.isEnabled = true; |
| 282 | + } |
| 283 | + tokenSource = null; |
| 284 | + } |
| 285 | + |
65 | 286 | public async void MemoryFriendlyGetItemsAsync(string path, Page passedPage)
|
66 | 287 | {
|
67 | 288 | TextState.isVisible = Visibility.Collapsed;
|
@@ -322,5 +543,14 @@ public static async void FillTreeNode(object item, TreeView EntireControl)
|
322 | 543 |
|
323 | 544 | }
|
324 | 545 | }
|
| 546 | + |
| 547 | + public async Task<IEnumerable<ListedItem>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken)) |
| 548 | + { |
| 549 | + return await Task.Run<IEnumerable<ListedItem>>(() => |
| 550 | + { |
| 551 | + var result = (from li in ListedItems select li).Skip(pageSize * pageIndex).Take(pageSize); |
| 552 | + return result; |
| 553 | + }); |
| 554 | + } |
325 | 555 | }
|
326 | 556 | }
|
0 commit comments