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

Commit

Permalink
Merge pull request #2093 from xamarin/fix-2068
Browse files Browse the repository at this point in the history
Make UWP FilePicker FutureAccessList more robust
  • Loading branch information
jfversluis committed Aug 10, 2023
2 parents 8ee188f + 3042aef commit d521376
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions Xamarin.Essentials/FilePicker/FilePicker.uwp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
Expand Down Expand Up @@ -35,12 +36,51 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options
resultList.Add(file);
}

foreach (var file in resultList)
StorageApplicationPermissions.FutureAccessList.Add(file);
AddToAndCleanUpFutureAccessList(resultList);

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

static void AddToAndCleanUpFutureAccessList(List<StorageFile> pickedFiles)
{
var fal = StorageApplicationPermissions.FutureAccessList;

try
{
// Check if (FutureAccessList current items + new picked files) is exceeding maximum items
if ((fal.Entries.Count + pickedFiles.Count) >= fal.MaximumItemsAllowed)
{
// We assume that the FutureAccessList items are saved in order, there is no metadata
// Therefore we start removing from the bottom of the list
var removeCount = Math.Min(fal.Entries.Count, pickedFiles.Count);
var falTokens = fal.Entries.TakeLast(removeCount).ToList();

foreach (var entry in falTokens)
{
fal.Remove(entry.Token);
}
}

// Check if the picked file count doesn't exceed the maximum, else take the last n number or picked files
var pickedFilesLimitedToMax = pickedFiles;
if (pickedFiles.Count > StorageApplicationPermissions.FutureAccessList.MaximumItemsAllowed)
{
pickedFilesLimitedToMax =
pickedFilesLimitedToMax.TakeLast((int)fal.MaximumItemsAllowed).ToList();
}

foreach (var file in pickedFilesLimitedToMax)
{
StorageApplicationPermissions.FutureAccessList.Add(file);
}
}
catch (Exception ex)
{
// Just log, we don't want to break on this
Debug.WriteLine($"Error adding to/cleaning up FutureAccessList: {ex.Message}");
}
}

static void SetFileTypes(PickOptions options, FileOpenPicker picker)
{
var hasAtLeastOneType = false;
Expand Down

0 comments on commit d521376

Please sign in to comment.