Skip to content

Commit a283cbe

Browse files
committed
Prevent a crash when inserting or removing flash drives
1 parent 4a07a5e commit a283cbe

File tree

6 files changed

+80
-31
lines changed

6 files changed

+80
-31
lines changed

Files UWP/Filesystem/ItemViewModel.cs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ItemViewModel<PageType> where PageType : class
3838
private ObservableCollection<Classic_ListedFolderItem> _classicFolderList;
3939

4040
private StorageFolderQueryResult _folderQueryResult;
41-
private StorageFileQueryResult _fileQueryResult;
41+
public StorageFileQueryResult _fileQueryResult;
4242
private CancellationTokenSource _cancellationTokenSource;
4343
private StorageFolder _rootFolder;
4444
private QueryOptions _options;
@@ -142,30 +142,35 @@ private void HomeItems_PropertyChanged(object sender, System.ComponentModel.Prop
142142
public void AddFileOrFolder(ListedItem item)
143143
{
144144
_filesAndFolders.Add(item);
145-
if (typeof(PageType) == typeof(GenericFileBrowser))
145+
if ((GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser) != null || (GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum) != null)
146146
{
147-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).emptyTextGFB.Visibility = Visibility.Collapsed;
148-
}
149-
else if (typeof(PageType) == typeof(PhotoAlbum))
150-
{
151-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).EmptyTextPA.Visibility = Visibility.Collapsed;
147+
if (typeof(PageType) == typeof(GenericFileBrowser))
148+
{
149+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).emptyTextGFB.Visibility = Visibility.Collapsed;
150+
}
151+
else if (typeof(PageType) == typeof(PhotoAlbum))
152+
{
153+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).EmptyTextPA.Visibility = Visibility.Collapsed;
154+
}
152155
}
153156

154-
155157
}
156158

157159
public void RemoveFileOrFolder(ListedItem item)
158160
{
159161
_filesAndFolders.Remove(item);
160162
if (_filesAndFolders.Count == 0)
161163
{
162-
if (typeof(PageType) == typeof(GenericFileBrowser))
164+
if ((GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser) != null || (GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum) != null)
163165
{
164-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).emptyTextGFB.Visibility = Visibility.Visible;
165-
}
166-
else if (typeof(PageType) == typeof(PhotoAlbum))
167-
{
168-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).EmptyTextPA.Visibility = Visibility.Visible;
166+
if (typeof(PageType) == typeof(GenericFileBrowser))
167+
{
168+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).emptyTextGFB.Visibility = Visibility.Visible;
169+
}
170+
else if (typeof(PageType) == typeof(PhotoAlbum))
171+
{
172+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).EmptyTextPA.Visibility = Visibility.Visible;
173+
}
169174
}
170175
}
171176
}
@@ -553,7 +558,7 @@ private async Task AddFile(StorageFile file, CancellationToken token)
553558
}
554559
}
555560

556-
private async void FileContentsChanged(IStorageQueryResultBase sender, object args)
561+
public async void FileContentsChanged(IStorageQueryResultBase sender, object args)
557562
{
558563
if (_filesRefreshing)
559564
{
@@ -568,13 +573,16 @@ private async void FileContentsChanged(IStorageQueryResultBase sender, object ar
568573
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
569574
() =>
570575
{
571-
if (typeof(PageType) == typeof(GenericFileBrowser))
576+
if((GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser) != null || (GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum) != null)
572577
{
573-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).progressBar.Visibility = Visibility.Visible;
574-
}
575-
else if (typeof(PageType) == typeof(PhotoAlbum))
576-
{
577-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).progressBar.Visibility = Visibility.Visible;
578+
if (typeof(PageType) == typeof(GenericFileBrowser))
579+
{
580+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).progressBar.Visibility = Visibility.Visible;
581+
}
582+
else if (typeof(PageType) == typeof(PhotoAlbum))
583+
{
584+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).progressBar.Visibility = Visibility.Visible;
585+
}
578586
}
579587
});
580588
_filesRefreshing = true;
@@ -631,13 +639,16 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
631639
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
632640
() =>
633641
{
634-
if (typeof(PageType) == typeof(GenericFileBrowser))
635-
{
636-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).progressBar.Visibility = Visibility.Collapsed;
637-
}
638-
else if (typeof(PageType) == typeof(PhotoAlbum))
642+
if ((GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser) != null || (GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum) != null)
639643
{
640-
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).progressBar.Visibility = Visibility.Collapsed;
644+
if (typeof(PageType) == typeof(GenericFileBrowser))
645+
{
646+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as GenericFileBrowser).progressBar.Visibility = Visibility.Collapsed;
647+
}
648+
else if (typeof(PageType) == typeof(PhotoAlbum))
649+
{
650+
(GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Content as PhotoAlbum).progressBar.Visibility = Visibility.Collapsed;
651+
}
641652
}
642653
});
643654

