Skip to content

Commit 7d0b052

Browse files
committed
Take Advantage of UWP MostRecentlyUsedList for Recent Items
1 parent f16859d commit 7d0b052

File tree

2 files changed

+55
-180
lines changed

2 files changed

+55
-180
lines changed

Files UWP/Interacts/Interaction.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,13 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
4343
if (index > -1)
4444
{
4545
var clickedOnItem = (type as GenericFileBrowser).instanceViewModel.FilesAndFolders[index];
46-
// Write location to recents file
47-
StorageFile RecentsFile = await YourHome.dataFolder.CreateFileAsync("recents.txt", CreationCollisionOption.OpenIfExists);
48-
var existingLines = (await FileIO.ReadLinesAsync(RecentsFile));
49-
if (existingLines != null && !existingLines.Contains(clickedOnItem.FilePath))
50-
{
51-
await FileIO.AppendTextAsync(RecentsFile, clickedOnItem.FilePath + "\n");
52-
}
46+
// Access MRU List
47+
var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
5348

5449
if (clickedOnItem.FileType == "Folder")
5550
{
51+
// Add location to MRU List
52+
mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath));
5653
var TabInstance = CurrentInstance;
5754
(type as GenericFileBrowser).instanceViewModel.Universal.path = clickedOnItem.FilePath;
5855
TabInstance.PathText.Text = clickedOnItem.FilePath;
@@ -111,11 +108,15 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
111108
}
112109
else if (clickedOnItem.FileType == "Application")
113110
{
111+
// Add location to MRU List
112+
mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath));
114113
await LaunchExe(clickedOnItem.FilePath);
115114
}
116115
else
117116
{
118117
StorageFile file = await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath);
118+
// Add location to MRU List
119+
mostRecentlyUsed.Add(file);
119120
var options = new LauncherOptions
120121
{
121122
DisplayApplicationPicker = false
@@ -132,16 +133,14 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
132133
if (index > -1)
133134
{
134135
var clickedOnItem = (type as PhotoAlbum).instanceViewModel.FilesAndFolders[index];
135-
// Write location to recents file
136-
StorageFile RecentsFile = await YourHome.dataFolder.CreateFileAsync("recents.txt", CreationCollisionOption.OpenIfExists);
137-
var existingLines = (await FileIO.ReadLinesAsync(RecentsFile));
138-
if (existingLines != null && !existingLines.Contains(clickedOnItem.FilePath))
139-
{
140-
await FileIO.AppendTextAsync(RecentsFile, clickedOnItem.FilePath + "\n");
141-
}
136+
// Access MRU List
137+
var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
142138

143139
if (clickedOnItem.FileType == "Folder")
144140
{
141+
// Add location to MRU List
142+
mostRecentlyUsed.Add(await StorageFolder.GetFolderFromPathAsync(clickedOnItem.FilePath));
143+
145144
var TabInstance = CurrentInstance;
146145
(type as PhotoAlbum).instanceViewModel.Universal.path = clickedOnItem.FilePath;
147146
TabInstance.PathText.Text = clickedOnItem.FilePath;
@@ -199,11 +198,15 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
199198
}
200199
else if (clickedOnItem.FileType == "Application")
201200
{
201+
// Add location to MRU List
202+
mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath));
202203
await LaunchExe(clickedOnItem.FilePath);
203204
}
204205
else
205206
{
206207
StorageFile file = await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath);
208+
// Add location to MRU List
209+
mostRecentlyUsed.Add(file);
207210
var options = new LauncherOptions
208211
{
209212
DisplayApplicationPicker = false

Files UWP/YourHome.xaml.cs

Lines changed: 38 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -166,196 +166,64 @@ private void Button_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEven
166166

167167
public async void PopulateRecentsList()
168168
{
169-
recentItemsCollection.Clear();
170-
dataFolder = Windows.Storage.ApplicationData.Current.LocalCacheFolder;
171-
RecentsFile = await dataFolder.CreateFileAsync("recents.txt", CreationCollisionOption.OpenIfExists);
169+
var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
172170
BitmapImage ItemImage = new BitmapImage();
173171
string ItemPath = null;
174172
string ItemName;
175173
Visibility ItemFolderImgVis;
176174
Visibility ItemEmptyImgVis;
177175
Visibility ItemFileIconVis;
178-
IList<string> lines = new List<string>();
179-
lines = await FileIO.ReadLinesAsync(RecentsFile);
180-
if (lines.Count == 0)
176+
if (mostRecentlyUsed.Entries.Count == 0)
181177
{
182178
Empty.Visibility = Visibility.Visible;
183179
}
184-
else if (lines.Count > 10)
180+
else
181+
{
182+
Empty.Visibility = Visibility.Collapsed;
183+
}
184+
foreach (Windows.Storage.AccessCache.AccessListEntry entry in mostRecentlyUsed.Entries)
185185
{
186+
string mruToken = entry.Token;
186187
try
187188
{
188-
for (int LineNum = 0; LineNum < 10; LineNum++)
189+
Windows.Storage.IStorageItem item = await mostRecentlyUsed.GetItemAsync(mruToken);
190+
if (item.IsOfType(StorageItemTypes.Folder))
189191
{
190-
lines.RemoveAt(0);
192+
ItemName = item.Name;
193+
ItemPath = item.Path;
194+
ItemFolderImgVis = Visibility.Visible;
195+
ItemEmptyImgVis = Visibility.Collapsed;
196+
ItemFileIconVis = Visibility.Collapsed;
197+
recentItemsCollection.Add(new RecentItem() { name = ItemName, path = ItemPath, EmptyImgVis = ItemEmptyImgVis, FolderImg = ItemFolderImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
191198
}
192-
193-
await FileIO.WriteLinesAsync(RecentsFile, lines);
194-
Empty.Visibility = Visibility.Collapsed;
195-
lines = await FileIO.ReadLinesAsync(RecentsFile);
196-
foreach (string s in lines)
199+
else if (item.IsOfType(StorageItemTypes.File))
197200
{
198-
try
201+
ItemName = item.Name;
202+
ItemPath = item.Path;
203+
ItemImage = new BitmapImage();
204+
StorageFile file = await StorageFile.GetFileFromPathAsync(ItemPath);
205+
var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
206+
if (thumbnail == null)
199207
{
200-
var item = await StorageFolder.GetFolderFromPathAsync(s);
201-
ItemName = item.DisplayName;
202-
ItemPath = item.Path;
203-
ItemFolderImgVis = Visibility.Visible;
204-
ItemEmptyImgVis = Visibility.Collapsed;
205-
ItemFileIconVis = Visibility.Collapsed;
206-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
207-
{
208-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
209-
}
210-
208+
ItemEmptyImgVis = Visibility.Visible;
211209
}
212-
catch (System.IO.FileNotFoundException)
213-
{
214-
IList<string> modifyLines = new List<string>();
215-
modifyLines = lines;
216-
modifyLines.Remove(s);
217-
await FileIO.WriteLinesAsync(RecentsFile, modifyLines);
218-
PopulateRecentsList();
219-
}
220-
catch (UnauthorizedAccessException)
221-
{
222-
Empty.Visibility = Visibility.Visible;
223-
}
224-
catch (System.ArgumentException)
225-
{
226-
var item = await StorageFile.GetFileFromPathAsync(s);
227-
ItemName = item.DisplayName;
228-
ItemPath = item.Path;
229-
ItemImage = new BitmapImage();
230-
var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
231-
if (thumbnail == null)
232-
{
233-
ItemEmptyImgVis = Visibility.Visible;
234-
}
235-
else
236-
{
237-
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
238-
ItemEmptyImgVis = Visibility.Collapsed;
239-
}
240-
ItemFolderImgVis = Visibility.Collapsed;
241-
ItemFileIconVis = Visibility.Visible;
242-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
243-
{
244-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
245-
}
246-
}
247-
}
248-
}
249-
catch (System.IO.FileNotFoundException)
250-
{
251-
if (ItemPath != null)
252-
{
253-
RemoveDeletedItemFromList(ItemPath, lines);
254-
}
255-
else
256-
{
257-
Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set.");
258-
}
259-
}
260-
catch (COMException)
261-
{
262-
if (ItemPath != null)
263-
{
264-
RemoveDeletedItemFromList(ItemPath, lines);
265-
}
266-
else
267-
{
268-
Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set.");
269-
}
270-
}
271-
}
272-
else
273-
{
274-
try
275-
{
276-
Empty.Visibility = Visibility.Collapsed;
277-
278-
foreach (string s in lines)
279-
{
280-
try
210+
else
281211
{
282-
ItemPath = s;
283-
var item = await StorageFolder.GetFolderFromPathAsync(s);
284-
ItemName = item.DisplayName;
285-
ItemPath = item.Path;
286-
ItemFolderImgVis = Visibility.Visible;
212+
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
287213
ItemEmptyImgVis = Visibility.Collapsed;
288-
ItemFileIconVis = Visibility.Collapsed;
289-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
290-
{
291-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
292-
}
293-
294-
}
295-
catch (UnauthorizedAccessException)
296-
{
297-
Empty.Visibility = Visibility.Visible;
298-
}
299-
catch (System.ArgumentException)
300-
{
301-
var item = await StorageFile.GetFileFromPathAsync(s);
302-
ItemName = item.DisplayName;
303-
ItemPath = item.Path;
304-
ItemImage = new BitmapImage();
305-
var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
306-
if (thumbnail == null)
307-
{
308-
ItemEmptyImgVis = Visibility.Visible;
309-
}
310-
else
311-
{
312-
await ItemImage.SetSourceAsync(thumbnail.CloneStream());
313-
ItemEmptyImgVis = Visibility.Collapsed;
314-
}
315-
ItemFolderImgVis = Visibility.Collapsed;
316-
ItemFileIconVis = Visibility.Visible;
317-
if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }))
318-
{
319-
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
320-
}
321214
}
215+
ItemFolderImgVis = Visibility.Collapsed;
216+
ItemFileIconVis = Visibility.Visible;
217+
recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis });
322218
}
323219
}
324220
catch (System.IO.FileNotFoundException)
325221
{
326-
if(ItemPath != null)
327-
{
328-
RemoveDeletedItemFromList(ItemPath, lines);
329-
}
330-
else
331-
{
332-
Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set.");
333-
}
222+
mostRecentlyUsed.Remove(mruToken);
334223
}
335-
catch (COMException)
336-
{
337-
if (ItemPath != null)
338-
{
339-
RemoveDeletedItemFromList(ItemPath, lines);
340-
}
341-
else
342-
{
343-
Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set.");
344-
}
345-
}
346-
347224
}
348225
}
349226

