diff --git a/Files UWP/AddItem.xaml.cs b/Files UWP/AddItem.xaml.cs index 889be636be2f..01e727bafa19 100644 --- a/Files UWP/AddItem.xaml.cs +++ b/Files UWP/AddItem.xaml.cs @@ -42,8 +42,8 @@ private async void ListView_ItemClick(object sender, ItemClickEventArgs e) var userInput = GenericFileBrowser.inputForRename; if (userInput != null) { - await folderToCreateItem.CreateFolderAsync(userInput, CreationCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Add(new ListedItem(){ FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Collapsed, FolderImg = Visibility.Visible, FileIconVis = Visibility.Collapsed, FileType = "Folder", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput) }); + var folder = await folderToCreateItem.CreateFolderAsync(userInput, CreationCollisionOption.FailIfExists); + App.ViewModel.AddFileOrFolder(new ListedItem(folder.FolderRelativeId){ FileName = userInput, FileDateReal = DateTimeOffset.Now, EmptyImgVis = Visibility.Collapsed, FolderImg = Visibility.Visible, FileIconVis = Visibility.Collapsed, FileType = "Folder", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput) }); } } else if ((e.ClickedItem as AddListItem).Header == "Text Document") @@ -52,8 +52,8 @@ private async void ListView_ItemClick(object sender, ItemClickEventArgs e) var userInput = GenericFileBrowser.inputForRename; if (userInput != null) { - await folderToCreateItem.CreateFileAsync(userInput + ".txt", CreationCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "Text Document", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".txt"), DotFileExtension = ".txt" }); + var folder = await folderToCreateItem.CreateFileAsync(userInput + ".txt", CreationCollisionOption.FailIfExists); + App.ViewModel.AddFileOrFolder(new ListedItem(folder.FolderRelativeId) { FileName = userInput, FileDateReal = DateTimeOffset.Now, EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "Text Document", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".txt"), DotFileExtension = ".txt" }); } } else if ((e.ClickedItem as AddListItem).Header == "Bitmap Image") @@ -62,9 +62,8 @@ private async void ListView_ItemClick(object sender, ItemClickEventArgs e) var userInput = GenericFileBrowser.inputForRename; if (userInput != null) { - await folderToCreateItem.CreateFileAsync(userInput + ".bmp", CreationCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = userInput, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "BMP File", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".bmp"), DotFileExtension = ".bmp" }); - + var folder = await folderToCreateItem.CreateFileAsync(userInput + ".bmp", CreationCollisionOption.FailIfExists); + App.ViewModel.AddFileOrFolder(new ListedItem(folder.FolderRelativeId) { FileName = userInput, FileDateReal = DateTimeOffset.Now, EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = "BMP File", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + userInput + ".bmp"), DotFileExtension = ".bmp" }); } } } diff --git a/Files UWP/Filesystem/ItemViewModel.cs b/Files UWP/Filesystem/ItemViewModel.cs index 03e8f57bf878..02906febeda1 100644 --- a/Files UWP/Filesystem/ItemViewModel.cs +++ b/Files UWP/Filesystem/ItemViewModel.cs @@ -1,61 +1,86 @@ -using System; +using ByteSizeLib; +using Files.Navigation; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; +using System.Linq; using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using Windows.ApplicationModel.Core; using Windows.Storage; using Windows.Storage.FileProperties; +using Windows.Storage.Search; +using Windows.UI.Core; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Media.Imaging; -using System.Threading; -using Files.Interacts; -using Files.Navigation; -using Windows.Storage.Search; using TreeView = Microsoft.UI.Xaml.Controls.TreeView; -using Windows.UI.Core; -using ByteSizeLib; -using Windows.Storage.Pickers; -using Microsoft.Win32.SafeHandles; -using System.IO; -using Windows.Foundation; -using System.Threading.Tasks; -using Windows.ApplicationModel.Core; namespace Files.Filesystem { public class ItemViewModel { - public ObservableCollection ClassicFolderList { get; set; } = new ObservableCollection(); - public ObservableCollection ClassicFileList { get; set; } = new ObservableCollection(); - public ObservableCollection FilesAndFolders { get; set; } = new ObservableCollection(); - public ObservableCollection ChildrenList; - public ListedItem LI { get; } = new ListedItem(); + public ReadOnlyObservableCollection FilesAndFolders { get; } + public ReadOnlyObservableCollection ClassicFolderList { get; } + public ReadOnlyObservableCollection ClassicFileList { get; } + public UniversalPath Universal { get; } = new UniversalPath(); public EmptyFolderTextState TextState { get; set; } = new EmptyFolderTextState(); public BackState BS { get; set; } = new BackState(); public ForwardState FS { get; set; } = new ForwardState(); public ProgressUIVisibility PVIS { get; set; } = new ProgressUIVisibility(); - private StorageFolder folder; - private string itemName; - private string itemDate; - private string itemType; - private string itemPath; - private string itemSize; - private string itemFileExtension; - private Visibility itemThumbnailImgVis; - private Visibility itemEmptyImgVis; - private Visibility itemFolderImgVis; - private StorageItemThumbnail itemThumbnailImg; - public string pageName; - public CancellationTokenSource tokenSource; + + private ObservableCollection _filesAndFolders; + private ObservableCollection _classicFileList; + private ObservableCollection _classicFolderList; + + private StorageFolderQueryResult _folderQueryResult; + private StorageFileQueryResult _fileQueryResult; + private CancellationTokenSource _cancellationTokenSource; + private StorageFolder _rootFolder; + private QueryOptions _options; + private string _pageName; + + private volatile bool _filesRefreshing; + //private volatile bool _foldersRefreshing; + + private const int _step = 250; public ItemViewModel() { - + _filesAndFolders = new ObservableCollection(); + _classicFileList = new ObservableCollection(); + _classicFolderList = new ObservableCollection(); + + FilesAndFolders = new ReadOnlyObservableCollection(_filesAndFolders); + ClassicFileList = new ReadOnlyObservableCollection(_classicFileList); + ClassicFolderList = new ReadOnlyObservableCollection(_classicFolderList); + } + + public void AddFileOrFolder(ListedItem item) + { + _filesAndFolders.Add(item); + } + + public void RemoveFileOrFolder(ListedItem item) + { + _filesAndFolders.Remove(item); + } + + public void CancelLoadAndClearFiles() + { + if (_cancellationTokenSource == null) { return; } + + _cancellationTokenSource.Cancel(); + _filesAndFolders.Clear(); + + //_folderQueryResult.ContentsChanged -= FolderContentsChanged; + _fileQueryResult.ContentsChanged -= FileContentsChanged; } private async void DisplayConsentDialog() @@ -65,14 +90,20 @@ private async void DisplayConsentDialog() public async void AddItemsToCollectionAsync(string path, Page currentPage) { + CancelLoadAndClearFiles(); + + _cancellationTokenSource = new CancellationTokenSource(); + var tokenSourceCopy = _cancellationTokenSource; + TextState.isVisible = Visibility.Collapsed; - tokenSource = new CancellationTokenSource(); - CancellationToken token = App.ViewModel.tokenSource.Token; - pageName = currentPage.Name; + + _pageName = currentPage.Name; Universal.path = path; - if (!pageName.Contains("Classic")) - FilesAndFolders.Clear(); + if (!_pageName.Contains("Classic")) + { + _filesAndFolders.Clear(); + } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); @@ -106,8 +137,8 @@ public async void AddItemsToCollectionAsync(string path, Page currentPage) try { + _rootFolder = await StorageFolder.GetFolderFromPathAsync(Universal.path); - folder = 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 { @@ -118,234 +149,83 @@ public async void AddItemsToCollectionAsync(string path, Page currentPage) BS.isEnabled = true; } - QueryOptions options = null; - switch(await folder.GetIndexedStateAsync()) + switch (await _rootFolder.GetIndexedStateAsync()) { case (IndexedState.FullyIndexed): - options = new QueryOptions(); - options.FolderDepth = FolderDepth.Shallow; - if (pageName.Contains("Generic")) + _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" }); + _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")) + else if (_pageName.Contains("Photo")) { - options.SetThumbnailPrefetch(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); - options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileExtension" }); + _options.SetThumbnailPrefetch(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); + _options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileExtension" }); } - options.IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties; + _options.IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties; break; default: - options = new QueryOptions(); - options.FolderDepth = FolderDepth.Shallow; - if (pageName.Contains("Generic")) + _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" }); + _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")) + else if (_pageName.Contains("Photo")) { - options.SetThumbnailPrefetch(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); - options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileExtension" }); + _options.SetThumbnailPrefetch(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); + _options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, new string[] { "System.FileExtension" }); } - options.IndexerOption = IndexerOption.UseIndexerWhenAvailable; + _options.IndexerOption = IndexerOption.UseIndexerWhenAvailable; break; } - + SortEntry sort = new SortEntry() { PropertyName = "System.FileName", AscendingOrder = true }; - options.SortOrder.Add(sort); - if (!folder.AreQueryOptionsSupported(options)) + _options.SortOrder.Add(sort); + if (!_rootFolder.AreQueryOptionsSupported(_options)) { - options.SortOrder.Clear(); + _options.SortOrder.Clear(); } uint index = 0; - const uint step = 250; - BasicProperties basicProperties; - StorageFolderQueryResult folderQueryResult = folder.CreateFolderQueryWithOptions(options); - uint NumFolItems = await folderQueryResult.GetItemCountAsync(); - IReadOnlyList storageFolders = await folderQueryResult.GetFoldersAsync(index, step); + _folderQueryResult = _rootFolder.CreateFolderQueryWithOptions(_options); + //_folderQueryResult.ContentsChanged += FolderContentsChanged; + var numFolders = await _folderQueryResult.GetItemCountAsync(); + IReadOnlyList storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step); while (storageFolders.Count > 0) { foreach (StorageFolder folder in storageFolders) { - if (token.IsCancellationRequested) - { - return; - } - basicProperties = await folder.GetBasicPropertiesAsync(); - itemName = folder.Name; - SetAsFriendlyDate(basicProperties.ItemDate.LocalDateTime); - itemPath = folder.Path; - if (ByteSize.FromBytes(basicProperties.Size).KiloBytes < 1) - { - itemSize = "0 KB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).KiloBytes >= 1 && ByteSize.FromBytes(basicProperties.Size).MegaBytes < 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).KiloBytes) + " KB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).MegaBytes >= 1 && ByteSize.FromBytes(basicProperties.Size).GigaBytes < 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).MegaBytes) + " MB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).GigaBytes >= 1 && ByteSize.FromBytes(basicProperties.Size).TeraBytes < 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).GigaBytes) + " GB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).TeraBytes >= 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).TeraBytes) + " TB"; - } - - itemType = "Folder"; - itemFolderImgVis = Visibility.Visible; - itemThumbnailImgVis = Visibility.Collapsed; - itemEmptyImgVis = Visibility.Collapsed; - - if (!pageName.Contains("Classic")) - { - if (token.IsCancellationRequested) - { - tokenSource = null; - return; - } - FilesAndFolders.Add(new ListedItem() { FileName = itemName, FileDate = itemDate, FileType = itemType, FolderImg = itemFolderImgVis, FileImg = null, FileIconVis = itemThumbnailImgVis, FilePath = itemPath, DotFileExtension = itemFileExtension, EmptyImgVis = itemEmptyImgVis, FileSize = null }); - } - else - { - if (token.IsCancellationRequested) - { - tokenSource = null; - return; - } - ClassicFolderList.Add(new Classic_ListedFolderItem() { FileName = itemName, FileDate = itemDate, FileExtension = itemType, FilePath = itemPath }); - } - - + if (tokenSourceCopy.IsCancellationRequested) { return; } + await AddFolder(folder, _pageName, tokenSourceCopy.Token); } - index += step; - storageFolders = await folderQueryResult.GetFoldersAsync(index, step); + index += _step; + storageFolders = await _folderQueryResult.GetFoldersAsync(index, _step); } index = 0; - StorageFileQueryResult fileQueryResult = folder.CreateFileQueryWithOptions(options); - uint NumFileItems = await fileQueryResult.GetItemCountAsync(); - IReadOnlyList storageFiles = await fileQueryResult.GetFilesAsync(index, step); - List propertiesToRetrieve = new List(); - propertiesToRetrieve.Add("System.FileExtension"); + _fileQueryResult = _rootFolder.CreateFileQueryWithOptions(_options); + _fileQueryResult.ContentsChanged += FileContentsChanged; + var numFiles = await _fileQueryResult.GetItemCountAsync(); + IReadOnlyList storageFiles = await _fileQueryResult.GetFilesAsync(index, _step); while (storageFiles.Count > 0) { foreach (StorageFile file in storageFiles) { - basicProperties = await file.GetBasicPropertiesAsync(); - var props = await file.Properties.RetrievePropertiesAsync(propertiesToRetrieve); - if (token.IsCancellationRequested) - { - return; - } - itemName = file.DisplayName; - SetAsFriendlyDate(basicProperties.DateModified.LocalDateTime); - itemPath = file.Path; - if (ByteSize.FromBytes(basicProperties.Size).KiloBytes < 1) - { - itemSize = "0 KB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).KiloBytes >= 1 && ByteSize.FromBytes(basicProperties.Size).MegaBytes < 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).KiloBytes) + " KB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).MegaBytes >= 1 && ByteSize.FromBytes(basicProperties.Size).GigaBytes < 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).MegaBytes) + " MB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).GigaBytes >= 1 && ByteSize.FromBytes(basicProperties.Size).TeraBytes < 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).GigaBytes) + " GB"; - } - else if (ByteSize.FromBytes(basicProperties.Size).TeraBytes >= 1) - { - itemSize = decimal.Round((decimal)ByteSize.FromBytes(basicProperties.Size).TeraBytes) + " TB"; - } - BitmapImage icon = new BitmapImage(); - itemType = file.DisplayType; - itemFolderImgVis = Visibility.Collapsed; - itemFileExtension = props["System.FileExtension"].ToString(); - if (!pageName.Contains("Photo")) - { - try - { - itemThumbnailImg = await file.GetThumbnailAsync(ThumbnailMode.ListView, 20, ThumbnailOptions.UseCurrentScale); - if (itemThumbnailImg != null) - { - itemEmptyImgVis = Visibility.Collapsed; - itemThumbnailImgVis = Visibility.Visible; - await icon.SetSourceAsync(itemThumbnailImg.CloneStream()); - } - else - { - itemEmptyImgVis = Visibility.Visible; - itemThumbnailImgVis = Visibility.Collapsed; - } - } - catch - { - itemEmptyImgVis = Visibility.Visible; - itemThumbnailImgVis = Visibility.Collapsed; - // Catch here to avoid crash - // TODO maybe some logging could be added in the future... - } - } - else - { - try - { - itemThumbnailImg = await file.GetThumbnailAsync(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); - if (itemThumbnailImg != null) - { - itemEmptyImgVis = Visibility.Collapsed; - itemThumbnailImgVis = Visibility.Visible; - await icon.SetSourceAsync(itemThumbnailImg.CloneStream()); - } - else - { - itemEmptyImgVis = Visibility.Visible; - itemThumbnailImgVis = Visibility.Collapsed; - } - } - catch - { - itemEmptyImgVis = Visibility.Visible; - itemThumbnailImgVis = Visibility.Collapsed; - - } - } - if (!pageName.Contains("Classic")) - { - if (token.IsCancellationRequested) - { - return; - } - FilesAndFolders.Add(new ListedItem() { DotFileExtension = itemFileExtension, EmptyImgVis = itemEmptyImgVis, FileImg = icon, FileIconVis = itemThumbnailImgVis, FolderImg = itemFolderImgVis, FileName = itemName, FileDate = itemDate, FileType = itemType, FilePath = itemPath, FileSize = itemSize }); - } - else - { - if (token.IsCancellationRequested) - { - return; - } - ClassicFileList.Add(new ListedItem() { FileImg = icon, FileIconVis = itemThumbnailImgVis, FolderImg = itemFolderImgVis, FileName = itemName, FileDate = itemDate, FileType = itemType, FilePath = itemPath }); - } + if (tokenSourceCopy.IsCancellationRequested) { return; } + await AddFile(file, _pageName, tokenSourceCopy.Token); } - index += step; - storageFiles = await fileQueryResult.GetFilesAsync(index, step); + index += _step; + storageFiles = await _fileQueryResult.GetFilesAsync(index, _step); } - if (NumFolItems + NumFileItems == 0) + if (numFiles + numFolders == 0) { TextState.isVisible = Visibility.Visible; } @@ -375,190 +255,244 @@ public async void AddItemsToCollectionAsync(string path, Page currentPage) return; } - if (!pageName.Contains("Classic")) + if (!_pageName.Contains("Classic")) { PVIS.isVisible = Visibility.Collapsed; } PVIS.isVisible = Visibility.Collapsed; - tokenSource = null; } - public void SetAsFriendlyDate(DateTime d) + public static async void FillTreeNode(object item, TreeView EntireControl) + { + var pathToFillFrom = (item as Classic_ListedFolderItem)?.FilePath; + StorageFolder folderFromPath = await StorageFolder.GetFolderFromPathAsync(pathToFillFrom); + IReadOnlyList SubFolderList = await folderFromPath.GetFoldersAsync(); + foreach (StorageFolder fol in SubFolderList) + { + var name = fol.Name; + var date = fol.DateCreated.LocalDateTime.ToString(CultureInfo.InvariantCulture); + var ext = fol.DisplayType; + var path = fol.Path; + (item as Classic_ListedFolderItem)?.Children.Add(new Classic_ListedFolderItem() { FileName = name, FilePath = path, FileDate = date, FileExtension = ext }); + + } + } + + private async Task AddFolder(StorageFolder folder, string pageName, CancellationToken token) { - if (d.Year == DateTime.Now.Year) // If item is accessed in the same year as stored + if (token.IsCancellationRequested) { return; } + + var basicProperties = await folder.GetBasicPropertiesAsync(); + + if (!pageName.Contains("Classic")) + { + if (token.IsCancellationRequested) { return; } + + _filesAndFolders.Add(new ListedItem(folder.FolderRelativeId) + { + FileName = folder.Name, + FileDateReal = basicProperties.DateModified, + FileType = "Folder", //TODO: Take a look at folder.DisplayType + FolderImg = Visibility.Visible, + FileImg = null, + FileIconVis = Visibility.Collapsed, + FilePath = folder.Path, + EmptyImgVis = Visibility.Collapsed, + FileSize = null + }); + } + else { - if (d.Month == DateTime.Now.Month) // If item is accessed in the same month as stored + if (token.IsCancellationRequested) { return; } + + _classicFolderList.Add(new Classic_ListedFolderItem() { - if ((DateTime.Now.Day - d.Day) < 7) // If item is accessed on the same week + FileName = folder.Name, + FileDate = ListedItem.GetFriendlyDate(basicProperties.DateModified), + FileExtension = "Folder", + FilePath = folder.Path + }); + } + } + + private async Task AddFile(StorageFile file, string pageName, CancellationToken token) + { + if (token.IsCancellationRequested) { return; } + + var basicProperties = await file.GetBasicPropertiesAsync(); + + var itemName = file.DisplayName; + var itemDate = basicProperties.DateModified; + var itemPath = file.Path; + var itemSize = ByteSize.FromBytes(basicProperties.Size).ToString(); + var itemType = file.DisplayType; + var itemFolderImgVis = Visibility.Collapsed; + var itemFileExtension = file.FileType; + + BitmapImage icon = new BitmapImage(); + Visibility itemThumbnailImgVis; + Visibility itemEmptyImgVis; + + if (!pageName.Contains("Photo")) + { + try + { + var itemThumbnailImg = await file.GetThumbnailAsync(ThumbnailMode.ListView, 20, ThumbnailOptions.UseCurrentScale); + if (itemThumbnailImg != null) { - if (d.DayOfWeek == DateTime.Now.DayOfWeek) // If item is accessed on the same day as stored - { - if ((DateTime.Now.Hour - d.Hour) > 1) - { - itemDate = DateTime.Now.Hour - d.Hour + " hours ago"; - } - else - { - itemDate = DateTime.Now.Hour - d.Hour + " hour ago"; - } - } - else // If item is from a previous day of the same week - { - itemDate = d.DayOfWeek + " at " + d.ToShortTimeString(); - } + itemEmptyImgVis = Visibility.Collapsed; + itemThumbnailImgVis = Visibility.Visible; + await icon.SetSourceAsync(itemThumbnailImg.CloneStream()); } - else // If item is from a previous week of the same month + else { - string monthAsString = "Month"; - switch (d.Month) - { - case 1: - monthAsString = "January"; - break; - case 2: - monthAsString = "February"; - break; - case 3: - monthAsString = "March"; - break; - case 4: - monthAsString = "April"; - break; - case 5: - monthAsString = "May"; - break; - case 6: - monthAsString = "June"; - break; - case 7: - monthAsString = "July"; - break; - case 8: - monthAsString = "August"; - break; - case 9: - monthAsString = "September"; - break; - case 10: - monthAsString = "October"; - break; - case 11: - monthAsString = "November"; - break; - case 12: - monthAsString = "December"; - break; - } - itemDate = monthAsString + " " + d.Day; + itemEmptyImgVis = Visibility.Visible; + itemThumbnailImgVis = Visibility.Collapsed; } - } - else // If item is from a past month of the same year + catch { - string monthAsString = "Month"; - switch (d.Month) + itemEmptyImgVis = Visibility.Visible; + itemThumbnailImgVis = Visibility.Collapsed; + // Catch here to avoid crash + // TODO maybe some logging could be added in the future... + } + } + else + { + try + { + var itemThumbnailImg = await file.GetThumbnailAsync(ThumbnailMode.PicturesView, 275, ThumbnailOptions.ResizeThumbnail); + if (itemThumbnailImg != null) { - case 1: - monthAsString = "January"; - break; - case 2: - monthAsString = "February"; - break; - case 3: - monthAsString = "March"; - break; - case 4: - monthAsString = "April"; - break; - case 5: - monthAsString = "May"; - break; - case 6: - monthAsString = "June"; - break; - case 7: - monthAsString = "July"; - break; - case 8: - monthAsString = "August"; - break; - case 9: - monthAsString = "September"; - break; - case 10: - monthAsString = "October"; - break; - case 11: - monthAsString = "November"; - break; - case 12: - monthAsString = "December"; - break; + itemEmptyImgVis = Visibility.Collapsed; + itemThumbnailImgVis = Visibility.Visible; + await icon.SetSourceAsync(itemThumbnailImg.CloneStream()); } - itemDate = monthAsString + " " + d.Day; + else + { + itemEmptyImgVis = Visibility.Visible; + itemThumbnailImgVis = Visibility.Collapsed; + } + } + catch + { + itemEmptyImgVis = Visibility.Visible; + itemThumbnailImgVis = Visibility.Collapsed; + } } - else // If item is from a past year + + if (!pageName.Contains("Classic")) { - string monthAsString = "Month"; - switch (d.Month) + if (token.IsCancellationRequested) { return; } + + _filesAndFolders.Add(new ListedItem(file.FolderRelativeId) { - case 1: - monthAsString = "January"; - break; - case 2: - monthAsString = "February"; - break; - case 3: - monthAsString = "March"; - break; - case 4: - monthAsString = "April"; - break; - case 5: - monthAsString = "May"; - break; - case 6: - monthAsString = "June"; - break; - case 7: - monthAsString = "July"; - break; - case 8: - monthAsString = "August"; - break; - case 9: - monthAsString = "September"; - break; - case 10: - monthAsString = "October"; - break; - case 11: - monthAsString = "November"; - break; - case 12: - monthAsString = "December"; - break; - } - itemDate = monthAsString + " " + d.Day + ", " + d.Year; + DotFileExtension = itemFileExtension, + EmptyImgVis = itemEmptyImgVis, + FileImg = icon, + FileIconVis = itemThumbnailImgVis, + FolderImg = itemFolderImgVis, + FileName = itemName, + FileDateReal = itemDate, + FileType = itemType, + FilePath = itemPath, + FileSize = itemSize + }); + } + else + { + if (token.IsCancellationRequested) { return; } + + _classicFileList.Add(new ListedItem(file.FolderRelativeId) + { + FileImg = icon, + FileIconVis = itemThumbnailImgVis, + FolderImg = itemFolderImgVis, + FileName = itemName, + FileDateReal = itemDate, + FileType = itemType, + FilePath = itemPath + }); } } - public static async void FillTreeNode(object item, TreeView EntireControl) + private async void FileContentsChanged(IStorageQueryResultBase sender, object args) { - var pathToFillFrom = (item as Classic_ListedFolderItem)?.FilePath; - StorageFolder folderFromPath = await StorageFolder.GetFolderFromPathAsync(pathToFillFrom); - IReadOnlyList SubFolderList = await folderFromPath.GetFoldersAsync(); - foreach (StorageFolder fol in SubFolderList) + if (_filesRefreshing) { - var name = fol.Name; - var date = fol.DateCreated.LocalDateTime.ToString(CultureInfo.InvariantCulture); - var ext = fol.DisplayType; - var path = fol.Path; - (item as Classic_ListedFolderItem)?.Children.Add(new Classic_ListedFolderItem() { FileName = name, FilePath = path, FileDate = date, FileExtension = ext }); + 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"); } + + //private async void FolderContentsChanged(IStorageQueryResultBase sender, object args) + //{ + // if (_foldersRefreshing) { return; } + + // _foldersRefreshing = true; + + // _foldersRefreshing = false; + //} } } diff --git a/Files UWP/Filesystem/ListedItem.cs b/Files UWP/Filesystem/ListedItem.cs index e8a5d3acc4f9..cc2785d8c9f3 100644 --- a/Files UWP/Filesystem/ListedItem.cs +++ b/Files UWP/Filesystem/ListedItem.cs @@ -1,23 +1,65 @@ -using Windows.UI.Xaml; +using System; +using Windows.UI.Xaml; using Windows.UI.Xaml.Media.Imaging; namespace Files.Filesystem { public class ListedItem { + public string FolderRelativeId { get; } + public Visibility FolderImg { get; set; } public Visibility FileIconVis { get; set; } public Visibility EmptyImgVis { get; set; } public BitmapImage FileImg { get; set; } public string FileName { get; set; } - public string FileDate { get; set; } + public string FileDate { get; private set; } public string FileType { get; set; } public string DotFileExtension { get; set; } public string FilePath { get; set; } public string FileSize { get; set; } - public ListedItem() + + public DateTimeOffset FileDateReal + { + get { return _fileDataReal; } + set + { + FileDate = GetFriendlyDate(value); + _fileDataReal = value; + } + } + + private DateTimeOffset _fileDataReal; + + public ListedItem(string folderRelativeId) + { + FolderRelativeId = folderRelativeId; + } + + public static string GetFriendlyDate(DateTimeOffset d) { + var elapsed = DateTimeOffset.Now - d; + if (elapsed.TotalDays > 7) + { + return d.ToString("D"); + } + else if (elapsed.TotalDays > 1) + { + return $"{elapsed.Days} days ago"; + } + else if (elapsed.TotalHours > 1) + { + return $"{elapsed.Hours} hours ago"; + } + else if (elapsed.TotalMinutes > 1) + { + return $"{elapsed.Minutes} minutes ago"; + } + else + { + return $"{elapsed.Seconds} seconds ago"; + } } } } diff --git a/Files UWP/GenericFileBrowser.xaml.cs b/Files UWP/GenericFileBrowser.xaml.cs index 2be495b2439c..fb592df479db 100644 --- a/Files UWP/GenericFileBrowser.xaml.cs +++ b/Files UWP/GenericFileBrowser.xaml.cs @@ -112,7 +112,7 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs) { base.OnNavigatedTo(eventArgs); var parameters = (string)eventArgs.Parameter; - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); App.ViewModel.Universal.path = parameters; App.ViewModel.AddItemsToCollectionAsync(App.ViewModel.Universal.path, GenericItemView); if (parameters.Equals(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))) @@ -332,11 +332,7 @@ private async void VisiblePath_TextChanged(object sender, KeyRoutedEventArgs e) var CurrentInput = PathBox.Text; if (CurrentInput != App.ViewModel.Universal.path) { - if (App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - App.ViewModel.FilesAndFolders.Clear(); - } + App.ViewModel.CancelLoadAndClearFiles(); if (CurrentInput == "Home" || CurrentInput == "home") { diff --git a/Files UWP/Interacts/Interaction.cs b/Files UWP/Interacts/Interaction.cs index 38fff50db6f4..c7a4cb7bf88b 100644 --- a/Files UWP/Interacts/Interaction.cs +++ b/Files UWP/Interacts/Interaction.cs @@ -20,6 +20,7 @@ using System.Collections; using Windows.Foundation; using Windows.UI.Xaml.Controls.Primitives; +using System.IO; namespace Files.Interacts { @@ -60,7 +61,7 @@ public static async void List_ItemClick(object sender, DoubleTappedRoutedEventAr App.ViewModel.TextState.isVisible = Visibility.Collapsed; History.ForwardList.Clear(); App.ViewModel.FS.isEnabled = false; - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { App.PathText.Text = "Desktop"; @@ -221,7 +222,7 @@ public static async void List_ItemClick(object sender, DoubleTappedRoutedEventAr App.ViewModel.TextState.isVisible = Visibility.Collapsed; History.ForwardList.Clear(); App.ViewModel.FS.isEnabled = false; - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); if (clickedOnItem.FilePath == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { App.PathText.Text = "Desktop"; @@ -454,7 +455,7 @@ public static async void OpenItem_Click(object sender, RoutedEventArgs e) App.ViewModel.TextState.isVisible = Visibility.Collapsed; History.ForwardList.Clear(); App.ViewModel.FS.isEnabled = false; - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); App.ViewModel.Universal.path = RowData.FilePath; App.ViewModel.AddItemsToCollectionAsync(App.ViewModel.Universal.path, GenericFileBrowser.GFBPageName); } @@ -476,7 +477,7 @@ public static async void OpenItem_Click(object sender, RoutedEventArgs e) App.ViewModel.TextState.isVisible = Visibility.Collapsed; History.ForwardList.Clear(); App.ViewModel.FS.isEnabled = false; - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); App.ViewModel.Universal.path = RowData.FilePath; App.ViewModel.AddItemsToCollectionAsync(RowData.FilePath, PhotoAlbum.PAPageName); } @@ -567,7 +568,7 @@ public static async void DeleteItem_Click(object sender, RoutedEventArgs e) await item.DeleteAsync(StorageDeleteOption.Default); } - App.ViewModel.FilesAndFolders.Remove(storItem); + App.ViewModel.RemoveFileOrFolder(storItem); } Debug.WriteLine("Ended for loop"); History.ForwardList.Clear(); @@ -594,7 +595,7 @@ public static async void DeleteItem_Click(object sender, RoutedEventArgs e) await item.DeleteAsync(StorageDeleteOption.Default); } - App.ViewModel.FilesAndFolders.Remove(storItem); + App.ViewModel.RemoveFileOrFolder(storItem); } Debug.WriteLine("Ended for loop"); History.ForwardList.Clear(); @@ -625,16 +626,36 @@ public static async void RenameItem_Click(object sender, RoutedEventArgs e) { var item = await StorageFolder.GetFolderFromPathAsync(RowData.FilePath); await item.RenameAsync(input, NameCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Remove(RowData); - App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = input, FileDate = "Now", EmptyImgVis = Visibility.Collapsed, FolderImg = Visibility.Visible, FileIconVis = Visibility.Collapsed, FileType = "Folder", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + input) }); + App.ViewModel.RemoveFileOrFolder(RowData); + App.ViewModel.AddFileOrFolder(new ListedItem(item.FolderRelativeId) + { + FileName = input, + FileDateReal = DateTimeOffset.Now, + EmptyImgVis = Visibility.Collapsed, + FolderImg = Visibility.Visible, + FileIconVis = Visibility.Collapsed, + FileType = "Folder", + FileImg = null, + FilePath = Path.Combine(App.ViewModel.Universal.path, input) + }); } else { var item = await StorageFile.GetFileFromPathAsync(RowData.FilePath); await item.RenameAsync(input + RowData.DotFileExtension, NameCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Remove(RowData); - App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = input, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = RowData.FileType, FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + input + RowData.DotFileExtension), DotFileExtension = RowData.DotFileExtension }); - + App.ViewModel.RemoveFileOrFolder(RowData); + App.ViewModel.AddFileOrFolder(new ListedItem(item.FolderRelativeId) + { + FileName = input, + FileDateReal = DateTimeOffset.Now, + EmptyImgVis = Visibility.Visible, + FolderImg = Visibility.Collapsed, + FileIconVis = Visibility.Collapsed, + FileType = RowData.FileType, + FileImg = null, + FilePath = Path.Combine(App.ViewModel.Universal.path, input + RowData.DotFileExtension), + DotFileExtension = RowData.DotFileExtension + }); } } @@ -659,16 +680,36 @@ public static async void RenameItem_Click(object sender, RoutedEventArgs e) { var item = await StorageFolder.GetFolderFromPathAsync(BoxData.FilePath); await item.RenameAsync(input, NameCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Remove(BoxData); - App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = input, FileDate = "Now", EmptyImgVis = Visibility.Collapsed, FolderImg = Visibility.Visible, FileIconVis = Visibility.Collapsed, FileType = "Folder", FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + input) }); + App.ViewModel.RemoveFileOrFolder(BoxData); + App.ViewModel.AddFileOrFolder(new ListedItem(item.FolderRelativeId) + { + FileName = input, + FileDateReal = DateTimeOffset.Now, + EmptyImgVis = Visibility.Collapsed, + FolderImg = Visibility.Visible, + FileIconVis = Visibility.Collapsed, + FileType = "Folder", + FileImg = null, + FilePath = Path.Combine(App.ViewModel.Universal.path, input) + }); } else { var item = await StorageFile.GetFileFromPathAsync(BoxData.FilePath); await item.RenameAsync(input + BoxData.DotFileExtension, NameCollisionOption.FailIfExists); - App.ViewModel.FilesAndFolders.Remove(BoxData); - App.ViewModel.FilesAndFolders.Add(new ListedItem() { FileName = input, FileDate = "Now", EmptyImgVis = Visibility.Visible, FolderImg = Visibility.Collapsed, FileIconVis = Visibility.Collapsed, FileType = BoxData.FileType, FileImg = null, FilePath = (App.ViewModel.Universal.path + "\\" + input + BoxData.DotFileExtension), DotFileExtension = BoxData.DotFileExtension }); - + App.ViewModel.RemoveFileOrFolder(BoxData); + App.ViewModel.AddFileOrFolder(new ListedItem(item.FolderRelativeId) + { + FileName = input, + FileDateReal = DateTimeOffset.Now, + EmptyImgVis = Visibility.Visible, + FolderImg = Visibility.Collapsed, + FileIconVis = Visibility.Collapsed, + FileType = BoxData.FileType, + FileImg = null, + FilePath = Path.Combine(App.ViewModel.Universal.path, input + BoxData.DotFileExtension), + DotFileExtension = BoxData.DotFileExtension + }); } } diff --git a/Files UWP/MainPage.xaml.cs b/Files UWP/MainPage.xaml.cs index 9cbe8af40bbd..a4d9f4b387f5 100644 --- a/Files UWP/MainPage.xaml.cs +++ b/Files UWP/MainPage.xaml.cs @@ -151,11 +151,8 @@ private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sende } else { - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - App.ViewModel.FilesAndFolders.Clear(); - } + App.ViewModel.CancelLoadAndClearFiles(); + App.ViewModel.TextState.isVisible = Visibility.Collapsed; if (item.ToString() == "Home") { diff --git a/Files UWP/Navigation/NavigationActions.cs b/Files UWP/Navigation/NavigationActions.cs index bc45c4051d0b..8f085901e0ab 100644 --- a/Files UWP/Navigation/NavigationActions.cs +++ b/Files UWP/Navigation/NavigationActions.cs @@ -11,11 +11,7 @@ public class NavigationActions { public static void Back_Click(object sender, RoutedEventArgs e) { - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - } - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); if (History.HistoryList.Count > 1) { @@ -24,7 +20,7 @@ public static void Back_Click(object sender, RoutedEventArgs e) History.AddToForwardList(History.HistoryList[History.HistoryList.Count - 1]); History.HistoryList.RemoveAt(History.HistoryList.Count - 1); - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); // If the item we are navigating back to is a specific library, accomodate this. if ((History.HistoryList[History.HistoryList.Count - 1]) == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) @@ -160,19 +156,12 @@ public static void Back_Click(object sender, RoutedEventArgs e) public static void Forward_Click(object sender, RoutedEventArgs e) { - - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - } - App.ViewModel.FilesAndFolders.Clear(); - + App.ViewModel.CancelLoadAndClearFiles(); if (History.ForwardList.Count() > 0) { App.ViewModel.TextState.isVisible = Visibility.Collapsed; - App.ViewModel.FilesAndFolders.Clear(); - + App.ViewModel.CancelLoadAndClearFiles(); if ((History.ForwardList[History.ForwardList.Count() - 1]) == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { @@ -313,12 +302,9 @@ public async static void Refresh_Click(object sender, RoutedEventArgs e) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - } + App.ViewModel.CancelLoadAndClearFiles(); + App.ViewModel.TextState.isVisible = Visibility.Collapsed; - App.ViewModel.FilesAndFolders.Clear(); App.ViewModel.AddItemsToCollectionAsync(App.ViewModel.Universal.path, GenericFileBrowser.GFBPageName); if ((History.HistoryList[History.HistoryList.Count - 1]) == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { diff --git a/Files UWP/Navigation/PhotoAlbumNavActions.cs b/Files UWP/Navigation/PhotoAlbumNavActions.cs index f84c8741fb6d..c1c630b627b9 100644 --- a/Files UWP/Navigation/PhotoAlbumNavActions.cs +++ b/Files UWP/Navigation/PhotoAlbumNavActions.cs @@ -13,20 +13,14 @@ public class PhotoAlbumNavActions { public static void Back_Click(object sender, RoutedEventArgs e) { - - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - } - App.ViewModel.FilesAndFolders.Clear(); - + App.ViewModel.CancelLoadAndClearFiles(); if (History.HistoryList.Count() > 1) { App.ViewModel.TextState.isVisible = Visibility.Collapsed; History.AddToForwardList(History.HistoryList[History.HistoryList.Count() - 1]); History.HistoryList.RemoveAt(History.HistoryList.Count() - 1); - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); if ((History.HistoryList[History.HistoryList.Count - 1]) == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { @@ -161,17 +155,12 @@ public static void Back_Click(object sender, RoutedEventArgs e) public static void Forward_Click(object sender, RoutedEventArgs e) { - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - } - App.ViewModel.FilesAndFolders.Clear(); + App.ViewModel.CancelLoadAndClearFiles(); if (History.ForwardList.Count() > 0) { App.ViewModel.TextState.isVisible = Visibility.Collapsed; - App.ViewModel.FilesAndFolders.Clear(); - + App.ViewModel.CancelLoadAndClearFiles(); if ((History.ForwardList[History.ForwardList.Count() - 1]) == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { @@ -311,12 +300,9 @@ public async static void Refresh_Click(object sender, RoutedEventArgs e) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { - if(App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - } + App.ViewModel.CancelLoadAndClearFiles(); + App.ViewModel.TextState.isVisible = Visibility.Collapsed; - App.ViewModel.FilesAndFolders.Clear(); App.ViewModel.AddItemsToCollectionAsync(App.PathText.Text, PhotoAlbum.PAPageName); if ((History.HistoryList[History.HistoryList.Count - 1]) == Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) { diff --git a/Files UWP/PhotoAlbum.xaml.cs b/Files UWP/PhotoAlbum.xaml.cs index 040b3341259c..f0168ea30681 100644 --- a/Files UWP/PhotoAlbum.xaml.cs +++ b/Files UWP/PhotoAlbum.xaml.cs @@ -129,11 +129,7 @@ private async void VisiblePath_TextChanged(object sender, KeyRoutedEventArgs e) var CurrentInput = PathBox.Text; if (CurrentInput != App.ViewModel.Universal.path) { - if (App.ViewModel.tokenSource != null) - { - App.ViewModel.tokenSource.Cancel(); - App.ViewModel.FilesAndFolders.Clear(); - } + App.ViewModel.CancelLoadAndClearFiles(); if (CurrentInput == "Home" || CurrentInput == "home") {