Files UWP/GenericFileBrowser.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
154154
}
155155
}
156156

157-
157+
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
158+
{
159+
base.OnNavigatingFrom(e);
160+
instanceViewModel._fileQueryResult.ContentsChanged -= instanceViewModel.FileContentsChanged;
161+
}
158162

159163

160164
private void AllView_DragOver(object sender, DragEventArgs e)

Files UWP/Interacts/Interaction.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
5050
{
5151
// Add location to MRU List
5252
mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath));
53+
5354
var TabInstance = CurrentInstance;
5455
(type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath;
5556
TabInstance.PathText.Text = clickedOnItem.FilePath;
@@ -101,7 +102,10 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
101102
}
102103
else
103104
{
104-
TabInstance.drivesList.SelectedIndex = 0;
105+
if (clickedOnItem.FilePath.Split(@"\")[0].Contains("C:"))
106+
{
107+
TabInstance.drivesList.SelectedIndex = 0;
108+
}
105109
(type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath;
106110
TabInstance.accessibleContentFrame.Navigate(typeof(GenericFileBrowser), (type as GenericFileBrowser).instanceViewModel.Universal.path, new SuppressNavigationTransitionInfo());
107111
}

Files UWP/PhotoAlbum.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
113113
}
114114
}
115115

116+
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
117+
{
118+
base.OnNavigatingFrom(e);
119+
instanceViewModel._fileQueryResult.ContentsChanged -= instanceViewModel.FileContentsChanged;
120+
}
121+
116122
private void Clipboard_ContentChanged(object sender, object e)
117123
{
118124
try

Files UWP/ProHome.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
785785

786786
private void ItemDisplayFrame_Navigating(object sender, Windows.UI.Xaml.Navigation.NavigatingCancelEventArgs e)
787787
{
788-
788+
789789
}
790790

791791
private void ItemDisplayFrame_Navigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)

Files UWP/YourHome.xaml.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
using System.ComponentModel;
1414
using System.Collections.Generic;
1515
using System.Runtime.InteropServices;
16+
using System.IO;
17+
using Windows.UI.Popups;
1618

1719
namespace Files
1820
{
@@ -250,7 +252,29 @@ private async void RecentsView_ItemClick(object sender, ItemClickEventArgs e)
250252
}
251253
catch (System.ArgumentException)
252254
{
253-
GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Navigate(typeof(GenericFileBrowser), path);
255+
if (new DirectoryInfo(path).Root.ToString().Contains(@"C:\"))
256+
{
257+
GetCurrentSelectedTabInstance<ProHome>().drivesList.SelectedIndex = 0;
258+
GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Navigate(typeof(GenericFileBrowser), path);
259+
}
260+
else
261+
{
262+
foreach(ListViewItem drive in GetCurrentSelectedTabInstance<ProHome>().drivesList.Items)
263+
{
264+
if (drive.Tag.ToString() == new DirectoryInfo(path).Root.ToString())
265+
{
266+
GetCurrentSelectedTabInstance<ProHome>().drivesList.SelectedItem = null;
267+
drive.IsSelected = true;
268+
GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Navigate(typeof(GenericFileBrowser), path);
269+
return;
270+
}
271+
}
272+
}
273+
}
274+
catch (COMException)
275+
{
276+
MessageDialog dialog = new MessageDialog("Please insert the necessary drive to access this item.", "Drive Unplugged");
277+
await dialog.ShowAsync();
254278
}
255279
}
256280

0 commit comments

Comments
 (0)