Skip to content

Commit

Permalink
show fillers in different color
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Mar 3, 2024
1 parent 907e911 commit 0eb4a17
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 31 deletions.
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/IMyAnimeListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public interface IMyAnimeListService
{
Task<IEnumerable<EpisodeModel>> GetEpisodes(long id);
IAsyncEnumerable<int> GetFillers(long id);
IObservable<IEnumerable<AnimeModel>> GetAiringAnime();
IObservable<IEnumerable<AnimeModel>> GetUpcomingAnime();
IObservable<IEnumerable<AnimeModel>> GetPopularAnime();
Expand Down
1 change: 1 addition & 0 deletions Totoro.Core/Models/EpisodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class EpisodeModel : ReactiveObject
[Reactive] public string SpecialEpisodeNumber { get; set; }
[Reactive] public bool IsSpecial { get; set; }
[Reactive] public string EpisodeTitle { get; set; }
[Reactive] public bool IsFillter { get; set; }
[ObservableAsProperty] public string DisplayName { get; }

public EpisodeModel()
Expand Down
52 changes: 37 additions & 15 deletions Totoro.Core/Services/MyAnimeList/MyAnimeListService.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using JikanDotNet;
using System.Collections.Generic;
using AngleSharp.Io;
using JikanDotNet;
using MalApi;
using MalApi.Interfaces;
using static Totoro.Core.Services.MyAnimeList.MalToModelConverter;

namespace Totoro.Core.Services.MyAnimeList;

public class MyAnimeListService : IAnimeService, IMyAnimeListService
public class MyAnimeListService(IMalClient client,
IAnilistService anilistService,
IAnimeIdService animeIdService,
ISettings settings) : IAnimeService, IMyAnimeListService
{
private readonly IMalClient _client;
private readonly IAnilistService _anilistService;
private readonly ISettings _settings;
private readonly IMalClient _client = client;
private readonly IAnilistService _anilistService = anilistService;
private readonly IAnimeIdService _animeIdService = animeIdService;
private readonly ISettings _settings = settings;
private readonly IJikan _jikan = new Jikan();
private readonly string _recursiveAnimeProperties = $"my_list_status,status,{AnimeFieldNames.TotalEpisodes},{AnimeFieldNames.Mean}";

public MyAnimeListService(IMalClient client,
IAnilistService anilistService,
ISettings settings)
{
_client = client;
_anilistService = anilistService;
_settings = settings;
}

public ListServiceType Type => ListServiceType.MyAnimeList;

public IObservable<AnimeModel> GetInformation(long id)
Expand Down Expand Up @@ -159,10 +156,35 @@ public async Task<IEnumerable<EpisodeModel>> GetEpisodes(long id)
}
catch
{
return Enumerable.Empty<EpisodeModel>();
return [];
}
}

public async IAsyncEnumerable<int> GetFillers(long id)
{
var animeId = await _animeIdService.GetId(id);

if (animeId is null || animeId.MyAnimeList is null)
{
yield break;
}

PaginatedJikanResponse<ICollection<AnimeEpisode>> response = null;
var currentPage = 1;
var offset = 0;
do
{
response = await _jikan.GetAnimeEpisodesAsync(animeId.MyAnimeList.Value, currentPage++);
foreach (var item in response.Data.Select((x, index) => (x, index)).Where(x => x.x.Filler == true).Select(x => x.index + 1 + offset))
{
yield return item;
}

offset += response.Data.Count;

} while (response.Pagination.HasNextPage);
}

