Skip to content

Display already loaded entries while loading files in a directory #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<DependentUpon>ConfirmDeleteDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Helpers\AppTheme.cs" />
<Compile Include="Helpers\DispatcherHelper.cs" />
<Compile Include="Helpers\ItemsDataTemplateSelector.cs" />
<Compile Include="Helpers\NaturalStringComparer.cs" />
<Compile Include="Helpers\StringExtensions.cs" />
Expand Down
82 changes: 82 additions & 0 deletions Files/Helpers/DispatcherHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Runtime.CompilerServices;
using Windows.UI.Core;

namespace Files.Helpers
{
/// <summary>
/// This class provides static methods helper for executing code in UI thread of the main window.
/// </summary>
static class DispatcherHelper
{ /// <summary>
/// This struct represents an awaitable dispatcher.
/// </summary>
public struct DispatcherPriorityAwaitable
{
private readonly CoreDispatcher dispatcher;
private readonly CoreDispatcherPriority priority;

internal DispatcherPriorityAwaitable(CoreDispatcher dispatcher, CoreDispatcherPriority priority)
{
this.dispatcher = dispatcher;
this.priority = priority;
}

/// <summary>
/// Get awaiter of DispatcherPriorityAwaiter
/// </summary>
/// <returns>Awaiter of DispatcherPriorityAwaiter</returns>
public DispatcherPriorityAwaiter GetAwaiter()
{
return new DispatcherPriorityAwaiter(this.dispatcher, this.priority);
}
}

/// <summary>
/// This struct represents the awaiter of a dispatcher.
/// </summary>
public struct DispatcherPriorityAwaiter : INotifyCompletion
{
private readonly CoreDispatcher dispatcher;
private readonly CoreDispatcherPriority priority;

/// <summary>
/// Gets a value indicating whether task has completed
/// </summary>
public bool IsCompleted => false;

internal DispatcherPriorityAwaiter(CoreDispatcher dispatcher, CoreDispatcherPriority priority)
{
this.dispatcher = dispatcher;
this.priority = priority;
}

/// <summary>
/// Get result for this awaiter
/// </summary>
public void GetResult()
{
}

/// <summary>
/// Fired once task has complated for notify completion
/// </summary>
/// <param name="continuation">Continuation action</param>
public async void OnCompleted(Action continuation)
{
await this.dispatcher.RunAsync(this.priority, new DispatchedHandler(continuation));
}
}

/// <summary>
/// Yield and allow UI update during tasks.
/// </summary>
/// <param name="dispatcher">Dispatcher of a thread to yield</param>
/// <param name="priority">Dispatcher execution priority, default is low</param>
/// <returns>Awaitable dispatcher task</returns>
public static DispatcherPriorityAwaitable YieldAsync(this CoreDispatcher dispatcher, CoreDispatcherPriority priority = CoreDispatcherPriority.Low)
{
return new DispatcherPriorityAwaitable(dispatcher, priority);
}
}
}
30 changes: 26 additions & 4 deletions Files/View Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace Files.Filesystem
{
public class ItemViewModel : INotifyPropertyChanged
public class ItemViewModel : INotifyPropertyChanged, IDisposable
{
public EmptyFolderTextState EmptyTextState { get; set; } = new EmptyFolderTextState();
public LoadingIndicator LoadIndicator { get; set; } = new LoadingIndicator();
Expand Down Expand Up @@ -448,9 +448,16 @@ public void OrderFiles()
ordered = ordered.ThenByDescending(orderByNameFunc, naturalStringComparer);
}
orderedList = ordered.ToList();
_filesAndFolders.Clear();
foreach (ListedItem i in orderedList)
_filesAndFolders.Add(i);

List<ListedItem> originalList = _filesAndFolders.ToList();
for (var i = 0; i < originalList.Count; i++)
{
if (originalList[i] != orderedList[i])
{
_filesAndFolders.RemoveAt(i);
_filesAndFolders.Insert(i, orderedList[i]);
}
}
}

public static T GetCurrentSelectedTabInstance<T>()
Expand Down Expand Up @@ -742,6 +749,15 @@ public async Task RapidAddItemsToCollectionAsync(string path)
}
}
}
if (_cancellationTokenSource.IsCancellationRequested)
{
break;
}

if (count % 64 == 0)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.YieldAsync();
}
} while (FindNextFile(hFile, out findData));

FindClose(hFile);
Expand All @@ -751,6 +767,7 @@ public async Task RapidAddItemsToCollectionAsync(string path)
{
if (_cancellationTokenSource.IsCancellationRequested)
{
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
IsLoadingItems = false;
return;
Expand Down Expand Up @@ -1139,5 +1156,10 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public void Dispose()
{
_cancellationTokenSource?.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion Files/View Models/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private async void LoadTerminalApps()
// await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(terminalsFileModel, Formatting.Indented));
// }
//}
Terminals = terminalsFileModel.Terminals;
Terminals = terminalsFileModel?.Terminals ?? new List<TerminalModel>();
}

private IList<TerminalModel> _Terminals = null;
Expand Down