Skip to content

Commit

Permalink
Use cancellation tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Nov 4, 2023
1 parent b38b69b commit 0b4eb5b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
16 changes: 13 additions & 3 deletions AniStream/ViewModels/ProviderSearchViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AniStream.Utils;
using AniStream.ViewModels.Framework;
using Berry.Maui.Controls;
using CommunityToolkit.Mvvm.Input;
using Juro.Core.Models.Anime;
using Juro.Core.Providers;
using Berry.Maui.Controls;

namespace AniStream.ViewModels;

Expand All @@ -15,6 +16,10 @@ public partial class ProviderSearchViewModel : CollectionViewModel<IAnimeInfo>
private readonly BottomSheet _bottomSheet;
private readonly EpisodeViewModel _episodeViewModel;

private readonly CancellationTokenSource _cancellationTokenSource = new();

public CancellationToken CancellationToken => _cancellationTokenSource.Token;

public ProviderSearchViewModel(
EpisodeViewModel episodeViewModel,
BottomSheet bottomSheet,
Expand Down Expand Up @@ -46,13 +51,16 @@ protected override async Task LoadCore()

try
{
var result = await _provider.SearchAsync(Query);
var result = await _provider.SearchAsync(Query, CancellationToken);
Push(result);
Offset += result.Count;
}
catch (Exception ex)
{
await App.AlertService.ShowAlertAsync("Error", ex.ToString());
if (!CancellationToken.IsCancellationRequested)
{
await App.AlertService.ShowAlertAsync("Error", ex.ToString());
}
}
finally
{
Expand All @@ -67,4 +75,6 @@ async Task ItemSelected(IAnimeInfo item)
await _bottomSheet.DismissAsync();
await _episodeViewModel.LoadEpisodes(item);
}

public void Cancel() => _cancellationTokenSource.Cancel();
}
8 changes: 7 additions & 1 deletion AniStream/Views/BottomSheets/ProviderSearchSheet.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Berry.Maui;
using AniStream.ViewModels;
using Berry.Maui;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Devices;
Expand All @@ -25,6 +26,11 @@ public ProviderSearchSheet()
{
SearchEntry.Focused -= SearchEntry_Focused;
SearchEntry.Completed -= SearchEntry_Completed;

if (BindingContext is ProviderSearchViewModel viewModel)
{
viewModel.Cancel();
}
};

var statusBarHeight =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:BaseBottomSheetView
x:Class="AniStream.Views.BottomSheets.EpisodeSelectionSheet"
x:Class="AniStream.Views.BottomSheets.VideoSourceSheet"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:berry="https://schemas.jerry08/dotnet/2023/maui"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using Microsoft.Maui.Devices;
using AniStream.ViewModels;
using Microsoft.Maui.Devices;

namespace AniStream.Views.BottomSheets;

public partial class EpisodeSelectionSheet
public partial class VideoSourceSheet
{
public EpisodeSelectionSheet()
public VideoSourceSheet()
{
InitializeComponent();

Shown += (_, _) => DeviceDisplay.Current.MainDisplayInfoChanged += OnMainDisplayInfoChanged;

Dismissed += (_, _) =>
{
DeviceDisplay.Current.MainDisplayInfoChanged -= OnMainDisplayInfoChanged;

if (BindingContext is VideoSourceViewModel viewModel)
{
viewModel.Cancel();
}
};

//this.UpdateChildrenLayout();
}

Expand Down

0 comments on commit 0b4eb5b

Please sign in to comment.