350-
private async void RemoveDeletedItemFromList(string s, IList<string> lines)
351-
{
352-
IList<string> modifyLines = new List<string>();
353-
modifyLines = lines;
354-
modifyLines.Remove(s);
355-
await FileIO.WriteLinesAsync(RecentsFile, modifyLines);
356-
PopulateRecentsList();
357-
}
358-
359227
private async void RecentsView_ItemClick(object sender, ItemClickEventArgs e)
360228
{
361229
var path = (e.ClickedItem as RecentItem).path;
@@ -376,6 +244,10 @@ private async void RecentsView_ItemClick(object sender, ItemClickEventArgs e)
376244
await Launcher.LaunchFileAsync(file, options);
377245
}
378246
}
247+
catch (UnauthorizedAccessException)
248+
{
249+
await GetCurrentSelectedTabInstance<ProHome>().permissionBox.ShowAsync();
250+
}
379251
catch (System.ArgumentException)
380252
{
381253
GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Navigate(typeof(GenericFileBrowser), path);
@@ -387,13 +259,13 @@ private void RecentsView_RightTapped(object sender, Windows.UI.Xaml.Input.RightT
387259

388260
}
389261

390-
private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
262+
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
391263
{
392264
recentItemsCollection.Clear();
393265
RecentsView.ItemsSource = null;
394-
await RecentsFile.DeleteAsync();
395-
GetCurrentSelectedTabInstance<ProHome>().accessibleContentFrame.Navigate(typeof(YourHome), null, new Windows.UI.Xaml.Media.Animation.SuppressNavigationTransitionInfo());
396-
266+
var mru = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
267+
mru.Clear();
268+
Empty.Visibility = Visibility.Visible;
397269
}
398270

399271
private void DropShadowPanel_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)

0 commit comments

Comments
 (0)