diff --git a/AniStream/ViewModels/ProviderSearchViewModel.cs b/AniStream/ViewModels/ProviderSearchViewModel.cs index f1d4f5a..13da399 100644 --- a/AniStream/ViewModels/ProviderSearchViewModel.cs +++ b/AniStream/ViewModels/ProviderSearchViewModel.cs @@ -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; @@ -15,6 +16,10 @@ public partial class ProviderSearchViewModel : CollectionViewModel private readonly BottomSheet _bottomSheet; private readonly EpisodeViewModel _episodeViewModel; + private readonly CancellationTokenSource _cancellationTokenSource = new(); + + public CancellationToken CancellationToken => _cancellationTokenSource.Token; + public ProviderSearchViewModel( EpisodeViewModel episodeViewModel, BottomSheet bottomSheet, @@ -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 { @@ -67,4 +75,6 @@ async Task ItemSelected(IAnimeInfo item) await _bottomSheet.DismissAsync(); await _episodeViewModel.LoadEpisodes(item); } + + public void Cancel() => _cancellationTokenSource.Cancel(); } diff --git a/AniStream/Views/BottomSheets/ProviderSearchSheet.xaml.cs b/AniStream/Views/BottomSheets/ProviderSearchSheet.xaml.cs index e1dbf47..7746949 100644 --- a/AniStream/Views/BottomSheets/ProviderSearchSheet.xaml.cs +++ b/AniStream/Views/BottomSheets/ProviderSearchSheet.xaml.cs @@ -1,4 +1,5 @@ -using Berry.Maui; +using AniStream.ViewModels; +using Berry.Maui; using Microsoft.Maui; using Microsoft.Maui.Controls; using Microsoft.Maui.Devices; @@ -25,6 +26,11 @@ public ProviderSearchSheet() { SearchEntry.Focused -= SearchEntry_Focused; SearchEntry.Completed -= SearchEntry_Completed; + + if (BindingContext is ProviderSearchViewModel viewModel) + { + viewModel.Cancel(); + } }; var statusBarHeight = diff --git a/AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml b/AniStream/Views/BottomSheets/VideoSourceSheet.xaml similarity index 99% rename from AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml rename to AniStream/Views/BottomSheets/VideoSourceSheet.xaml index 77a1eb8..a155d39 100644 --- a/AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml +++ b/AniStream/Views/BottomSheets/VideoSourceSheet.xaml @@ -1,6 +1,6 @@  DeviceDisplay.Current.MainDisplayInfoChanged += OnMainDisplayInfoChanged; Dismissed += (_, _) => + { DeviceDisplay.Current.MainDisplayInfoChanged -= OnMainDisplayInfoChanged; + if (BindingContext is VideoSourceViewModel viewModel) + { + viewModel.Cancel(); + } + }; + //this.UpdateChildrenLayout(); }