diff --git a/AniStream/Models/Range.cs b/AniStream/Models/Range.cs index 16fea2b..cf28ba0 100644 --- a/AniStream/Models/Range.cs +++ b/AniStream/Models/Range.cs @@ -1,14 +1,18 @@ using System.Collections.Generic; +using CommunityToolkit.Mvvm.ComponentModel; using Juro.Core.Models.Anime; namespace AniStream.Models; -public class Range +public partial class Range : ObservableObject { public string Name { get; set; } = default!; public List Episodes { get; set; } = new(); + [ObservableProperty] + private bool _isSelected; + public Range(IEnumerable episodes, int startIndex, int endIndex) { Episodes.AddRange(episodes); diff --git a/AniStream/ViewModels/EpisodeViewModel.cs b/AniStream/ViewModels/EpisodeViewModel.cs index bc260d9..a32dc28 100644 --- a/AniStream/ViewModels/EpisodeViewModel.cs +++ b/AniStream/ViewModels/EpisodeViewModel.cs @@ -124,18 +124,20 @@ public async Task LoadEpisodes(IAnimeInfo anime) { var startIndex = 1; var endIndex = 0; + for (var i = 0; i < EpisodeChunks.Count; i++) { - var chunk = EpisodeChunks[i].ToList(); if (_settingsService.EpisodesDescending) { - chunk.Reverse(); + EpisodeChunks[i] = EpisodeChunks[i].Reverse().ToArray(); } - endIndex = startIndex + chunk.Count - 1; - ranges.Add(new Range(chunk, startIndex, endIndex)); - startIndex += chunk.Count; + endIndex = startIndex + EpisodeChunks[i].Length - 1; + ranges.Add(new Range(EpisodeChunks[i], startIndex, endIndex)); + startIndex += EpisodeChunks[i].Length; } + + ranges[0].IsSelected = true; } else { @@ -147,20 +149,9 @@ public async Task LoadEpisodes(IAnimeInfo anime) result.ForEach(ep => ep.Image = anime.Image); - foreach (var episode in result) - { - var episodeKey = $"{Entity.Id}-{episode.Number}"; - _playerSettings.WatchedEpisodes.TryGetValue(episodeKey, out var watchedEpisode); - if (watchedEpisode is not null) - { - episode.Progress = watchedEpisode.WatchedPercentage; - } - } - RefreshEpisodesProgress(); Entities.Push(EpisodeChunks[0]); - Ranges.Push(ranges); OnPropertyChanged(nameof(Ranges)); } @@ -168,6 +159,13 @@ public async Task LoadEpisodes(IAnimeInfo anime) [RelayCommand] private void RangeSelected(Range range) { + for (var i = 0; i < Ranges.Count; i++) + { + Ranges[i].IsSelected = false; + } + + range.IsSelected = true; + Entities.ReplaceRange(range.Episodes); } diff --git a/AniStream/ViewModels/Videos/VideoPlayerViewModel.cs b/AniStream/ViewModels/Videos/VideoPlayerViewModel.cs index 245bf33..afde31e 100644 --- a/AniStream/ViewModels/Videos/VideoPlayerViewModel.cs +++ b/AniStream/ViewModels/Videos/VideoPlayerViewModel.cs @@ -73,16 +73,34 @@ public VideoPlayerViewModel(IAnimeInfo anime, Episode episode, VideoSource? vide public VideoPlayerViewModel(IAnimeInfo anime, Episode episode, Media media) : this(anime, episode, null, media) { } + [RelayCommand] + public void OnLoaded(IMediaElement mediaElement) + { + Shell.Current.Navigating += Current_Navigating; + + MediaElement = mediaElement; + Controller = new(this, _anime, _episode, default!, _media); + Controller.OnLoaded(mediaElement); + } + + private void Current_Navigating(object? sender, ShellNavigatingEventArgs e) + { + _cancellationTokenSource.Cancel(); + UpdateProgress(); + } + [RelayCommand] private void OnUnloaded() { + Shell.Current.Navigating -= Current_Navigating; + Controller.Dispose(); ApplicationEx.SetOrientation(_initialOrientation); } [RelayCommand] - private async Task OnStateChanged(MediaElementState state) + private void OnStateChanged(MediaElementState state) { switch (state) { @@ -97,10 +115,10 @@ private async Task OnStateChanged(MediaElementState state) CanSaveProgress = true; break; case MediaElementState.Paused: - await UpdateProgress(); + UpdateProgress(); break; case MediaElementState.Stopped: - await UpdateProgress(); + UpdateProgress(); break; case MediaElementState.Failed: CanSaveProgress = false; @@ -110,14 +128,6 @@ private async Task OnStateChanged(MediaElementState state) } } - [RelayCommand] - public void OnLoaded(IMediaElement mediaElement) - { - MediaElement = mediaElement; - Controller = new(this, _anime, _episode, default!, _media); - Controller.OnLoaded(mediaElement); - } - public async Task ShowSpeedSelector() { var speeds = _playerSettings.GetSpeeds(); @@ -218,7 +228,7 @@ async Task ShowSheet() await sheet.ShowAsync(); } - public async Task UpdateProgress() + public void UpdateProgress() { if (!CanSaveProgress) return; diff --git a/AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml b/AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml index b793ab2..bc7f9b8 100644 --- a/AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml +++ b/AniStream/Views/BottomSheets/EpisodeSelectionSheet.xaml @@ -70,7 +70,7 @@ - +