Skip to content

Commit

Permalink
Add cancellation token
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Nov 6, 2023
1 parent d32bc55 commit 27bc61c
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions AniStream/ViewModels/EpisodeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AniStream.Services;
using AniStream.Utils;
Expand Down Expand Up @@ -65,6 +66,12 @@ public partial class EpisodeViewModel : CollectionViewModel<Episode>, IQueryAttr

private bool IsSavingFavorite { get; set; }

private bool IsProviderSearchSheetShowing { get; set; }

private readonly CancellationTokenSource _cancellationTokenSource = new();

public CancellationToken CancellationToken => _cancellationTokenSource.Token;

public EpisodeViewModel(AniClient aniClient)
{
_anilistClient = aniClient;
Expand All @@ -86,6 +93,14 @@ public EpisodeViewModel(AniClient aniClient)
if (e.PropertyName == nameof(IsDubSelected))
IsDubSelectedChanged();
};

Shell.Current.Navigating += Current_Navigating;
}

private void Current_Navigating(object? sender, ShellNavigatingEventArgs e)
{
Shell.Current.Navigating -= Current_Navigating;
_cancellationTokenSource.Cancel();
}

private async void IsDubSelectedChanged()
Expand Down Expand Up @@ -128,6 +143,10 @@ protected override async Task LoadCore()
{
// Find best match
Anime = await TryFindBestAnime();

if (CancellationToken.IsCancellationRequested)
return;

if (Anime is null)
{
await Toast.Make("Nothing found").Show();
Expand All @@ -137,9 +156,12 @@ protected override async Task LoadCore()

await LoadEpisodes(Anime);
}
catch (Exception e)
catch
{
SearchingText = "Nothing Found";
if (!CancellationToken.IsCancellationRequested)
{
SearchingText = "Nothing Found";
}
}
finally
{
Expand All @@ -166,7 +188,7 @@ public async Task LoadEpisodes(IAnimeInfo anime)

try
{
var result = await _provider.GetEpisodesAsync(anime.Id);
var result = await _provider.GetEpisodesAsync(anime.Id, CancellationToken);
if (result.Count == 0)
return;

Expand Down Expand Up @@ -270,13 +292,22 @@ private void RefreshEpisodesProgress()

SearchingText = $"Searching : {Entity.Title?.PreferredTitle}" + dubText;

var result = await _provider.SearchAsync(Entity.Title.RomajiTitle + dubText);
var result = await _provider.SearchAsync(
Entity.Title.RomajiTitle + dubText,
CancellationToken
);

if (result.Count == 0)
result = await _provider.SearchAsync(Entity.Title.NativeTitle + dubText);
result = await _provider.SearchAsync(
Entity.Title.NativeTitle + dubText,
CancellationToken
);

if (result.Count == 0)
result = await _provider.SearchAsync(Entity.Title.EnglishTitle + dubText);
result = await _provider.SearchAsync(
Entity.Title.EnglishTitle + dubText,
CancellationToken
);

return result.FirstOrDefault();
}
Expand Down Expand Up @@ -431,12 +462,20 @@ private void ChangeGridMode(GridLayoutMode gridLayoutMode)
[RelayCommand]
private async Task ShowProviderSearch()
{
if (IsProviderSearchSheetShowing)
return;

IsProviderSearchSheetShowing = true;

var sheet = new ProviderSearchSheet();
sheet.BindingContext = new ProviderSearchViewModel(
this,
sheet,
Entity.Title.PreferredTitle
);

sheet.Dismissed += (_, _) => IsProviderSearchSheetShowing = false;

await sheet.ShowAsync();
}

Expand All @@ -450,4 +489,6 @@ public void ApplyQueryAttributes(IDictionary<string, object> query)

RefreshIsFavorite();
}

public void Cancel() => _cancellationTokenSource.Cancel();
}

0 comments on commit 27bc61c

Please sign in to comment.