private static MalApi.Season PrevSeason()
{
var date = DateTime.Now;
Expand Down
57 changes: 42 additions & 15 deletions Totoro.Core/ViewModels/WatchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public partial class WatchViewModel : NavigatableViewModel
private readonly ISimklService _simklService;
private readonly IAnimeDetectionService _animeDetectionService;
private readonly IAnimePreferencesService _preferencesService;
private readonly IMyAnimeListService _myAnimeListService;
private readonly List<IMediaEventListener> _mediaEventListeners;
private readonly string[] _subDubProviders = ["gogo-anime", "anime-saturn"];

Expand All @@ -64,7 +65,8 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,
ISimklService simklService,
IEnumerable<IMediaEventListener> mediaEventListeners,
IAnimeDetectionService animeDetectionService,
IAnimePreferencesService preferencesService)
IAnimePreferencesService preferencesService,
IMyAnimeListService myAnimeListService)
{
_providerFactory = providerFactory;
_viewService = viewService;
Expand All @@ -77,6 +79,7 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,
_simklService = simklService;
_animeDetectionService = animeDetectionService;
_preferencesService = preferencesService;
_myAnimeListService = myAnimeListService;
_mediaEventListeners = mediaEventListeners.ToList();

NextEpisode = ReactiveCommand.Create(() =>
Expand Down Expand Up @@ -115,7 +118,7 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,

this.ObservableForProperty(x => x.Anime, x => x)
.WhereNotNull()
.Do(async model => _episodeMetadata ??= await simklService.GetEpisodes(model.Id))
.Do(async model => await UpdateMetaData(model.Id))
.SelectMany(model => Find(model.Id, model.Title))
.Where(x => x is not (null, null))
.Log(this, "Selected Anime", x => $"{x.Sub.Title}")
Expand Down Expand Up @@ -154,18 +157,7 @@ public WatchViewModel(IPluginFactory<AnimeProvider> providerFactory,
this.WhenAnyValue(x => x.EpisodeModels)
.Where(x => x is not null && Anime is not null && _episodeMetadata is not null)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ =>
{
foreach (var item in _episodeMetadata)
{
if (EpisodeModels.FirstOrDefault(x => x.EpisodeNumber == item.EpisodeNumber) is not { } ep)
{
continue;
}

ep.EpisodeTitle = item.EpisodeTitle;
}
});
.Subscribe(_ => UpdateMetaData());

this.WhenAnyValue(x => x.EpisodeModels)
.WhereNotNull()
Expand Down Expand Up @@ -631,7 +623,7 @@ private void SetAnime(long id)
.Subscribe(async anime =>
{
_anime = anime;
_episodeMetadata = await _simklService.GetEpisodes(id);
await UpdateMetaData(id);
SetAnime(_anime);
}, RxApp.DefaultExceptionHandler.OnError);
}
Expand Down Expand Up @@ -760,4 +752,39 @@ private Task<TimestampResult> GetTimeStamps(TimeSpan duration)
? _timestampsService.GetTimeStampsWithMalId(malId, EpisodeModels.Current.EpisodeNumber, duration.TotalSeconds)
: _timestampsService.GetTimeStampsWithMalId(Anime.Id, EpisodeModels.Current.EpisodeNumber, duration.TotalSeconds);
}

private void UpdateMetaData()
{
foreach (var item in _episodeMetadata)
{
if (EpisodeModels.FirstOrDefault(x => x.EpisodeNumber == item.EpisodeNumber) is not { } ep)
{
continue;
}

ep.EpisodeTitle = item.EpisodeTitle;
ep.IsFillter = item.IsFillter;
}
}

private async Task UpdateMetaData(long id)
{
var meta = (await _simklService.GetEpisodes(id)).ToList();
var fillers = await _myAnimeListService.GetFillers(Anime.Id).ToListAsync();

foreach (var item in meta)
{
if(fillers.Contains(item.EpisodeNumber))
{
item.IsFillter = true;
}
}

_episodeMetadata = meta;

if(EpisodeModels is not null)
{
UpdateMetaData();
}
}
}
1 change: 1 addition & 0 deletions Totoro.WinUI/Views/WatchPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<ListView.ItemTemplate>
<DataTemplate x:DataType="m:EpisodeModel">
<TextBlock
Foreground="{x:Bind local:WatchPage.Foreground(IsFillter), Mode=OneWay}"
Text="{x:Bind DisplayName, Mode=OneWay}"
TextTrimming="CharacterEllipsis"
ToolTipService.ToolTip="{x:Bind DisplayName, Mode=OneWay}" />
Expand Down
10 changes: 9 additions & 1 deletion Totoro.WinUI/Views/WatchPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Data;
using Microsoft.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using ReactiveMarbles.ObservableEvents;
using Totoro.Core.ViewModels;
using Totoro.WinUI.Contracts;
using Totoro.WinUI.Media.Flyleaf;
using Totoro.WinUI.Media.Wmp;
using Totoro.WinUI.UserControls;
using WinUIEx;

namespace Totoro.WinUI.Views;

Expand Down Expand Up @@ -143,6 +144,13 @@ private void EnterPiPMode()
};
}

public static SolidColorBrush Foreground(bool isFiller)
{
return isFiller
? new SolidColorBrush(Colors.Yellow)
: new SolidColorBrush(Colors.White);
}

private string GetWindowTitle()
{
if(ViewModel.Anime is null)
Expand Down

0 comments on commit 0eb4a17

Please sign in to comment.