Skip to content

Commit

Permalink
add discover my anime list section
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Jan 1, 2024
1 parent a50239c commit 517c1d6
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Totoro.Core/Contracts/IMyAnimeListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
public interface IMyAnimeListService
{
Task<IEnumerable<EpisodeModel>> GetEpisodes(long id);
IObservable<IEnumerable<AnimeModel>> GetAiringAnime();
IObservable<IEnumerable<AnimeModel>> GetUpcomingAnime();
IObservable<IEnumerable<AnimeModel>> GetPopularAnime();
IObservable<IEnumerable<AnimeModel>> GetRecommendedAnime();
}
29 changes: 26 additions & 3 deletions Totoro.Core/Services/MyAnimeList/MyAnimeListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ IGetSeasonalAnimeListRequest baseRequest(MalApi.Season season)
foreach (var season in new[] { current, prev, next })
{
var pagedAnime = await baseRequest(season).Find();
observer.OnNext(pagedAnime.Data.Select(MalToModelConverter.ConvertModel));
observer.OnNext(pagedAnime.Data.Select(ConvertModel));
}

observer.OnCompleted();
Expand All @@ -105,11 +105,34 @@ IGetSeasonalAnimeListRequest baseRequest(MalApi.Season season)
}


public IObservable<IEnumerable<AnimeModel>> GetAiringAnime()
public IObservable<IEnumerable<AnimeModel>> GetAiringAnime() => GetTopAnime(AnimeRankingType.Airing);
public IObservable<IEnumerable<AnimeModel>> GetUpcomingAnime() => GetTopAnime(AnimeRankingType.Upcoming);
public IObservable<IEnumerable<AnimeModel>> GetPopularAnime() => GetTopAnime(AnimeRankingType.ByPopularity);
public IObservable<IEnumerable<AnimeModel>> GetRecommendedAnime()
{
var request = _client
.Anime()
.Top(MalApi.AnimeRankingType.Airing)
.SuggestedForMe()
.WithLimit(15)
.IncludeNsfw()
.WithFields(_commonFields);

if (_settings.IncludeNsfw)
{
request.IncludeNsfw();
}

return request.Find()
.ToObservable()
.Select(x => x.Data.Select(x => ConvertModel(x)));
}

