public async Task LoadFolderContents(int imageSize, IProgress <int> progress, ICollection <MediaData> mediaDatas = null) { if (!StorageApplicationPermissions.FutureAccessList.ContainsItem("gallery")) { return; } ImageSize = imageSize; _folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("gallery"); if (_folder != null) { // Time and log file query. using (new DisposableLogger(GalleryLog.FileQueryBegin, (sw) => GalleryLog.FileQueryEnd(sw, FilesFound))) { var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, FileTypes.Extensions) { FolderDepth = FolderDepth.Deep, IndexerOption = IndexerOption.UseIndexerWhenAvailable, }; // Sort results. queryOptions.SortOrder.Clear(); var sortEntry = QueryUtils.GetSortEntryFromSettings(); queryOptions.SortOrder.Add(sortEntry); // Prefetch thumbnails. queryOptions.SetThumbnailPrefetch(ThumbnailMode.SingleItem, (uint)imageSize, ThumbnailOptions.UseCurrentScale); queryOptions.SetPropertyPrefetch(PropertyPrefetchOptions.ImageProperties, new[] { "System.DateModified" }); // Create query. _query = _folder.CreateFileQueryWithOptions(queryOptions); // Register tracker. _query.ContentsChanged += Query_ContentsChanged; FilesFound = (int)await _query.GetItemCountAsync(); } // Time and log file parsing. using (new DisposableLogger(GalleryLog.FileParseBegin, (sw) => GalleryLog.FileParseEnd(sw, FilesFound))) { uint index = 0, stepSize = SettingsService.Instance.MediaLoadBatchSize; var files = await _query.GetFilesAsync(index, stepSize); index += stepSize; while (files.Count != 0) { var fileTask = _query.GetFilesAsync(index, stepSize).AsTask(); for (var i = 0; i < files.Count; i++) { var mediaFile = files[i]; // Don't bother with files not supported by MIME. if (!FileTypes.IsSupportedExtension(mediaFile.FileType)) { continue; } if (mediaDatas == null) { await AddFileAsync(imageSize, mediaFile, false); } else { // Find mediaData in mediaDatas based on path of mediaFile. var matchingMediaDatas = mediaDatas.Where(data => data?.Meta?.MediaFilePath?.Equals(mediaFile?.Path) ?? false).ToList(); var mediaData = matchingMediaDatas.FirstOrDefault(); if (mediaData is null) { // Track the lost media files. _lostMediaFiles.Add(mediaFile); continue; } if (mediaData.Meta is null) { continue; } if (string.IsNullOrEmpty(mediaData.Meta.MediaFilePath)) { continue; } // Tell the mediaData which file belongs to it. mediaData.MediaFile = mediaFile; await AddFileAsync(imageSize, mediaFile, false, mediaData); } } files = await fileTask; index += stepSize; // Report progress to UI. progress?.Report(MediaDatas.Count); } } // Register tracker. RegisterFolderContentTracker(); // Run tracker once to pick up on any file changes since last application boot. Query_ContentsChanged(_query, null); } }