Skip to content

Commit

Permalink
- play episode from discover page,
Browse files Browse the repository at this point in the history
- fix videojs not playing, used functionality was depricated.
  • Loading branch information
insomniachi committed Sep 5, 2022
1 parent 70ddc61 commit ac5f670
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 25 deletions.
25 changes: 21 additions & 4 deletions AnimDL.UI.Core/ViewModels/DiscoverViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ public class DiscoverViewModel : NavigatableViewModel, IHaveState
{
private readonly IRecentEpisodesProvider _recentEpisodesProvider;
private readonly IFeaturedAnimeProvider _featuredAnimeProvider;
private readonly INavigationService _navigationService;

public DiscoverViewModel(IRecentEpisodesProvider recentEpisodesProvider,
IFeaturedAnimeProvider featuredAnimeProvider)
IFeaturedAnimeProvider featuredAnimeProvider,
INavigationService navigationService)
{
_recentEpisodesProvider = recentEpisodesProvider;
_featuredAnimeProvider = featuredAnimeProvider;
_navigationService = navigationService;

SelectEpisode = ReactiveCommand.Create<AiredEpisode>(OnEpisodeSelected);

Observable
.Timer(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10))
.ObserveOn(RxApp.MainThreadScheduler)
Expand All @@ -28,15 +36,14 @@ public DiscoverViewModel(IRecentEpisodesProvider recentEpisodesProvider,

SelectedIndex++;
});

_recentEpisodesProvider = recentEpisodesProvider;
_featuredAnimeProvider = featuredAnimeProvider;
}

[Reactive] public IList<FeaturedAnime> Featured { get; set; } = new List<FeaturedAnime>();
[Reactive] public IList<AiredEpisode> Episodes { get; set; } = new List<AiredEpisode>();
[Reactive] public int SelectedIndex { get; set; }

public ICommand SelectEpisode { get; }

public void RestoreState(IState state)
{
Featured = state.GetValue<IList<FeaturedAnime>>(nameof(Featured));
Expand All @@ -61,4 +68,14 @@ public void StoreState(IState state)
state.AddOrUpdate(Featured);
state.AddOrUpdate(Episodes);
}

private void OnEpisodeSelected(AiredEpisode episode)
{
var navigationParameters = new Dictionary<string, object>
{
["EpisodeInfo"] = episode
};

_navigationService.NavigateTo<WatchViewModel>(parameter: navigationParameters);
}
}
26 changes: 16 additions & 10 deletions AnimDL.UI.Core/ViewModels/WatchViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AnimDL.Api;
using System.Text.RegularExpressions;
using AnimDL.Api;

namespace AnimDL.UI.Core.ViewModels;

Expand All @@ -19,6 +20,8 @@ public class WatchViewModel : NavigatableViewModel, IHaveState
private readonly ReadOnlyObservableCollection<SearchResultModel> _searchResults;
private readonly ReadOnlyObservableCollection<int> _episodes;

private int _episodeRequest;

