Skip to content

Commit

Permalink
use animixplay featured anime in discover page
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Aug 27, 2022
1 parent d3126b9 commit cd5fb0c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
56 changes: 33 additions & 23 deletions AnimDL.UI.Core/ViewModels/DiscoverViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
using MalApi;
using System.Text.Json.Serialization;
using MalApi;
using MalApi.Interfaces;

namespace AnimDL.UI.Core.ViewModels;

public class DiscoverViewModel : NavigatableViewModel, IHaveState
{
private readonly IMalClient _client;

public DiscoverViewModel(IMalClient client)
public DiscoverViewModel()
{
_client = client;

Observable
.Timer(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ =>
{
if (TopAiring.Count == 0)
if (Featured.Count == 0)
{
SelectedIndex = 0;
return;
}

if (SelectedIndex == TopAiring.Count - 1)
if (SelectedIndex == Featured.Count - 1)
{
SelectedIndex = 0;
return;
Expand All @@ -32,33 +30,45 @@ public DiscoverViewModel(IMalClient client)
});
}

[Reactive] public ObservableCollection<Anime> TopAiring { get; set; } = new();
[Reactive] public ObservableCollection<FeaturedAnime> Featured { get; set; }
[Reactive] public int SelectedIndex { get; set; }

public void RestoreState(IState state)
{
Featured = state.GetValue<ObservableCollection<FeaturedAnime>>(nameof(Featured));
}

public async Task SetInitialState()
{
var result = await _client.Anime()
.Top(AnimeRankingType.Airing)
.WithLimit(10)
.Find();

result.Data.ForEach(async x =>
{
TopAiring.Add(await _client.Anime()
.WithId(x.Anime.Id)
.WithField(x => x.Background)
.WithField(x => x.Pictures)
.WithField(x => x.Synopsis)
.WithField(x => x.Genres)
.Find());
});
using var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54");
var stream = await client.GetStreamAsync("https://animixplay.to/assets/s/featured.json");
Featured = await JsonSerializer.DeserializeAsync<ObservableCollection<FeaturedAnime>>(stream);
}

public void StoreState(IState state)
{
state.AddOrUpdate(Featured);
}
}

public class FeaturedAnime
{
public string Id => Url?.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Skip(1).Take(1).FirstOrDefault();
public string[] GenresArray => Genres?.Split(",");

[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("url")]
public string Url { get; set; }

[JsonPropertyName("img")]
public string Image { get; set; }

[JsonPropertyName("genre")]
public string Genres { get; set; }

[JsonPropertyName("desc")]
public string Description { get; set; }
}
18 changes: 9 additions & 9 deletions AnimDL.WinUI/Views/DiscoverPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@

<Grid Margin="0,10">
<Grid.RowDefinitions>
<RowDefinition Height="400" />
<RowDefinition Height="300" />
<RowDefinition />
</Grid.RowDefinitions>

<Grid>
<FlipView
x:Name="Gallery"
ItemsSource="{x:Bind ViewModel.TopAiring, Mode=OneWay}"
ItemsSource="{x:Bind ViewModel.Featured, Mode=OneWay}"
SelectedIndex="{x:Bind ViewModel.SelectedIndex, Mode=TwoWay}">
<FlipView.ItemTemplate>
<DataTemplate x:DataType="malapi:Anime">
<DataTemplate x:DataType="viewmodels:FeaturedAnime">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Source="{x:Bind MainPicture.Large}" Stretch="UniformToFill" />
<Image Source="{x:Bind Image}" Stretch="UniformToFill" />
<StackPanel Grid.Column="1" Padding="10">
<TextBlock
FontSize="30"
FontWeight="Bold"
Text="{x:Bind Title}"
TextWrapping="WrapWholeWords" />

<ItemsRepeater Margin="0,10" ItemsSource="{x:Bind Genres}">
<ItemsRepeater Margin="0,10" ItemsSource="{x:Bind GenresArray}">
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="malapi:Genre">
<DataTemplate x:DataType="x:String">
<Border
Padding="5"
Background="{ThemeResource AccentControlElevationBorderBrush}"
CornerRadius="3">
<TextBlock Text="{x:Bind Name}" />
<TextBlock Text="{x:Bind}" />
</Border>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
Expand All @@ -56,7 +56,7 @@
<TextBlock
Padding="10"
FontSize="20"
Text="{x:Bind Synopsis}"
Text="{x:Bind Description}"
TextTrimming="CharacterEllipsis"
TextWrapping="WrapWholeWords" />
</StackPanel>
Expand All @@ -67,7 +67,7 @@
<PipsPager
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
NumberOfPages="{x:Bind ViewModel.TopAiring.Count, Mode=OneWay}"
NumberOfPages="{x:Bind ViewModel.Featured.Count, Mode=OneWay}"
SelectedPageIndex="{x:Bind Gallery.SelectedIndex, Mode=TwoWay}" />
</Grid>

Expand Down

0 comments on commit cd5fb0c

Please sign in to comment.