private IObservable<IEnumerable<AnimeModel>> GetTopAnime(AnimeRankingType type)
{
var request = _client
.Anime()
.Top(type)
.WithLimit(15)
.IncludeNsfw()
.WithFields(_commonFields);

Expand Down
34 changes: 34 additions & 0 deletions Totoro.Core/ViewModels/Discover/MyAnimeListDiscoverViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

namespace Totoro.Core.ViewModels.Discover;

public class MyAnimeListDiscoverViewModel : NavigatableViewModel
{
private readonly IMyAnimeListService _myAnimeListService;

public MyAnimeListDiscoverViewModel(IMyAnimeListService myAnimeListService)
{
_myAnimeListService = myAnimeListService;
}

[Reactive] public bool IsLoading { get; set; }
public ObservableCollection<NamedAnimeList> Lists { get; } = [];


public override async Task OnNavigatedTo(IReadOnlyDictionary<string, object> parameters)
{
IsLoading = true;

Lists.Add(new("Top Airing", await _myAnimeListService.GetAiringAnime()));
Lists.Add(new("Upcomming", await _myAnimeListService.GetUpcomingAnime()));
Lists.Add(new("Popular", await _myAnimeListService.GetPopularAnime()));
Lists.Add(new("Recommended", await _myAnimeListService.GetRecommendedAnime()));

IsLoading = false;
}
}

public class NamedAnimeList(string name, IEnumerable<AnimeModel> list)
{
public string Name { get; } = name;
public List<AnimeModel> List { get; } = list.ToList();
}
9 changes: 5 additions & 4 deletions Totoro.Core/ViewModels/DiscoverViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ namespace Totoro.Core.ViewModels;

public class DiscoverViewModel : NavigatableViewModel
{
public DiscoverViewModel()
public DiscoverViewModel(ISettings settings)
{

Sections.First(x => x.ViewModel == typeof(MyAnimeListDiscoverViewModel)).Visible = settings.DefaultListService == ListServiceType.MyAnimeList;
}

[Reactive] public PivotItemModel SelectedSection { get; set; }

public ObservableCollection<PivotItemModel> Sections { get; } =
[
new PivotItemModel { Header = "Recently Aired", ViewModel = typeof(RecentEpisodesViewModel) },
new PivotItemModel { Header = "Search" , ViewModel = typeof(SearchProviderViewModel) }
new () { Header = "Recently Aired", ViewModel = typeof(RecentEpisodesViewModel) },
new () { Header = "Discover", ViewModel = typeof(MyAnimeListDiscoverViewModel), Visible = false },
new () { Header = "Search" , ViewModel = typeof(SearchProviderViewModel) }
];
}
2 changes: 1 addition & 1 deletion Totoro.Installer/Components.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
WorkingDirectory="INSTALLFOLDER"/>
<RemoveFolder Id="DesktopFolder" On="uninstall"/>
<RegistryValue Root="HKCU"
Key="Software\$(loc.ProductFolder)"
Key="Software\!(loc.ProductFolder)"
Name="Shortcut"
Type="integer"
Value="1"
Expand Down
5 changes: 3 additions & 2 deletions Totoro.WinUI/Helpers/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public static IServiceCollection AddPlatformServices(this IServiceCollection ser
services.AddSingleton<IWindowService, WindowService>();

// child navigation
services.AddKeyedSingleton<IWinUINavigationService, NavigationService>(typeof(DiscoverViewModel).Name);
services.AddKeyedSingleton<IWinUINavigationService, NavigationService>(typeof(TorrentingViewModel).Name);
services.AddKeyedSingleton<IWinUINavigationService, NavigationService>(nameof(DiscoverViewModel));
services.AddKeyedSingleton<IWinUINavigationService, NavigationService>(nameof(TorrentingViewModel));

services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddTransient<IContentDialogService, ContentDialogService>();
Expand Down Expand Up @@ -136,6 +136,7 @@ private static IServiceCollection AddChildPages(this IServiceCollection services
// Discover
services.AddPageForNavigation<RecentEpisodesViewModel, RecentEpisodesSection>();
services.AddPageForNavigation<SearchProviderViewModel, SearchProviderSection>();
services.AddPageForNavigation<MyAnimeListDiscoverViewModel, MyAnimeListDiscoverSection>();

// Torrenting
services.AddPageForNavigation<SearchTorrentViewModel, SearchSection>();
Expand Down
4 changes: 4 additions & 0 deletions Totoro.WinUI/Totoro.WinUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Page Update="Views\DiscoverSections\MyAnimeListDiscoverSection.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="UserControls\PivotNavigation.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down Expand Up @@ -244,6 +247,7 @@
<ItemGroup>
<None Remove="Dialogs\Views\SearchListServicePage.xaml" />
<None Remove="UserControls\PivotNavigation.xaml" />
<None Remove="Views\DiscoverSections\MyAnimeListDiscoverSection.xaml" />
<None Remove="Views\DiscoverSections\RecentEpisodesSection.xaml" />
<None Remove="Views\DiscoverSections\SearchProviderSection.xaml" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8" ?>
<discoversections:MyAnimeListDiscoverSectionBase
x:Class="Totoro.WinUI.Views.DiscoverSections.MyAnimeListDiscoverSection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctk="using:CommunityToolkit.WinUI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:discover="using:Totoro.Core.ViewModels.Discover"
xmlns:discoversections="using:Totoro.WinUI.Views.DiscoverSections"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:Totoro.Core.Models"
xmlns:root="using:Totoro.WinUI"
xmlns:uc="using:Totoro.WinUI.UserControls"
mc:Ignorable="d">
<discoversections:MyAnimeListDiscoverSectionBase.Resources>
<Style x:Key="TitleStyle" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="20" />
</Style>
</discoversections:MyAnimeListDiscoverSectionBase.Resources>

<ctk:SwitchPresenter TargetType="x:Boolean" Value="{x:Bind ViewModel.IsLoading, Mode=OneWay}">
<ctk:Case Value="True">
<Grid>
<ProgressRing IsActive="True" />
</Grid>
</ctk:Case>
<ctk:Case Value="False">
<Grid Margin="{StaticResource LargeTopMargin}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<ItemsView
Grid.Row="1"
ItemsSource="{x:Bind ViewModel.Lists, Mode=OneWay}"
SelectionMode="None">
<ItemsView.ItemTemplate>
<DataTemplate x:DataType="discover:NamedAnimeList">
<ItemContainer>
<!--<TextBlock Text="test" />-->
<StackPanel Spacing="10">
<TextBlock Style="{StaticResource TitleStyle}" Text="{x:Bind Name}" />
<ItemsView ItemsSource="{x:Bind List}">
<ItemsView.ItemTemplate>
<DataTemplate x:DataType="models:AnimeModel">
<ItemContainer>
<uc:AnimeCard
Width="270"
Height="380"
Margin="3"
Anime="{x:Bind}"
Command="{x:Bind root:App.Commands.AnimeCard}" />
</ItemContainer>
</DataTemplate>
</ItemsView.ItemTemplate>
<ItemsView.Layout>
<StackLayout Orientation="Horizontal" />
</ItemsView.Layout>
</ItemsView>
</StackPanel>
</ItemContainer>
</DataTemplate>
</ItemsView.ItemTemplate>
<ItemsView.Layout>
<StackLayout Orientation="Vertical" Spacing="30" />
</ItemsView.Layout>
</ItemsView>

</Grid>
</ctk:Case>
</ctk:SwitchPresenter>
</discoversections:MyAnimeListDiscoverSectionBase>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.UI.Xaml.Controls;
using Totoro.Core.ViewModels.Discover;

namespace Totoro.WinUI.Views.DiscoverSections;

public class MyAnimeListDiscoverSectionBase : ReactivePage<MyAnimeListDiscoverViewModel> { }

public sealed partial class MyAnimeListDiscoverSection : MyAnimeListDiscoverSectionBase
{
public MyAnimeListDiscoverSection()
{
InitializeComponent();
}
}

0 comments on commit 517c1d6

Please sign in to comment.