@@ -182,7 +182,7 @@ public ItemViewModel(string ViewPath, Page p)
182182
183183 tokenSource = new CancellationTokenSource ( ) ;
184184 token = tokenSource . Token ;
185- GetItemsAsync ( ViewPath , token ) ;
185+ MemoryFriendlyGetItemsAsync ( ViewPath , token ) ;
186186
187187 if ( pageName != "ClassicModePage" )
188188 {
@@ -212,53 +212,55 @@ private async void DisplayConsentDialog()
212212 message . Commands . Add ( new UICommand ( "Allow..." , new UICommandInvokedHandler ( Interaction . GrantAccessPermissionHandler ) ) ) ;
213213 await message . ShowAsync ( ) ;
214214 }
215-
216-
217- public async void GetItemsAsync ( string path , CancellationToken token )
215+ string sort = "By_Name" ;
216+ SortEntry entry ;
217+ public async void MemoryFriendlyGetItemsAsync ( string path , CancellationToken token )
218218 {
219219 Stopwatch stopwatch = new Stopwatch ( ) ;
220220 stopwatch . Start ( ) ;
221221
222222 PUIP . Path = path ;
223223 try
224224 {
225- folder = await StorageFolder . GetFolderFromPathAsync ( path ) ; // Set location to the current directory specified in path
226- folderList = await folder . GetFoldersAsync ( ) ; // Create a read-only list of all folders in location
227- if ( token . IsCancellationRequested == true )
225+ folder = await StorageFolder . GetFolderFromPathAsync ( path ) ;
226+ QueryOptions options = new QueryOptions ( )
228227 {
229- return ;
230- }
231- fileList = await folder . GetFilesAsync ( ) ; // Create a read-only list of all files in location
232- NumOfFolders = folderList . Count ; // How many folders are in the list
233- NumOfFiles = fileList . Count ; // How many files are in the list
234- NumOfItems = NumOfFiles + NumOfFolders ;
235- NumItemsRead = 0 ;
228+ FolderDepth = FolderDepth . Shallow ,
229+ IndexerOption = IndexerOption . UseIndexerWhenAvailable
230+ } ;
236231
237- if ( NumOfItems == 0 )
232+ if ( sort == "By_Name" )
238233 {
239- TextState . isVisible = Visibility . Visible ;
234+ entry = new SortEntry ( )
235+ {
236+ AscendingOrder = true ,
237+ PropertyName = "System.FileName"
238+ } ;
240239 }
240+ options . SortOrder . Add ( entry ) ;
241241
242- PUIH . Header = "Loading " + NumOfItems + " items" ;
243- ButtonText . buttonText = "Hide" ;
244-
245- if ( NumOfItems >= 250 )
242+ uint index = 0 ;
243+ const uint step = 250 ;
244+ if ( ! folder . AreQueryOptionsSupported ( options ) )
246245 {
247- PVIS . isVisible = Visibility . Visible ;
246+ options . SortOrder . Clear ( ) ;
248247 }
249- if ( NumOfFolders > 0 )
248+
249+ StorageFolderQueryResult folderQueryResult = folder . CreateFolderQueryWithOptions ( options ) ;
250+ IReadOnlyList < StorageFolder > folders = await folderQueryResult . GetFoldersAsync ( index , step ) ;
251+ while ( folders . Count != 0 )
250252 {
251- foreach ( StorageFolder fol in folderList )
253+ foreach ( StorageFolder folder in folders )
252254 {
253- if ( token . IsCancellationRequested == true )
255+ if ( token . IsCancellationRequested )
254256 {
255257 return ;
256258 }
257- int ProgressReported = ( NumItemsRead * 100 / NumOfItems ) ;
258- UpdateProgUI ( ProgressReported ) ;
259- gotFolName = fol . Name . ToString ( ) ;
260- gotFolDate = fol . DateCreated . ToString ( ) ;
261- gotFolPath = fol . Path . ToString ( ) ;
259+ // int ProgressReported = (NumItemsRead * 100 / NumOfItems);
260+ // UpdateProgUI(ProgressReported);
261+ gotFolName = folder . Name . ToString ( ) ;
262+ gotFolDate = folder . DateCreated . ToString ( ) ;
263+ gotFolPath = folder . Path . ToString ( ) ;
262264 gotFolType = "Folder" ;
263265 gotFolImg = Visibility . Visible ;
264266 gotFileImgVis = Visibility . Collapsed ;
@@ -272,48 +274,50 @@ public async void GetItemsAsync(string path, CancellationToken token)
272274 {
273275 FilesAndFolders . Add ( new ListedItem ( ) { ItemIndex = FilesAndFolders . Count , FileImg = null , FileIconVis = gotFileImgVis , FolderImg = gotFolImg , FileName = gotFolName , FileDate = gotFolDate , FileExtension = gotFolType , FilePath = gotFolPath } ) ;
274276 }
275-
276-
277- NumItemsRead ++ ;
278277 }
279-
278+ index += step ;
279+ folders = await folderQueryResult . GetFoldersAsync ( index , step ) ;
280280 }
281281
282- if ( NumOfFiles > 0 )
282+ index = 0 ;
283+ StorageFileQueryResult fileQueryResult = folder . CreateFileQueryWithOptions ( options ) ;
284+ IReadOnlyList < StorageFile > files = await fileQueryResult . GetFilesAsync ( index , step ) ;
285+
286+ while ( files . Count != 0 )
283287 {
284- foreach ( StorageFile f in fileList )
288+ foreach ( StorageFile file in files )
285289 {
286- if ( token . IsCancellationRequested == true )
290+ if ( token . IsCancellationRequested )
287291 {
288292 return ;
289293 }
290- int ProgressReported = ( NumItemsRead * 100 / NumOfItems ) ;
291- UpdateProgUI ( ProgressReported ) ;
292- gotName = f . Name . ToString ( ) ;
293- gotDate = f . DateCreated . ToString ( ) ; // In the future, parse date to human readable format
294- if ( f . FileType . ToString ( ) == ".exe" )
294+ // int ProgressReported = (NumItemsRead * 100 / NumOfItems);
295+ // UpdateProgUI(ProgressReported);
296+ gotName = file . Name . ToString ( ) ;
297+ gotDate = file . DateCreated . ToString ( ) ; // In the future, parse date to human readable format
298+ if ( file . FileType . ToString ( ) == ".exe" )
295299 {
296300 gotType = "Executable" ;
297301 }
298302 else
299303 {
300- gotType = f . DisplayType ;
304+ gotType = file . DisplayType ;
301305 }
302- gotPath = f . Path . ToString ( ) ;
306+ gotPath = file . Path . ToString ( ) ;
303307 gotFolImg = Visibility . Collapsed ;
304308 if ( isPhotoAlbumMode == false )
305309 {
306310 const uint requestedSize = 20 ;
307311 const ThumbnailMode thumbnailMode = ThumbnailMode . ListView ;
308312 const ThumbnailOptions thumbnailOptions = ThumbnailOptions . UseCurrentScale ;
309- gotFileImg = await f . GetThumbnailAsync ( thumbnailMode , requestedSize , thumbnailOptions ) ;
313+ gotFileImg = await file . GetThumbnailAsync ( thumbnailMode , requestedSize , thumbnailOptions ) ;
310314 }
311315 else
312316 {
313317 const uint requestedSize = 275 ;
314318 const ThumbnailMode thumbnailMode = ThumbnailMode . PicturesView ;
315319 const ThumbnailOptions thumbnailOptions = ThumbnailOptions . ResizeThumbnail ;
316- gotFileImg = await f . GetThumbnailAsync ( thumbnailMode , requestedSize , thumbnailOptions ) ;
320+ gotFileImg = await file . GetThumbnailAsync ( thumbnailMode , requestedSize , thumbnailOptions ) ;
317321 }
318322
319323 BitmapImage icon = new BitmapImage ( ) ;
@@ -331,18 +335,16 @@ public async void GetItemsAsync(string path, CancellationToken token)
331335 {
332336 FilesAndFolders . Add ( new ListedItem ( ) { FileImg = icon , FileIconVis = gotFileImgVis , FolderImg = gotFolImg , FileName = gotName , FileDate = gotDate , FileExtension = gotType , FilePath = gotPath } ) ;
333337 }
334- NumItemsRead ++ ;
335338 }
336-
337-
339+ index += step ;
340+ files = await fileQueryResult . GetFilesAsync ( index , step ) ;
338341 }
339342 if ( pageName != "ClassicModePage" )
340343 {
341344 PVIS . isVisible = Visibility . Collapsed ;
342345 }
343346
344-
345- }
347+ }
346348 catch ( UnauthorizedAccessException )
347349 {
348350 DisplayConsentDialog ( ) ;
@@ -356,8 +358,152 @@ public async void GetItemsAsync(string path, CancellationToken token)
356358 }
357359 stopwatch . Stop ( ) ;
358360 Debug . WriteLine ( "Loading of: " + path + " completed in " + stopwatch . ElapsedMilliseconds + " Milliseconds." ) ;
359-
360361 }
362+
363+ //public async void GetItemsAsync(string path, CancellationToken token)
364+ //{
365+ // Stopwatch stopwatch = new Stopwatch();
366+ // stopwatch.Start();
367+
368+ // PUIP.Path = path;
369+ // try
370+ // {
371+ // folder = await StorageFolder.GetFolderFromPathAsync(path); // Set location to the current directory specified in path
372+ // folderList = await folder.GetFoldersAsync(); // Create a read-only list of all folders in location
373+ // if (token.IsCancellationRequested == true)
374+ // {
375+ // return;
376+ // }
377+ // fileList = await folder.GetFilesAsync(); // Create a read-only list of all files in location
378+ // NumOfFolders = folderList.Count; // How many folders are in the list
379+ // NumOfFiles = fileList.Count; // How many files are in the list
380+ // NumOfItems = NumOfFiles + NumOfFolders;
381+ // NumItemsRead = 0;
382+
383+ // if (NumOfItems == 0)
384+ // {
385+ // TextState.isVisible = Visibility.Visible;
386+ // }
387+
388+ // PUIH.Header = "Loading " + NumOfItems + " items";
389+ // ButtonText.buttonText = "Hide";
390+
391+ // if (NumOfItems >= 250)
392+ // {
393+ // PVIS.isVisible = Visibility.Visible;
394+ // }
395+ // if (NumOfFolders > 0)
396+ // {
397+ // foreach (StorageFolder fol in folderList)
398+ // {
399+ // if (token.IsCancellationRequested == true)
400+ // {
401+ // return;
402+ // }
403+ // int ProgressReported = (NumItemsRead * 100 / NumOfItems);
404+ // UpdateProgUI(ProgressReported);
405+ // gotFolName = fol.Name.ToString();
406+ // gotFolDate = fol.DateCreated.ToString();
407+ // gotFolPath = fol.Path.ToString();
408+ // gotFolType = "Folder";
409+ // gotFolImg = Visibility.Visible;
410+ // gotFileImgVis = Visibility.Collapsed;
411+
412+
413+ // if (pageName == "ClassicModePage")
414+ // {
415+ // ClassicFolderList.Add(new Classic_ListedFolderItem() { FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath });
416+ // }
417+ // else
418+ // {
419+ // FilesAndFolders.Add(new ListedItem() { ItemIndex = FilesAndFolders.Count, FileImg = null, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath });
420+ // }
421+
422+
423+ // NumItemsRead++;
424+ // }
425+
426+ // }
427+
428+ // if (NumOfFiles > 0)
429+ // {
430+ // foreach (StorageFile f in fileList)
431+ // {
432+ // if (token.IsCancellationRequested == true)
433+ // {
434+ // return;
435+ // }
436+ // int ProgressReported = (NumItemsRead * 100 / NumOfItems);
437+ // UpdateProgUI(ProgressReported);
438+ // gotName = f.Name.ToString();
439+ // gotDate = f.DateCreated.ToString(); // In the future, parse date to human readable format
440+ // if (f.FileType.ToString() == ".exe")
441+ // {
442+ // gotType = "Executable";
443+ // }
444+ // else
445+ // {
446+ // gotType = f.DisplayType;
447+ // }
448+ // gotPath = f.Path.ToString();
449+ // gotFolImg = Visibility.Collapsed;
450+ // if (isPhotoAlbumMode == false)
451+ // {
452+ // const uint requestedSize = 20;
453+ // const ThumbnailMode thumbnailMode = ThumbnailMode.ListView;
454+ // const ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;
455+ // gotFileImg = await f.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
456+ // }
457+ // else
458+ // {
459+ // const uint requestedSize = 275;
460+ // const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;
461+ // const ThumbnailOptions thumbnailOptions = ThumbnailOptions.ResizeThumbnail;
462+ // gotFileImg = await f.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
463+ // }
464+
465+ // BitmapImage icon = new BitmapImage();
466+ // if (gotFileImg != null)
467+ // {
468+ // icon.SetSource(gotFileImg.CloneStream());
469+ // }
470+ // gotFileImgVis = Visibility.Visible;
471+
472+ // if (pageName == "ClassicModePage")
473+ // {
474+ // ClassicFileList.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath });
475+ // }
476+ // else
477+ // {
478+ // FilesAndFolders.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath });
479+ // }
480+ // NumItemsRead++;
481+ // }
482+
483+
484+ // }
485+ // if (pageName != "ClassicModePage")
486+ // {
487+ // PVIS.isVisible = Visibility.Collapsed;
488+ // }
489+
490+
491+ // }
492+ // catch (UnauthorizedAccessException)
493+ // {
494+ // DisplayConsentDialog();
495+ // }
496+ // catch (System.Runtime.InteropServices.COMException e)
497+ // {
498+ // Frame rootFrame = Window.Current.Content as Frame;
499+ // MessageDialog driveGone = new MessageDialog(e.Message, "Drive Not Found");
500+ // await driveGone.ShowAsync();
501+ // rootFrame.Navigate(typeof(MainPage), new SuppressNavigationTransitionInfo());
502+ // }
503+ // stopwatch.Stop();
504+ // Debug.WriteLine("Loading of: " + path + " completed in " + stopwatch.ElapsedMilliseconds + " Milliseconds.");
505+
506+ //}
361507
362508 public static ProgressPercentage progressPER = new ProgressPercentage ( ) ;
363509
0 commit comments