public async Task SetStorageQueryResultAsync(StorageFolderQueryResult InputQuery) { if (InputQuery == null) { throw new ArgumentNullException(nameof(InputQuery), "Parameter could not be null"); } FolderQuery = InputQuery; MaxNum = await FolderQuery.GetItemCountAsync(); CurrentIndex = MaxNum > 20 ? 20 : MaxNum; if (MaxNum > 20) { HasMoreItems = true; } }
private async void FileContentsChanged(IStorageQueryResultBase sender, object args) { if (_filesRefreshing) { Debug.WriteLine("Filesystem change event fired but refresh is already running"); return; } else { Debug.WriteLine("Filesystem change event fired. Refreshing..."); } _filesRefreshing = true; //query options have to be reapplied otherwise old results are returned _fileQueryResult.ApplyNewQueryOptions(_options); _folderQueryResult.ApplyNewQueryOptions(_options); var fileCount = await _fileQueryResult.GetItemCountAsync(); var folderCount = await _folderQueryResult.GetItemCountAsync(); var files = await _fileQueryResult.GetFilesAsync(); var folders = await _folderQueryResult.GetFoldersAsync(); var cancellationTokenSourceCopy = _cancellationTokenSource; // modifying a file also results in a new unique FolderRelativeId so no need to check for DateModified explicitly var addedFiles = files.Select(f => f.FolderRelativeId).Except(_filesAndFolders.Select(f => f.FolderRelativeId)); var addedFolders = folders.Select(f => f.FolderRelativeId).Except(_filesAndFolders.Select(f => f.FolderRelativeId)); var removedFilesAndFolders = _filesAndFolders .Select(f => f.FolderRelativeId) .Except(files.Select(f => f.FolderRelativeId)) .Except(folders.Select(f => f.FolderRelativeId)) .ToArray(); foreach (var file in addedFiles) { var toAdd = files.First(f => f.FolderRelativeId == file); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { await AddFile(toAdd, _pageName, cancellationTokenSourceCopy.Token); }); } foreach (var folder in addedFolders) { var toAdd = folders.First(f => f.FolderRelativeId == folder); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { await AddFolder(toAdd, _pageName, cancellationTokenSourceCopy.Token); }); } foreach (var item in removedFilesAndFolders) { var toRemove = _filesAndFolders.First(f => f.FolderRelativeId == item); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { RemoveFileOrFolder(toRemove); }); } _filesRefreshing = false; Debug.WriteLine("Filesystem refresh complete"); }
public async void AddItemsToCollectionAsync(string path, Page currentPage) { CancelLoadAndClearFiles(); _cancellationTokenSource = new CancellationTokenSource(); var tokenSourceCopy = _cancellationTokenSource; TextState.isVisible = Visibility.Collapsed; _pageName = currentPage.Name; Universal.path = path; if (!_pageName.Contains("Classic")) { _filesAndFolders.Clear(); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); PVIS.isVisible = Visibility.Visible; switch (Universal.path) { case "Desktop": Universal.path = MainPage.DesktopPath; break; case "Downloads": Universal.path = MainPage.DownloadsPath; break; case "Documents": Universal.path = MainPage.DocumentsPath; break; case "Pictures": Universal.path = MainPage.PicturesPath; break; case "Music": Universal.path = MainPage.MusicPath; break; case "Videos": Universal.path = MainPage.VideosPath; break; case "OneDrive": Universal.path = MainPage.OneDrivePath; break; } try { _rootFolder = await StorageFolder.GetFolderFromPathAsync(Universal.path); History.AddToHistory(Universal.path); if (History.HistoryList.Count == 1) // If this is the only item present in History, we don't want back button to be enabled { BS.isEnabled = false; } else if (History.HistoryList.Count > 1) // Otherwise, if this is not the first item, we'll enable back click { BS.isEnabled = true; } switch (await _rootFolder.GetIndexedStateAsync()) { case (IndexedState.FullyIndexed): _options = new QueryOptions(); _options.FolderDepth = FolderDepth.Shallow; if (_pageName.Contains("Generic")) { _options.SetThumbnailPrefetch(ThumbnailMode.ListView, 20, ThumbnailOptions.UseCurrentScale); _options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.DateModified", "System.ContentType", "System.Size", "System.FileExtension" }); } else if (_pageName.Contains("Photo")) { _options.SetThumbnailPrefetch(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); _options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileExtension" }); } _options.IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties; break; default: _options = new QueryOptions(); _options.FolderDepth = FolderDepth.Shallow; if (_pageName.Contains("Generic")) { _options.SetThumbnailPrefetch(ThumbnailMode.ListView, 20, ThumbnailOptions.UseCurrentScale); _options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.DateModified", "System.ContentType", "System.ItemPathDisplay", "System.Size", "System.FileExtension" }); } else if (_pageName.Contains("Photo")) { _options.SetThumbnailPrefetch(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); _options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileExtension" }); } _options.IndexerOption = IndexerOption.UseIndexerWhenAvailable; break; } SortEntry sort = new SortEntry() { PropertyName = "System.FileName", AscendingOrder = true }; _options.SortOrder.Add(sort); if (!_rootFolder.AreQueryOptionsSupported(_options)) { _options.SortOrder.Clear(); } uint index = 0; _folderQueryResult = _rootFolder.CreateFolderQueryWithOptions(_options); //_folderQueryResult.ContentsChanged += FolderContentsChanged; var numFolders = await _folderQueryResult.GetItemCountAsync(); IReadOnlyList <StorageFolder> storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step); while (storageFolders.Count > 0) { foreach (StorageFolder folder in storageFolders) { if (tokenSourceCopy.IsCancellationRequested) { return; } await AddFolder(folder, _pageName, tokenSourceCopy.Token); } index += _step; storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step); } index = 0; _fileQueryResult = _rootFolder.CreateFileQueryWithOptions(_options); _fileQueryResult.ContentsChanged += FileContentsChanged; var numFiles = await _fileQueryResult.GetItemCountAsync(); IReadOnlyList <StorageFile> storageFiles = await _fileQueryResult.GetFilesAsync(index, _step); while (storageFiles.Count > 0) { foreach (StorageFile file in storageFiles) { if (tokenSourceCopy.IsCancellationRequested) { return; } await AddFile(file, _pageName, tokenSourceCopy.Token); } index += _step; storageFiles = await _fileQueryResult.GetFilesAsync(index, _step); } if (numFiles + numFolders == 0) { TextState.isVisible = Visibility.Visible; } stopwatch.Stop(); Debug.WriteLine("Loading of items in " + Universal.path + " completed in " + stopwatch.Elapsed.Seconds + " seconds.\n"); } catch (UnauthorizedAccessException e) { if (path.Contains(@"C:\")) { DisplayConsentDialog(); } else { MessageDialog unsupportedDevice = new MessageDialog("This device may be unsupported. Please file an issue report in Settings - About containing what device we couldn't access. Technical information: " + e, "Unsupported Device"); await unsupportedDevice.ShowAsync(); return; } } catch (COMException e) { Frame rootFrame = Window.Current.Content as Frame; MessageDialog driveGone = new MessageDialog(e.Message, "Drive Unplugged"); await driveGone.ShowAsync(); rootFrame.Navigate(typeof(MainPage), new SuppressNavigationTransitionInfo()); return; } if (!_pageName.Contains("Classic")) { PVIS.isVisible = Visibility.Collapsed; } PVIS.isVisible = Visibility.Collapsed; }