Skip to content

Commit

Permalink
Fix showing loading, when it doesn't have to
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Jan 31, 2025
1 parent 92fbb40 commit c335561
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
37 changes: 23 additions & 14 deletions src/PicView.Avalonia/Navigation/ImageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,20 @@ public async Task NextIteration(NavigateTo navigateTo, CancellationTokenSource c
}
}

/// <summary>
/// Iterates to the given index in the image list, shows the corresponding image and preloads the next/previous images.
/// </summary>
/// <param name="index">The index to iterate to.</param>
/// <param name="cts">The cancellation token source.</param>
/// <returns>A <see cref="Task" /> that represents the asynchronous operation.</returns>
public async Task IterateToIndex(int index, CancellationTokenSource cts)
{
if (index < 0 || index >= ImagePaths.Count)
{
ErrorHandling.ShowStartUpMenu(_vm);
return;
}

try
{
CurrentIndex = index;
Expand All @@ -508,20 +514,21 @@ public async Task IterateToIndex(int index, CancellationTokenSource cts)
var preloadValue = PreLoader.Get(index, ImagePaths);
if (preloadValue is not null)
{
if (preloadValue.IsLoading)
// Wait for image to load
if (preloadValue is { IsLoading: true, ImageModel.Image: not null })
{
TryShowPreview();
}

while (preloadValue.IsLoading)
{
await Task.Delay(20, cts.Token).ConfigureAwait(false);
if (CurrentIndex != index)
do
{
// Skip loading if user went to next value
await cts.CancelAsync();
return;
}
await Task.Delay(20, cts.Token).ConfigureAwait(false);
if (CurrentIndex != index)
{
// Skip loading if user went to next value
await cts.CancelAsync();
return;
}
} while (preloadValue.IsLoading);
}
}
else
Expand Down Expand Up @@ -555,7 +562,8 @@ public async Task IterateToIndex(int index, CancellationTokenSource cts)

if (!cts.IsCancellationRequested)
{
await UpdateImage.UpdateSource(_vm, index, ImagePaths, IsReversed, preloadValue, nextPreloadValue)
await UpdateImage.UpdateSource(_vm, index, ImagePaths, IsReversed, preloadValue,
nextPreloadValue)
.ConfigureAwait(false);
}
}
Expand Down Expand Up @@ -594,7 +602,7 @@ await PreLoader.PreLoadAsync(CurrentIndex, IsReversed, ImagePaths)
{
// Ignore
#if DEBUG
Trace.WriteLine($"{nameof(IterateToIndex)} canceled");
Trace.WriteLine($"\n{nameof(IterateToIndex)} canceled\n");
#endif
}
catch (Exception e)
Expand Down Expand Up @@ -622,6 +630,7 @@ void TryShowPreview()

private static Timer? _timer;


private async Task TimerIteration(int index, CancellationTokenSource cts)
{
if (_timer is null)
Expand Down Expand Up @@ -687,4 +696,4 @@ private void Dispose(bool disposing)
}

#endregion
}
}
24 changes: 18 additions & 6 deletions src/PicView.Avalonia/Navigation/NavigationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,32 +610,44 @@ public static async Task LoadPicFromDirectoryAsync(string file, MainViewModel vm

#region Private helpers

/// <summary>
/// Checks if the previous iteration has been cancelled and starts the next iteration in a new task.
/// </summary>
/// <param name="navigateTo">The direction to navigate.</param>
/// <param name="vm">The main view model instance.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private static async Task CheckCancellationAndStartNextIteration(NavigateTo navigateTo, MainViewModel vm)
{
await Task.Run(async () =>
await Task.Run(() =>
{
if (_cancellationTokenSource is not null)
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
_ = _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.NextIteration(navigateTo, _cancellationTokenSource).ConfigureAwait(false);
_ = vm.ImageIterator.NextIteration(navigateTo, _cancellationTokenSource).ConfigureAwait(false);
_cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5));
}).ConfigureAwait(false);
}

/// <summary>
/// Checks if the previous iteration has been cancelled and starts the iteration at the given index in a new task.
/// </summary>
/// <param name="index">The index to iterate to.</param>
/// <param name="vm">The main view model instance.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private static async Task CheckCancellationAndStartIterateToIndex(int index, MainViewModel vm)
{
await Task.Run(async () =>
await Task.Run(() =>
{
if (_cancellationTokenSource is not null)
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
_ = _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.IterateToIndex(index, _cancellationTokenSource).ConfigureAwait(false);
_ = vm.ImageIterator.IterateToIndex(index, _cancellationTokenSource).ConfigureAwait(false);
_cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5));
}).ConfigureAwait(false);
}
Expand Down

0 comments on commit c335561

Please sign in to comment.