11using System ;
2- using Files ;
3- using Navigation ;
42using System . Collections . Generic ;
53using System . Collections . ObjectModel ;
64using System . Diagnostics ;
5+ using System . Globalization ;
6+ using System . Runtime . InteropServices ;
77using Windows . Storage ;
88using Windows . Storage . FileProperties ;
9- using Windows . UI . Xaml ;
10- using Windows . UI . Xaml . Media . Imaging ;
11- using Windows . Storage . Search ;
129using Windows . UI . Popups ;
13- using Interacts ;
10+ using Windows . UI . Xaml ;
1411using Windows . UI . Xaml . Controls ;
1512using Windows . UI . Xaml . Media . Animation ;
13+ using Windows . UI . Xaml . Media . Imaging ;
1614using System . Threading ;
17- using System . Threading . Tasks ;
15+ using Files . Interacts ;
16+ using Files . Navigation ;
17+ using Windows . Storage . Search ;
18+ using TreeView = Microsoft . UI . Xaml . Controls . TreeView ;
1819
19- namespace ItemListPresenter
20+ namespace Files . Filesystem
2021{
21- public class ListedItem
22- {
23- public Visibility FolderImg { get ; set ; }
24- public Visibility FileIconVis { get ; set ; }
25- public BitmapImage FileImg { get ; set ; }
26- public string FileName { get ; set ; }
27- public string FileDate { get ; set ; }
28- public string FileExtension { get ; set ; }
29- public string FilePath { get ; set ; }
30- public int ItemIndex { get ; set ; }
31- public ListedItem ( )
32- {
33-
34- }
35- }
36-
37- public class Classic_ListedFolderItem
38- {
39- public string FileName { get ; set ; }
40- public string FileDate { get ; set ; }
41- public string FileExtension { get ; set ; }
42- public string FilePath { get ; set ; }
43- public ObservableCollection < Classic_ListedFolderItem > Children { get ; set ; } = new ObservableCollection < Classic_ListedFolderItem > ( ) ;
44- }
45-
4622 public class ItemViewModel
4723 {
4824 public static ObservableCollection < Classic_ListedFolderItem > classicFolderList = new ObservableCollection < Classic_ListedFolderItem > ( ) ;
4925 public static ObservableCollection < Classic_ListedFolderItem > ClassicFolderList { get { return classicFolderList ; } }
50-
26+
5127 public static ObservableCollection < ListedItem > classicFileList = new ObservableCollection < ListedItem > ( ) ;
5228 public static ObservableCollection < ListedItem > ClassicFileList { get { return classicFileList ; } }
5329
@@ -82,10 +58,6 @@ public static BackState BS
8258 {
8359 return bs ;
8460 }
85- set
86- {
87-
88- }
8961 }
9062
9163 public static ForwardState fs = new ForwardState ( ) ;
@@ -95,10 +67,6 @@ public static ForwardState FS
9567 {
9668 return fs ;
9769 }
98- set
99- {
100-
101- }
10270 }
10371
10472 public static ProgressUIVisibility pvis = new ProgressUIVisibility ( ) ;
@@ -108,10 +76,6 @@ public static ProgressUIVisibility PVIS
10876 {
10977 return pvis ;
11078 }
111- set
112- {
113-
114- }
11579 }
11680
11781 private ListedItem li = new ListedItem ( ) ;
@@ -154,33 +118,33 @@ public static ProgressUIVisibility PVIS
154118 public static CancellationToken token ;
155119 public static CancellationTokenSource tokenSource ;
156120
157- public ItemViewModel ( string ViewPath , Page p )
121+ public ItemViewModel ( string viewPath , Page p )
158122 {
159-
123+
160124 pageName = p . Name ;
161125 // Personalize retrieved items for view they are displayed in
162- if ( p . Name == "GenericItemView" || p . Name == "ClassicModePage" )
126+ if ( p . Name == "GenericItemView" || p . Name == "ClassicModePage" )
163127 {
164128 isPhotoAlbumMode = false ;
165129 }
166130 else if ( p . Name == "PhotoAlbumViewer" )
167131 {
168132 isPhotoAlbumMode = true ;
169133 }
170-
171- if ( pageName != "ClassicModePage" )
134+
135+ if ( pageName != "ClassicModePage" )
172136 {
173- GenericFileBrowser . P . path = ViewPath ;
137+ GenericFileBrowser . P . path = viewPath ;
174138 FilesAndFolders . Clear ( ) ;
175139 }
176140
177141 tokenSource = new CancellationTokenSource ( ) ;
178142 token = tokenSource . Token ;
179- MemoryFriendlyGetItemsAsync ( ViewPath , token ) ;
143+ MemoryFriendlyGetItemsAsync ( viewPath , token ) ;
180144
181145 if ( pageName != "ClassicModePage" )
182146 {
183- History . AddToHistory ( ViewPath ) ;
147+ History . AddToHistory ( viewPath ) ;
184148
185149 if ( History . HistoryList . Count == 1 )
186150 {
@@ -195,15 +159,15 @@ public ItemViewModel(string ViewPath, Page p)
195159 //Debug.WriteLine("Enabled Property");
196160 }
197161 }
198-
162+
199163
200164 }
201165
202166 private async void DisplayConsentDialog ( )
203167 {
204168 MessageDialog message = new MessageDialog ( "This app is not able to access your files. You need to allow it to by granting permission in Settings." ) ;
205169 message . Title = "Permission Denied" ;
206- message . Commands . Add ( new UICommand ( "Allow..." , new UICommandInvokedHandler ( Interaction . GrantAccessPermissionHandler ) ) ) ;
170+ message . Commands . Add ( new UICommand ( "Allow..." , Interaction . GrantAccessPermissionHandler ) ) ;
207171 await message . ShowAsync ( ) ;
208172 }
209173 string sort = "By_Name" ;
@@ -248,13 +212,13 @@ public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken tok
248212 int foldersCountSnapshot = folders . Count ;
249213 while ( folders . Count != 0 )
250214 {
251- foreach ( StorageFolder folder in folders )
215+ foreach ( StorageFolder folder in folders )
252216 {
253217 if ( token . IsCancellationRequested )
254218 {
255219 return ;
256220 }
257-
221+
258222 gotFolName = folder . Name . ToString ( ) ;
259223 gotFolDate = folder . DateCreated . ToString ( ) ;
260224 gotFolPath = folder . Path . ToString ( ) ;
@@ -288,7 +252,7 @@ public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken tok
288252 {
289253 return ;
290254 }
291-
255+
292256 gotName = file . Name . ToString ( ) ;
293257 gotDate = file . DateCreated . ToString ( ) ; // In the future, parse date to human readable format
294258 if ( file . FileType . ToString ( ) == ".exe" )
@@ -306,7 +270,15 @@ public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken tok
306270 const uint requestedSize = 20 ;
307271 const ThumbnailMode thumbnailMode = ThumbnailMode . ListView ;
308272 const ThumbnailOptions thumbnailOptions = ThumbnailOptions . UseCurrentScale ;
309- gotFileImg = await file . GetThumbnailAsync ( thumbnailMode , requestedSize , thumbnailOptions ) ;
273+ try
274+ {
275+ gotFileImg = await file . GetThumbnailAsync ( thumbnailMode , requestedSize , thumbnailOptions ) ;
276+ }
277+ catch
278+ {
279+ // Silent catch here to avoid crash
280+ // TODO maybe some logging could be added in the future...
281+ }
310282 }
311283 else
312284 {
@@ -335,7 +307,7 @@ public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken tok
335307 index += step ;
336308 files = await fileQueryResult . GetFilesAsync ( index , step ) ;
337309 }
338- if ( foldersCountSnapshot + filesCountSnapshot == 0 )
310+ if ( foldersCountSnapshot + filesCountSnapshot == 0 )
339311 {
340312 TextState . isVisible = Visibility . Visible ;
341313 }
@@ -344,23 +316,36 @@ public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken tok
344316 PVIS . isVisible = Visibility . Collapsed ;
345317 }
346318 PVIS . isVisible = Visibility . Collapsed ;
347- }
348- catch ( UnauthorizedAccessException )
319+ stopwatch . Stop ( ) ;
320+ Debug . WriteLine ( "Loading of: " + path + " completed in " + stopwatch . ElapsedMilliseconds + " Milliseconds." ) ;
321+ }
322+ catch ( UnauthorizedAccessException e )
349323 {
350- DisplayConsentDialog ( ) ;
324+ if ( path . Contains ( @"C:\" ) )
325+ {
326+ DisplayConsentDialog ( ) ;
327+ }
328+ else
329+ {
330+ MessageDialog unsupportedDevice = new MessageDialog ( "This device is unsupported. Please file an issue report in Settings - About containing what device we couldn't access. Technical information: " + e , "Unsupported Device" ) ;
331+ await unsupportedDevice . ShowAsync ( ) ;
332+ }
333+ stopwatch . Stop ( ) ;
334+ Debug . WriteLine ( "Loading of: " + path + " failed in " + stopwatch . ElapsedMilliseconds + " Milliseconds." ) ;
351335 }
352- catch ( System . Runtime . InteropServices . COMException e )
336+ catch ( COMException e )
353337 {
338+ stopwatch . Stop ( ) ;
339+ Debug . WriteLine ( "Loading of: " + path + " failed in " + stopwatch . ElapsedMilliseconds + " Milliseconds." ) ;
354340 Frame rootFrame = Window . Current . Content as Frame ;
355341 MessageDialog driveGone = new MessageDialog ( e . Message , "Drive Unplugged" ) ;
356342 await driveGone . ShowAsync ( ) ;
357343 rootFrame . Navigate ( typeof ( MainPage ) , new SuppressNavigationTransitionInfo ( ) ) ;
358344 }
359- stopwatch . Stop ( ) ;
360- Debug . WriteLine ( "Loading of: " + path + " completed in " + stopwatch . ElapsedMilliseconds + " Milliseconds." ) ;
345+
361346 }
362-
363-
347+
348+
364349
365350 public static ProgressPercentage progressPER = new ProgressPercentage ( ) ;
366351
@@ -396,18 +381,18 @@ public static async void DisplayReviewUIWithArgs(string header, string subHeader
396381 await GenericFileBrowser . reviewBox . ShowAsync ( ) ;
397382 }
398383
399- public static async void FillTreeNode ( object item , Microsoft . UI . Xaml . Controls . TreeView EntireControl )
384+ public static async void FillTreeNode ( object item , TreeView EntireControl )
400385 {
401- var PathToFillFrom = ( item as Classic_ListedFolderItem ) . FilePath ;
402- StorageFolder FolderFromPath = await StorageFolder . GetFolderFromPathAsync ( PathToFillFrom ) ;
403- IReadOnlyList < StorageFolder > SubFolderList = await FolderFromPath . GetFoldersAsync ( ) ;
404- foreach ( StorageFolder fol in SubFolderList )
386+ var pathToFillFrom = ( item as Classic_ListedFolderItem ) ? . FilePath ;
387+ StorageFolder folderFromPath = await StorageFolder . GetFolderFromPathAsync ( pathToFillFrom ) ;
388+ IReadOnlyList < StorageFolder > SubFolderList = await folderFromPath . GetFoldersAsync ( ) ;
389+ foreach ( StorageFolder fol in SubFolderList )
405390 {
406391 var name = fol . Name ;
407- var date = fol . DateCreated . LocalDateTime . ToString ( ) ;
392+ var date = fol . DateCreated . LocalDateTime . ToString ( CultureInfo . InvariantCulture ) ;
408393 var ext = fol . DisplayType ;
409394 var path = fol . Path ;
410- ( item as Classic_ListedFolderItem ) . Children . Add ( new Classic_ListedFolderItem ( ) { FileName = name , FilePath = path , FileDate = date , FileExtension = ext } ) ;
395+ ( item as Classic_ListedFolderItem ) ? . Children . Add ( new Classic_ListedFolderItem ( ) { FileName = name , FilePath = path , FileDate = date , FileExtension = ext } ) ;
411396
412397 }
413398 }
0 commit comments