Skip to content

Commit 79a95e5

Browse files
committed
Squash Recents list FileNotFound crashing bug
1 parent b2328c0 commit 79a95e5

File tree

3 files changed

+131
-71
lines changed

3 files changed

+131
-71
lines changed

Files UWP/Filesystem/ItemViewModel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ public void CancelLoadAndClearFiles()
8080
_filesAndFolders.Clear();
8181

8282
//_folderQueryResult.ContentsChanged -= FolderContentsChanged;
83-
_fileQueryResult.ContentsChanged -= FileContentsChanged;
83+
if(_fileQueryResult != null)
84+
{
85+
_fileQueryResult.ContentsChanged -= FileContentsChanged;
86+
}
8487
}
8588

86-
private async void DisplayConsentDialog()
89+
public static async void DisplayConsentDialog()
8790
{
8891
await MainPage.permissionBox.ShowAsync();
8992
}

Files UWP/Interacts/Interaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public static void AllView_RightTapped(object sender, RightTappedRoutedEventArgs
404404
// If user clicks on actual row
405405
else
406406
{
407-
var ObjectPressed = ((ObservableCollection<ListedItem>)dataGrid.ItemsSource)[RowPressed.GetIndex()];
407+
var ObjectPressed = ((ReadOnlyObservableCollection<ListedItem>)dataGrid.ItemsSource)[RowPressed.GetIndex()];
408408
dataGrid.SelectedItems.Add(ObjectPressed);
409409
GenericFileBrowser.context.ShowAt(dataGrid, e.GetPosition(dataGrid));
410410
}

Files UWP/YourHome.xaml.cs

Lines changed: 125 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Windows.System;
1515
using Windows.UI.Xaml.Navigation;
1616
using System.ComponentModel;
17+
using System.Collections.Generic;
1718

1819
namespace Files
1920
{
@@ -199,114 +200,170 @@ public async void PopulateRecentsList()
199200
dataFolder = Windows.Storage.ApplicationData.Current.LocalCacheFolder;
200201
RecentsFile = await dataFolder.CreateFileAsync("recents.txt", CreationCollisionOption.OpenIfExists);
201202
BitmapImage ItemImage = new BitmapImage();
202-
string ItemPath;
203+
string ItemPath = null;
203204
string ItemName;
204205
Visibility ItemFolderImgVis;
205206
Visibility ItemEmptyImgVis;
206207
Visibility ItemFileIconVis;
207-
var lines = await FileIO.ReadLinesAsync(RecentsFile);
208+
IList<string> lines = new List<string>();
209+
lines = await FileIO.ReadLinesAsync(RecentsFile);
208210
if (lines.Count == 0)
209211
{
210212
Empty.Visibility = Visibility.Visible;
211213
}
212214
else if (lines.Count > 10)
213215
{
214-
for (int LineNum = 0; LineNum < 10; LineNum++)
216+
try
215217
{
216-
lines.RemoveAt(0);
217-
}
218+
for (int LineNum = 0; LineNum < 10; LineNum++)
219+
{
220+
lines.RemoveAt(0);
221+
}
218222

219-
await FileIO.WriteLinesAsync(RecentsFile, lines);
220-
Empty.Visibility = Visibility.Collapsed;
221-
lines = await FileIO.ReadLinesAsync(RecentsFile);
222-
foreach (string s in lines)
223-
{
224-
try
223+
await FileIO.WriteLinesAsync(RecentsFile, lines);
224+
Empty.Visibility = Visibility.Collapsed;
225+
lines = await FileIO.ReadLinesAsync(RecentsFile);
226+
foreach (string s in lines)
225227
{
226-
var item = await StorageFolder.GetFolderFromPathAsync(s);
227-
ItemName = item.DisplayName;
228-
ItemPath = item.Path;
229-
ItemFolderImgVis = Visibility.Visible;
230-
ItemEmptyImgVis = Visibility.Collapsed;
231-
ItemFileIconVis = Visibility.Collapsed;
232-
if(!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
228+
try
233229
{
234-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
235-
}
230+
var item = await StorageFolder.GetFolderFromPathAsync(s);
231+
ItemName = item.DisplayName;
232+
ItemPath = item.Path;
233+
ItemFolderImgVis = Visibility.Visible;
234+
ItemEmptyImgVis = Visibility.Collapsed;
235+
ItemFileIconVis = Visibility.Collapsed;
236+
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
237+
{
238+
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
239+
}
236240

237-
}
238-
catch (System.ArgumentException)
239-
{
240-
var item = await StorageFile.GetFileFromPathAsync(s);
241-
ItemName = item.DisplayName;
242-
ItemPath = item.Path;
243-
ItemImage = new BitmapImage();
244-
var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
245-
if (thumbnail == null)
241+
}
242+
catch (System.IO.FileNotFoundException)
246243
{
247-
ItemEmptyImgVis = Visibility.Visible;
244+
IList<string> modifyLines = new List<string>();
245+
modifyLines = lines;
246+
modifyLines.Remove(s);
247+
await FileIO.WriteLinesAsync(RecentsFile, modifyLines);
248+
PopulateRecentsList();
248249
}
249-
else
250+
catch (UnauthorizedAccessException)
250251
{
251-
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
252-
ItemEmptyImgVis = Visibility.Collapsed;
252+
Empty.Visibility = Visibility.Visible;
253253
}
254-
ItemFolderImgVis = Visibility.Collapsed;
255-
ItemFileIconVis = Visibility.Visible;
256-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
254+
catch (System.ArgumentException)
257255
{
258-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
256+
var item = await StorageFile.GetFileFromPathAsync(s);
257+
ItemName = item.DisplayName;
258+
ItemPath = item.Path;
259+
ItemImage = new BitmapImage();
260+
var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
261+
if (thumbnail == null)
262+
{
263+
ItemEmptyImgVis = Visibility.Visible;
264+
}
265+
else
266+
{
267+
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
268+
ItemEmptyImgVis = Visibility.Collapsed;
269+
}
270+
ItemFolderImgVis = Visibility.Collapsed;
271+
ItemFileIconVis = Visibility.Visible;
272+
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
273+
{
274+
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
275+
}
259276
}
260277
}
261278
}
279+
catch (System.IO.FileNotFoundException)
280+
{
281+
if (ItemPath != null)
282+
{
283+
RemoveDeletedItemFromList(ItemPath, lines);
284+
}
285+
else
286+
{
287+
Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set.");
288+
}
289+
}
262290
}
263291
else
264292
{
265-
Empty.Visibility = Visibility.Collapsed;
266-
267-
foreach (string s in lines)
293+
try
268294
{
269-
try
270-
{
271-
var item = await StorageFolder.GetFolderFromPathAsync(s);
272-
ItemName = item.DisplayName;
273-
ItemPath = item.Path;
274-
ItemFolderImgVis = Visibility.Visible;
275-
ItemEmptyImgVis = Visibility.Collapsed;
276-
ItemFileIconVis = Visibility.Collapsed;
277-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
278-
{
279-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
280-
}
295+
Empty.Visibility = Visibility.Collapsed;
281296

282-
}
283-
catch (System.ArgumentException)
297+
foreach (string s in lines)
284298
{
285-
var item = await StorageFile.GetFileFromPathAsync(s);
286-
ItemName = item.DisplayName;
287-
ItemPath = item.Path;
288-
ItemImage = new BitmapImage();
289-
var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
290-
if (thumbnail == null)
299+
try
291300
{
292-
ItemEmptyImgVis = Visibility.Visible;
301+
ItemPath = s;
302+
var item = await StorageFolder.GetFolderFromPathAsync(s);
303+
ItemName = item.DisplayName;
304+
ItemPath = item.Path;
305+
ItemFolderImgVis = Visibility.Visible;
306+
ItemEmptyImgVis = Visibility.Collapsed;
307+
ItemFileIconVis = Visibility.Collapsed;
308+
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
309+
{
310+
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
311+
}
312+
293313
}
294-
else
314+
catch (UnauthorizedAccessException)
295315
{
296-
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
297-
ItemEmptyImgVis = Visibility.Collapsed;
316+
Empty.Visibility = Visibility.Visible;
298317
}
299-
ItemFolderImgVis = Visibility.Collapsed;
300-
ItemFileIconVis = Visibility.Visible;
301-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
318+
catch (System.ArgumentException)
302319
{
303-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
320+
var item = await StorageFile.GetFileFromPathAsync(s);
321+
ItemName = item.DisplayName;
322+
ItemPath = item.Path;
323+
ItemImage = new BitmapImage();
324+
var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
325+
if (thumbnail == null)
326+
{
327+
ItemEmptyImgVis = Visibility.Visible;
328+
}
329+
else
330+
{
331+
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
332+
ItemEmptyImgVis = Visibility.Collapsed;
333+
}
334+
ItemFolderImgVis = Visibility.Collapsed;
335+
ItemFileIconVis = Visibility.Visible;
336+
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
337+
{
338+
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
339+
}
304340
}
305341
}
306342
}
343+
catch (System.IO.FileNotFoundException)
344+
{
345+
if(ItemPath != null)
346+
{
347+
RemoveDeletedItemFromList(ItemPath, lines);
348+
}
349+
else
350+
{
351+
Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set.");
352+
}
353+
}
354+
307355
}
308356
}
309357

358+
private async void RemoveDeletedItemFromList(string s, IList<string> lines)
359+
{
360+
IList<string> modifyLines = new List<string>();
361+
modifyLines = lines;
362+
modifyLines.Remove(s);
363+
await FileIO.WriteLinesAsync(RecentsFile, modifyLines);
364+
PopulateRecentsList();
365+
}
366+
310367
private async void RecentsView_ItemClick(object sender, ItemClickEventArgs e)
311368
{
312369
var path = (e.ClickedItem as RecentItem).path;

0 commit comments

Comments
 (0)