|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Linq;
|
4 | 5 | using System.Threading.Tasks;
|
5 | 6 | using Windows.Storage;
|
@@ -35,12 +36,51 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options
|
35 | 36 | resultList.Add(file);
|
36 | 37 | }
|
37 | 38 |
|
38 |
| - foreach (var file in resultList) |
39 |
| - StorageApplicationPermissions.FutureAccessList.Add(file); |
| 39 | + AddToAndCleanUpFutureAccessList(resultList); |
40 | 40 |
|
41 | 41 | return resultList.Select(storageFile => new FileResult(storageFile));
|
42 | 42 | }
|
43 | 43 |
|
| 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 | + |
44 | 84 | static void SetFileTypes(PickOptions options, FileOpenPicker picker)
|
45 | 85 | {
|
46 | 86 | var hasAtLeastOneType = false;
|
|
0 commit comments