From 92887e2e533b60cd7a8e5442eb0585aad398db38 Mon Sep 17 00:00:00 2001 From: Insomniachi Date: Wed, 6 Dec 2023 21:43:11 +0530 Subject: [PATCH] - fix not able to move window - use simkl for episode metadata - fix simkl tracking not selectable in settings --- Totoro.Core/Contracts/ISimklService.cs | 1 + Totoro.Core/Services/Simkl/SimklService.cs | 64 +++++++++++++++---- Totoro.Core/ViewModels/SettingsViewModel.cs | 2 +- Totoro.Core/ViewModels/WatchViewModel.cs | 10 +-- .../ViewModels/SubmitTimeStampsViewModel.cs | 23 ++----- .../Dialogs/Views/SubmitTimeStampsView.xaml | 10 +-- Totoro.WinUI/Services/ViewService.cs | 14 +++- Totoro.WinUI/Views/ShellPage.xaml.cs | 4 -- 8 files changed, 79 insertions(+), 49 deletions(-) diff --git a/Totoro.Core/Contracts/ISimklService.cs b/Totoro.Core/Contracts/ISimklService.cs index 46693ab1..44127187 100644 --- a/Totoro.Core/Contracts/ISimklService.cs +++ b/Totoro.Core/Contracts/ISimklService.cs @@ -5,4 +5,5 @@ namespace Totoro.Core.Contracts; public interface ISimklService { Task GetId(ListServiceType type, long id); + Task> GetEpisodes(long id); } diff --git a/Totoro.Core/Services/Simkl/SimklService.cs b/Totoro.Core/Services/Simkl/SimklService.cs index f02074c2..067c5ed8 100644 --- a/Totoro.Core/Services/Simkl/SimklService.cs +++ b/Totoro.Core/Services/Simkl/SimklService.cs @@ -5,25 +5,18 @@ namespace Totoro.Core.Services.Simkl; internal class SimklService : ISimklService { private readonly ISimklClient _simklClient; + private readonly ISettings _settings; - public SimklService(ISimklClient simklClient) + public SimklService(ISimklClient simklClient, + ISettings settings) { _simklClient = simklClient; + _settings = settings; } public async Task GetId(ListServiceType type, long id) { - var serviceType = type switch - { - ListServiceType.MyAnimeList => "mal", - ListServiceType.AniList => "anilist", - ListServiceType.Kitsu => "kitsu", - ListServiceType.AniDb => "anidb", - ListServiceType.Simkl => "simkl", - _ => throw new UnreachableException() - }; - - var response = await _simklClient.Search(serviceType, id); + var response = await _simklClient.Search(GetServiceType(type), id); if (response.FirstOrDefault() is not { Id.Simkl: not null } metaData) { return null; @@ -61,4 +54,51 @@ private static AnimeIdExtended ToExtendedId(SimklIds ids) return extended; } + + public async Task> GetEpisodes(long id) + { + id = await GetSimklId(id); + + if(id == 0) + { + return Enumerable.Empty(); + } + + var episodes = await _simklClient.GetEpisodes(id); + + return episodes.Select(x => new EpisodeModel + { + EpisodeNumber = x.EpisodeNumber, + EpisodeTitle = x.Title + }); + } + + private async ValueTask GetSimklId(long id) + { + if(_settings.DefaultListService == ListServiceType.Simkl) + { + return id; + } + + var response = await _simklClient.Search(GetServiceType(_settings.DefaultListService), id); + if (response.FirstOrDefault() is not { Id.Simkl: not null } metaData) + { + return 0; + } + + return metaData.Id.Simkl ?? 0; + } + + private static string GetServiceType(ListServiceType type) + { + return type switch + { + ListServiceType.MyAnimeList => "mal", + ListServiceType.AniList => "anilist", + ListServiceType.Kitsu => "kitsu", + ListServiceType.AniDb => "anidb", + ListServiceType.Simkl => "simkl", + _ => throw new UnreachableException() + }; + } } diff --git a/Totoro.Core/ViewModels/SettingsViewModel.cs b/Totoro.Core/ViewModels/SettingsViewModel.cs index 11a7f61e..0c7bb23c 100644 --- a/Totoro.Core/ViewModels/SettingsViewModel.cs +++ b/Totoro.Core/ViewModels/SettingsViewModel.cs @@ -34,7 +34,7 @@ public class SettingsViewModel : NavigatableViewModel public IEnumerable MangaProviderTypes => PluginFactory.Instance.Plugins; public IEnumerable ListDisplayModes { get; } = Enum.GetValues().Cast().Take(2).ToList(); public List LogLevels { get; } = new List { LogLevel.Debug, LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Critical }; - public List ServiceTypes { get; } = new List { ListServiceType.MyAnimeList, ListServiceType.AniList }; + public List ServiceTypes { get; } = new List { ListServiceType.MyAnimeList, ListServiceType.AniList, ListServiceType.Simkl }; public List HomePages { get; } = new List { "Discover", "My List" }; public List AnimeActions { get; } = new List { "Watch", "Info" }; public List QualitySelections { get; } = Enum.GetValues().Cast().ToList(); diff --git a/Totoro.Core/ViewModels/WatchViewModel.cs b/Totoro.Core/ViewModels/WatchViewModel.cs index b3d9b927..c5a1e68a 100644 --- a/Totoro.Core/ViewModels/WatchViewModel.cs +++ b/Totoro.Core/ViewModels/WatchViewModel.cs @@ -25,7 +25,7 @@ public partial class WatchViewModel : NavigatableViewModel private readonly IAnimeServiceContext _animeService; private readonly IStreamPageMapper _streamPageMapper; private readonly IVideoStreamResolverFactory _videoStreamResolverFactory; - private readonly IMyAnimeListService _myAnimeListService; + private readonly ISimklService _simklService; private readonly IAnimeDetectionService _animeDetectionService; private readonly INameService _nameService; private readonly List _mediaEventListeners; @@ -47,8 +47,8 @@ public WatchViewModel(IPluginFactory providerFactory, IMediaPlayerFactory mediaPlayerFactory, IStreamPageMapper streamPageMapper, IVideoStreamResolverFactory videoStreamResolverFactory, + ISimklService simklService, IEnumerable mediaEventListeners, - IMyAnimeListService myAnimeListService, IAnimeDetectionService animeDetectionService, INameService nameService) { @@ -60,7 +60,7 @@ public WatchViewModel(IPluginFactory providerFactory, _animeService = animeService; _streamPageMapper = streamPageMapper; _videoStreamResolverFactory = videoStreamResolverFactory; - _myAnimeListService = myAnimeListService; + _simklService = simklService; _animeDetectionService = animeDetectionService; _nameService = nameService; _mediaEventListeners = mediaEventListeners.ToList(); @@ -97,7 +97,7 @@ public WatchViewModel(IPluginFactory providerFactory, this.ObservableForProperty(x => x.Anime, x => x) .WhereNotNull() - .Do(async model => _episodeMetadata ??= await myAnimeListService.GetEpisodes(model.Id)) + .Do(async model => _episodeMetadata ??= await simklService.GetEpisodes(model.Id)) .SelectMany(model => Find(model.Id, model.Title)) .Where(x => x is not (null, null)) .Log(this, "Selected Anime", x => $"{x.Sub.Title}") @@ -554,7 +554,7 @@ private void SetAnime(long id) .Subscribe(async anime => { _anime = anime; - _episodeMetadata = await _myAnimeListService.GetEpisodes(id); + _episodeMetadata = await _simklService.GetEpisodes(id); SetAnime(_anime); }, RxApp.DefaultExceptionHandler.OnError); } diff --git a/Totoro.WinUI/Dialogs/ViewModels/SubmitTimeStampsViewModel.cs b/Totoro.WinUI/Dialogs/ViewModels/SubmitTimeStampsViewModel.cs index 868ca869..1d4052f9 100644 --- a/Totoro.WinUI/Dialogs/ViewModels/SubmitTimeStampsViewModel.cs +++ b/Totoro.WinUI/Dialogs/ViewModels/SubmitTimeStampsViewModel.cs @@ -11,7 +11,6 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService) { _timestampsService = timestampsService; - PlayRange = ReactiveCommand.Create(Play); SetStartPosition = ReactiveCommand.Create(() => StartPosition = MediaPlayer.GetMediaPlayer().Position.TotalSeconds); SetEndPosition = ReactiveCommand.Create(() => EndPosition = MediaPlayer.GetMediaPlayer().Position.TotalSeconds); SkipNearEnd = ReactiveCommand.Create(() => MediaPlayer.SeekTo(TimeSpan.FromSeconds(EndPosition - 5))); @@ -62,6 +61,9 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService) this.WhenAnyValue(x => x.Stream) .WhereNotNull() .Subscribe(async x => await MediaPlayer.SetMedia(x)); + + this.WhenAnyValue(x => x.StartPosition) + .Subscribe(pos => EndPosition = pos + 90); } [Reactive] public double StartPosition { get; set; } @@ -70,6 +72,7 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService) [Reactive] public double CurrentPlayerPosition { get; set; } [Reactive] public VideoStreamModel Stream { get; set; } [Reactive] public AniSkipResult ExistingResult { get; set; } + [Reactive] public string Status { get; set; } public long MalId { get; set; } public int Episode { get; set; } public double Duration { get; set; } @@ -77,8 +80,8 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService) public double SuggestedStartPosition { get; set; } public double SuggestedEndPosition => Duration - 120; public WinUIMediaPlayerWrapper MediaPlayer { get; } = App.GetService().Create(MediaPlayerType.WindowsMediaPlayer) as WinUIMediaPlayerWrapper; + public bool HandleClose { get; set; } - public ICommand PlayRange { get; } public ICommand SetStartPosition { get; } public ICommand SetEndPosition { get; } public ICommand SkipNearEnd { get; } @@ -86,20 +89,6 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService) public ICommand VoteUp { get; } public ICommand VoteDown { get; } - private void Play() - { - _subscription?.Dispose(); - MediaPlayer.Play(StartPosition); - - _subscription = this.WhenAnyValue(x => x.CurrentPlayerPosition) - .Where(time => time >= EndPosition) - .Subscribe(_ => - { - _subscription?.Dispose(); - MediaPlayer.Pause(); - }); - } - private void Vote(bool vote) { if (SelectedTimeStampType == "OP" && ExistingResult.Opening is { } op) @@ -114,7 +103,9 @@ private void Vote(bool vote) public async Task SubmitTimeStamp() { + HandleClose = true; await _timestampsService.SubmitTimeStamp(MalId, Episode, SelectedTimeStampType.ToLower(), new Interval { StartTime = StartPosition, EndTime = EndPosition }, Duration); + Status = $"{SelectedTimeStampType} timestamp submitted"; } } diff --git a/Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml b/Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml index 8dee937b..b04ff0e1 100644 --- a/Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml +++ b/Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml @@ -53,15 +53,7 @@