2828using Windows . ApplicationModel . Core ;
2929using Windows . UI . Core ;
3030using Windows . Storage . Search ;
31+ using Windows . UI . Xaml . Input ;
3132
3233namespace Files
3334{
3435 sealed partial class App : Application
3536 {
3637 public static ProHome selectedTabInstance { get ; set ; }
3738 DeviceWatcher watcher ;
39+ public static ObservableCollection < SidebarItem > sideBarItems = new ObservableCollection < SidebarItem > ( ) ;
40+
3841 public App ( )
3942 {
4043 this . InitializeComponent ( ) ;
@@ -61,12 +64,10 @@ public App()
6164 if ( localSettings . Values [ "theme" ] . ToString ( ) == "Light" )
6265 {
6366 SettingsPages . Personalization . TV . ThemeValue = ApplicationTheme . Light ;
64- //Debug.WriteLine("Theme Requested as Light");
6567 }
6668 else if ( localSettings . Values [ "theme" ] . ToString ( ) == "Dark" )
6769 {
6870 SettingsPages . Personalization . TV . ThemeValue = ApplicationTheme . Dark ;
69- //Debug.WriteLine("Theme Requested as Dark");
7071 }
7172 else
7273 {
@@ -75,19 +76,15 @@ public App()
7576 if ( color == Colors . White )
7677 {
7778 SettingsPages . Personalization . TV . ThemeValue = ApplicationTheme . Light ;
78- // Debug.WriteLine("Theme Requested as Default (Light)");
79-
8079 }
8180 else
8281 {
8382 SettingsPages . Personalization . TV . ThemeValue = ApplicationTheme . Dark ;
84- //Debug.WriteLine("Theme Requested as Default (Dark)");
8583 }
8684 }
8785 }
8886
8987 this . RequestedTheme = SettingsPages . Personalization . TV . ThemeValue ;
90- //Debug.WriteLine("!!Requested Theme!!" + RequestedTheme.ToString());
9188
9289 if ( localSettings . Values [ "FavoritesDisplayed_Start" ] == null )
9390 {
@@ -119,8 +116,7 @@ public App()
119116 localSettings . Values [ "DrivesDisplayed_NewTab" ] = false ;
120117 }
121118
122- //FindDrives();
123-
119+ PopulatePinnedSidebarItems ( ) ;
124120 }
125121
126122 public void PopulateDrivesListWithLocalDisks ( )
@@ -320,6 +316,151 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
320316 }
321317 }
322318
319+ public static List < string > LinesToRemoveFromFile = new List < string > ( ) ;
320+
321+ public async void PopulatePinnedSidebarItems ( )
322+ {
323+
324+ AddDefaultLocations ( ) ;
325+
326+ StorageFile ListFile ;
327+ StorageFolder cacheFolder = ApplicationData . Current . LocalCacheFolder ;
328+ try
329+ {
330+ ListFile = await cacheFolder . GetFileAsync ( "PinnedItems.txt" ) ;
331+ }
332+ catch ( FileNotFoundException )
333+ {
334+ ListFile = await cacheFolder . CreateFileAsync ( "PinnedItems.txt" ) ;
335+ }
336+
337+ if ( ListFile != null )
338+ {
339+ var ListFileLines = await FileIO . ReadLinesAsync ( ListFile ) ;
340+ foreach ( string locationPath in ListFileLines )
341+ {
342+ try
343+ {
344+ StorageFolder fol = await StorageFolder . GetFolderFromPathAsync ( locationPath ) ;
345+ var name = fol . DisplayName ;
346+ var content = name ;
347+ var icon = "\uE8B7 " ;
348+
349+ bool isDuplicate = false ;
350+ foreach ( SidebarItem sbi in sideBarItems )
351+ {
352+ if ( ! string . IsNullOrWhiteSpace ( sbi . Path ) && ! sbi . isDefaultLocation )
353+ {
354+ if ( sbi . Path . ToString ( ) == locationPath )
355+ {
356+ isDuplicate = true ;
357+
358+ }
359+ }
360+ }
361+
362+ if ( ! isDuplicate )
363+ {
364+ sideBarItems . Add ( new SidebarItem ( ) { isDefaultLocation = false , Text = name , IconGlyph = icon , Path = locationPath } ) ;
365+ }
366+ }
367+ catch ( UnauthorizedAccessException e )
368+ {
369+ Debug . WriteLine ( e . Message ) ;
370+ }
371+ catch ( FileNotFoundException e )
372+ {
373+ Debug . WriteLine ( "Pinned item was deleted and will be removed from the file lines list soon: " + e . Message ) ;
374+ LinesToRemoveFromFile . Add ( locationPath ) ;
375+ }
376+ catch ( System . Runtime . InteropServices . COMException e )
377+ {
378+ Debug . WriteLine ( "Pinned item's drive was ejected and will be removed from the file lines list soon: " + e . Message ) ;
379+ LinesToRemoveFromFile . Add ( locationPath ) ;
380+ }
381+ }
382+
383+ RemoveStaleSidebarItems ( ) ;
384+ }
385+ }
386+
387+ private void AddDefaultLocations ( )
388+ {
389+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Home" , IconGlyph = "\uE737 " , isDefaultLocation = true } ) ;
390+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Desktop" , IconGlyph = "\uE8FC " , isDefaultLocation = true } ) ;
391+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Downloads" , IconGlyph = "\uE896 " , isDefaultLocation = true } ) ;
392+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Documents" , IconGlyph = "\uE8A5 " , isDefaultLocation = true } ) ;
393+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Pictures" , IconGlyph = "\uEB9F " , isDefaultLocation = true } ) ;
394+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Music" , IconGlyph = "\uEC4F " , isDefaultLocation = true } ) ;
395+ sideBarItems . Add ( new SidebarItem ( ) { Text = "Videos" , IconGlyph = "\uE8B2 " , isDefaultLocation = true } ) ;
396+ }
397+
398+ public static async void RemoveStaleSidebarItems ( )
399+ {
400+ StorageFile ListFile ;
401+ StorageFolder cacheFolder = ApplicationData . Current . LocalCacheFolder ;
402+ try
403+ {
404+ ListFile = await cacheFolder . GetFileAsync ( "PinnedItems.txt" ) ;
405+ }
406+ catch ( FileNotFoundException )
407+ {
408+ ListFile = await cacheFolder . CreateFileAsync ( "PinnedItems.txt" ) ;
409+ }
410+
411+ if ( ListFile != null )
412+ {
413+ var ListFileLines = await FileIO . ReadLinesAsync ( ListFile ) ;
414+ foreach ( string path in LinesToRemoveFromFile )
415+ {
416+ ListFileLines . Remove ( path ) ;
417+ }
418+
419+ await FileIO . WriteLinesAsync ( ListFile , ListFileLines ) ;
420+ ListFileLines = await FileIO . ReadLinesAsync ( ListFile ) ;
421+
422+ // Remove unpinned items from sidebar
423+ var sideBarItems_Copy = sideBarItems . ToList ( ) ;
424+ foreach ( SidebarItem location in sideBarItems )
425+ {
426+ if ( ! location . isDefaultLocation )
427+ {
428+ if ( ! ListFileLines . Contains ( location . Path . ToString ( ) ) )
429+ {
430+ sideBarItems_Copy . Remove ( location ) ;
431+ }
432+ }
433+
434+ }
435+ sideBarItems . Clear ( ) ;
436+ foreach ( SidebarItem correctItem in sideBarItems_Copy )
437+ {
438+ sideBarItems . Add ( correctItem ) ;
439+ }
440+ LinesToRemoveFromFile . Clear ( ) ;
441+ }
442+ }
443+
444+ public static SidebarItem rightClickedItem ;
445+
446+ public static async void FlyoutItem_Click ( object sender , RoutedEventArgs e )
447+ {
448+ StorageFolder cacheFolder = ApplicationData . Current . LocalCacheFolder ;
449+ var ListFile = await cacheFolder . GetFileAsync ( "PinnedItems.txt" ) ;
450+ var ListFileLines = await FileIO . ReadLinesAsync ( ListFile ) ;
451+ foreach ( string path in ListFileLines )
452+ {
453+ if ( path == App . rightClickedItem . Path . ToString ( ) )
454+ {
455+ App . LinesToRemoveFromFile . Add ( path ) ;
456+ RemoveStaleSidebarItems ( ) ;
457+ return ;
458+ }
459+ }
460+ }
461+
462+
463+
323464 public static Windows . UI . Xaml . UnhandledExceptionEventArgs exceptionInfo { get ; set ; }
324465 public static string exceptionStackTrace { get ; set ; }
325466 public Dialogs . ExceptionDialog exceptionDialog ;
0 commit comments