Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Commit d521376

Browse files
authored
Merge pull request #2093 from xamarin/fix-2068
Make UWP FilePicker FutureAccessList more robust
2 parents 8ee188f + 3042aef commit d521376

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

Xamarin.Essentials/FilePicker/FilePicker.uwp.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Windows.Storage;
@@ -35,12 +36,51 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options
3536
resultList.Add(file);
3637
}
3738

38-
foreach (var file in resultList)
39-
StorageApplicationPermissions.FutureAccessList.Add(file);
39+
AddToAndCleanUpFutureAccessList(resultList);
4040

4141
return resultList.Select(storageFile => new FileResult(storageFile));
4242
}
4343

44+
static void AddToAndCleanUpFutureAccessList(List<StorageFile> pickedFiles)
45+
{
46+
var fal = StorageApplicationPermissions.FutureAccessList;
47+
48+
try
49+
{
50+
// Check if (FutureAccessList current items + new picked files) is exceeding maximum items
51+
if ((fal.Entries.Count + pickedFiles.Count) >= fal.MaximumItemsAllowed)
52+
{
53+
// We assume that the FutureAccessList items are saved in order, there is no metadata
54+
// Therefore we start removing from the bottom of the list
55+
var removeCount = Math.Min(fal.Entries.Count, pickedFiles.Count);
56+
var falTokens = fal.Entries.TakeLast(removeCount).ToList();
57+
58+
foreach (var entry in falTokens)
59+
{
60+
fal.Remove(entry.Token);
61+
}
62+
}
63+
64+
// Check if the picked file count doesn't exceed the maximum, else take the last n number or picked files
65+
var pickedFilesLimitedToMax = pickedFiles;
66+
if (pickedFiles.Count > StorageApplicationPermissions.FutureAccessList.MaximumItemsAllowed)
67+
{
68+
pickedFilesLimitedToMax =
69+
pickedFilesLimitedToMax.TakeLast((int)fal.MaximumItemsAllowed).ToList();
70+
}
71+
72+
foreach (var file in pickedFilesLimitedToMax)
73+
{
74+
StorageApplicationPermissions.FutureAccessList.Add(file);
75+
}
76+
}
77+
catch (Exception ex)
78+
{
79+
// Just log, we don't want to break on this
80+
Debug.WriteLine($"Error adding to/cleaning up FutureAccessList: {ex.Message}");
81+
}
82+
}
83+
4484
static void SetFileTypes(PickOptions options, FileOpenPicker picker)
4585
{
4686
var hasAtLeastOneType = false;

0 commit comments

Comments
 (0)