public WatchViewModel(IProviderFactory providerFactory,
ITrackingService trackingService,
IViewService viewService,
Expand Down Expand Up @@ -162,11 +165,9 @@ public WatchViewModel(IProviderFactory providerFactory,
.SelectMany(result => Provider.StreamProvider.GetNumberOfStreams(result.Url))
.Select(count => Enumerable.Range(1, count).ToList())
.Do(list => _episodesCache.EditDiff(list))
.Where(_ => Anime is not null)
.Select(_ => Anime.Tracking?.WatchedEpisodes ?? 0)
.Where(ep => ep < Anime.TotalEpisodes)
.Select(_ => (Anime?.Tracking?.WatchedEpisodes + 1) ?? _episodeRequest)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ep => CurrentEpisode = ep + 1);
.Subscribe(ep => CurrentEpisode = ep);

/// Scrape url for <see cref="CurrentEpisode"/> and set to <see cref="Url"/>
this.ObservableForProperty(x => x.CurrentEpisode, x => x)
Expand Down Expand Up @@ -204,13 +205,18 @@ public WatchViewModel(IProviderFactory providerFactory,

public override Task OnNavigatedTo(IReadOnlyDictionary<string, object> parameters)
{
if (!parameters.ContainsKey("Anime"))
if (parameters.ContainsKey("Anime"))
{
return Task.CompletedTask;
HideControls = true;
Anime = parameters["Anime"] as IAnimeModel;
}
else if(parameters.ContainsKey("EpisodeInfo"))
{
var epInfo = parameters["EpisodeInfo"] as AiredEpisode;
var epMatch = Regex.Match(epInfo.EpisodeUrl, @"ep(\d+)");
_episodeRequest = epMatch.Success ? int.Parse(epMatch.Groups[1].Value) : 1;
SelectedAudio = new SearchResult { Title = epInfo.Anime, Url = epInfo.EpisodeUrl };
}

HideControls = true;
Anime = parameters["Anime"] as IAnimeModel;

return Task.CompletedTask;
}
Expand Down
5 changes: 3 additions & 2 deletions AnimDL.WinUI/Helpers/VideoJsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class VideoJsHelper
<meta charset=utf-8 />
<title>Your title</title>
<link href=""https://unpkg.com/video.js/dist/video-js.css"" rel=""stylesheet"">
<script src=""https://unpkg.com/video.js/dist/video.js""></script >
<script src=""https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js""></script>
<script src=""https://unpkg.com/video.js/dist/video.js""></script>
<script src=""https://unpkg.com/@videojs/http-streaming/dist/videojs-http-streaming.js""></script>
</head>
<body>
Expand Down Expand Up @@ -77,6 +77,7 @@ public class VideoJsHelper

public static string GetPlayerHtml(string url)
{
var result = string.Format(PlayerFormat, url);
return string.Format(PlayerFormat, url);
}
}
16 changes: 10 additions & 6 deletions AnimDL.WinUI/Services/Schedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ public class Schedule : ISchedule
public Dictionary<long, TimeRemaining> Dictionary { get; set; } = new();
private DateTime _lastUpdatedAt;
private bool _isRefreshing;
private readonly HttpClient _client = new();
private readonly HttpClient _httpClient;
private readonly IMalClient _malClient;

public Schedule(IMessageBus messageBus, IMalClient client)
public Schedule(IMessageBus messageBus,
IMalClient client,
HttpClient httpClient)
{
_httpClient = httpClient;
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UI.Core.Constants.UserAgent);

Observable.StartAsync(() => FetchSchedule());

messageBus.Listen<MinuteTick>()
Expand Down Expand Up @@ -62,14 +67,13 @@ public async Task FetchSchedule()
_isRefreshing = true;

Dictionary.Clear();
using var message = new HttpRequestMessage(HttpMethod.Get, "https://animixplay.to/assets/s/schedule.json");
message.Headers.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 response = _client.Send(message);
var response = await _httpClient.GetAsync("https://animixplay.to/assets/s/schedule.json");
var json = await response.Content.ReadAsStringAsync();
var node = JsonNode.Parse(json).AsArray();
var now = DateTimeOffset.Now.ToUnixTimeMilliseconds();

_lastUpdatedAt = DateTime.Now;

foreach (var item in node)
{
var time = long.Parse(item["time"].ToString());
Expand Down
6 changes: 4 additions & 2 deletions AnimDL.WinUI/Views/DiscoverPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
xmlns:ctk="using:CommunityToolkit.WinUI.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:CommunityToolkit.WinUI.UI"
xmlns:viewmodels="using:AnimDL.UI.Core.ViewModels"
xmlns:views="using:AnimDL.WinUI.Views"
Margin="{StaticResource MediumLeftRightMargin}"
Expand Down Expand Up @@ -78,6 +79,7 @@
x:Name="AnimeListView"
Grid.Row="1"
animations:ItemsReorderAnimation.Duration="00:00:00.4000000"
ui:ListViewExtensions.Command="{x:Bind ViewModel.SelectEpisode}"
IsItemClickEnabled="True"
ItemsSource="{x:Bind ViewModel.Episodes, Mode=OneWay}">
<GridView.ItemTemplate>
Expand Down Expand Up @@ -125,11 +127,11 @@
Padding="3"
VerticalAlignment="Center"
FontSize="15"
Text="{x:Bind Anime, Mode=TwoWay}"
Text="{x:Bind Anime}"
TextAlignment="Center"
TextTrimming="WordEllipsis"
TextWrapping="NoWrap"
ToolTipService.ToolTip="{x:Bind Anime, Mode=OneWay}" />
ToolTipService.ToolTip="{x:Bind Anime}" />
<TextBlock HorizontalAlignment="Center" Text="{x:Bind InfoText}" />
</StackPanel>
</Border>
Expand Down
1 change: 0 additions & 1 deletion AnimDL.WinUI/Views/UserListPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
xmlns:ui="using:CommunityToolkit.WinUI.UI"
xmlns:v="using:AnimDL.WinUI.Views"
xmlns:viewmodels="using:AnimDL.UI.Core.ViewModels"
xmlns:views="using:AnimDL.WinUI.Views"
Name="Page"
Margin="{StaticResource MediumLeftRightMargin}"
d:DataContext="{d:DesignInstance Type=viewmodels:UserListViewModel}"
Expand Down

0 comments on commit ac5f670

Please sign in to comment.