Skip to content

Commit

Permalink
- Add search for discover page
Browse files Browse the repository at this point in the history
- fix regression in schedule page, submit timestamp page
  • Loading branch information
insomniachi committed Dec 29, 2022
1 parent 417f54d commit 3282f33
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public IObservable<IEnumerable<ScheduledAnimeModel>> GetCurrentlyAiringTrackedAn
.WithFields(FieldNames)
.Find();
observer.OnNext(ConvertToScheduledAnimeModel(pagedAnime.Data.Where(CurrentlyAiringOrFinishedWithinLastWeek).ToList()));
observer.OnNext(ConvertToScheduledAnimeModel(pagedAnime.Data.Where(CurrentlyAiringOrFinishedToday).ToList()));
while (!string.IsNullOrEmpty(pagedAnime.Paging.Next))
{
pagedAnime = await _client.GetNextAnimePage(pagedAnime);
observer.OnNext(ConvertToScheduledAnimeModel(pagedAnime.Data.Where(CurrentlyAiringOrFinishedWithinLastWeek).ToList()));
observer.OnNext(ConvertToScheduledAnimeModel(pagedAnime.Data.Where(CurrentlyAiringOrFinishedToday).ToList()));
}
observer.OnCompleted();
Expand Down Expand Up @@ -169,7 +169,7 @@ private IEnumerable<ScheduledAnimeModel> ConvertToScheduledAnimeModel(List<MalAp
return anime.Select(x => _converter.Convert<ScheduledAnimeModel>(x) as ScheduledAnimeModel);
}

private static bool CurrentlyAiringOrFinishedWithinLastWeek(MalApi.Anime anime)
private static bool CurrentlyAiringOrFinishedToday(MalApi.Anime anime)
{
if(anime.Status == MalApi.AiringStatus.CurrentlyAiring)
{
Expand All @@ -181,6 +181,6 @@ private static bool CurrentlyAiringOrFinishedWithinLastWeek(MalApi.Anime anime)
return false;
}

return (DateTime.Today - date).TotalDays < 7;
return DateTime.Today == date;
}
}
73 changes: 51 additions & 22 deletions Totoro.Core/ViewModels/DiscoverViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,38 @@ public DiscoverViewModel(IProviderFactory providerFacotory,
_episodesCache
.Connect()
.RefCount()
//.Filter(this.WhenAnyValue(x => x.ShowOnlyWatchingAnime).Select(Filter))
//.Sort(SortExpressionComparer<AiredEpisode>.Descending(x => x.TimeOfAiring), SortOptimisations.ComparesImmutableValuesOnly)
.Filter(this.WhenAnyValue(x => x.FilterText).Select(FilterByTitle))
.Sort(SortExpressionComparer<AiredEpisode>.Ascending(x => _episodesCache.Items.IndexOf(x)))
.Page(this.WhenAnyValue(x => x.PagerViewModel).WhereNotNull().SelectMany(x => x.AsPager()))
.Do(changes => PagerViewModel?.Update(changes.Response))
.Bind(out _episodes)
.DisposeMany()
.Subscribe()
.DisposeWith(Garbage);

CardWidth = settings.DefaultProviderType == ProviderType.AnimePahe ? 480 : 190; // animepahe image is thumbnail
ShowOnlyWatchingAnime = IsAuthenticated = trackingService.IsAuthenticated;
DontUseImageEx = settings.DefaultProviderType == ProviderType.Yugen; // using imagex for yugen is crashing

SelectEpisode = ReactiveCommand.CreateFromTask<AiredEpisode>(OnEpisodeSelected);
SelectFeaturedAnime = ReactiveCommand.Create<FeaturedAnime>(OnFeaturedAnimeSelected);
ShowOnlyWatchingAnime = IsAuthenticated = trackingService.IsAuthenticated;
LoadMore = ReactiveCommand.Create(LoadMoreEpisodes, this.WhenAnyValue(x => x.IsLoading).Select(x => !x));
}

[Reactive] public int SelectedIndex { get; set; }
[Reactive] public bool ShowOnlyWatchingAnime { get; set; }
[Reactive] public bool IsLoading { get; set; }
[Reactive] public PagerViewModel PagerViewModel { get; set; }
[Reactive] public string FilterText { get; set; }
[Reactive] public bool DontUseImageEx { get; private set; }

public bool IsAuthenticated { get; }
public double CardWidth { get; }
public ReadOnlyObservableCollection<AiredEpisode> Episodes => _episodes;

public ICommand SelectEpisode { get; }
public ICommand SelectFeaturedAnime { get; }
public double CardWidth { get; }
public ICommand LoadMore { get; }

public void RestoreState(IState state)
{
Expand Down Expand Up @@ -74,28 +84,24 @@ public void StoreState(IState state)

public override Task OnNavigatedTo(IReadOnlyDictionary<string, object> parameters)
{
if(_provider.AiredEpisodesProvider is null)
{
return Task.CompletedTask;
}

IsLoading = true;

_provider
.AiredEpisodesProvider
.GetRecentlyAiredEpisodes()
.ToObservable()
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(eps =>
{
_episodesCache.AddOrUpdate(eps);
IsLoading = false;
}, RxApp.DefaultExceptionHandler.OnError)
.DisposeWith(Garbage);
LoadPage(1)
.Finally(() => PagerViewModel = new(0, _episodesCache.Items.Count()))
.Subscribe(_ => { }, RxApp.DefaultExceptionHandler.OnError);

return Task.CompletedTask;
}

private void LoadMoreEpisodes() =>
LoadPage((PagerViewModel?.PageCount ?? 1) + 1)
.Finally(() =>
{
if(PagerViewModel.CurrentPage == PagerViewModel.PageCount - 2)
{
PagerViewModel.CurrentPage++;
}
})
.Subscribe(_ => { }, RxApp.DefaultExceptionHandler.OnError);

private Task OnEpisodeSelected(AiredEpisode episode)
{
var navigationParameters = new Dictionary<string, object>
Expand All @@ -117,4 +123,27 @@ private void OnFeaturedAnimeSelected(FeaturedAnime anime)

_navigationService.NavigateTo<WatchViewModel>(parameter: navigationParameters);
}

private IObservable<IEnumerable<AiredEpisode>> LoadPage(int page)
{
if (_provider.AiredEpisodesProvider is null)
{
return Observable.Empty<IEnumerable<AiredEpisode>>();
}

IsLoading = true;

return _provider
.AiredEpisodesProvider
.GetRecentlyAiredEpisodes(page)
.ToObservable()
.ObserveOn(RxApp.MainThreadScheduler)
.Do(eps =>
{
_episodesCache.AddOrUpdate(eps);
IsLoading = false;
});
}

private static Func<AiredEpisode, bool> FilterByTitle(string title) => (AiredEpisode ae) => string.IsNullOrEmpty(title) || ae.Title.ToLower().Contains(title.ToLower());
}
2 changes: 1 addition & 1 deletion Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SubmitTimeStampsView()
.Events()
.Click
.Select(_ => Unit.Default)
.InvokeCommand(ViewModel.SetStartPosition)
.InvokeCommand(ViewModel.SetEndPosition)
.DisposeWith(d);
});
}
Expand Down
65 changes: 54 additions & 11 deletions Totoro.WinUI/Views/DiscoverPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,42 @@
d:DataContext="{d:DesignInstance Type=viewmodels:DiscoverViewModel}"
mc:Ignorable="d">

<Grid Margin="0,25">
<views:DiscoverPageBase.Resources>
<x:Boolean x:Key="True">True</x:Boolean>
<x:Boolean x:Key="False">False</x:Boolean>
</views:DiscoverPageBase.Resources>

<Grid Margin="0,45">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>

<StackPanel HorizontalAlignment="Center" Spacing="5">

<StackPanel Orientation="Horizontal" Spacing="10">
<AutoSuggestBox
Width="300"
Height="35"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
PlaceholderText="Search"
QueryIcon="Find"
Text="{x:Bind ViewModel.FilterText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Command="{x:Bind ViewModel.LoadMore}" Content="Load More" />
</StackPanel>

<PipsPager
Margin="{StaticResource SmallTopBottomMargin}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
NextButtonVisibility="Visible"
NumberOfPages="{x:Bind ViewModel.PagerViewModel.PageCount, Mode=OneWay}"
PreviousButtonVisibility="Visible"
SelectedPageIndex="{x:Bind ViewModel.PagerViewModel.CurrentPage, Mode=TwoWay}" />

</StackPanel>
<!--<Grid Margin="10">
<FlipView
x:Name="Gallery"
Expand Down Expand Up @@ -90,28 +119,39 @@
<ctk:AdaptiveGridView
x:Name="AnimeListView"
Grid.Row="2"
Margin="0,25,0,0"
animations:ItemsReorderAnimation.Duration="00:00:00.4000000"
ui:ListViewExtensions.Command="{x:Bind ViewModel.SelectEpisode}"
DesiredWidth="{x:Bind ViewModel.CardWidth}"
IsItemClickEnabled="True"
ItemHeight="320"
ItemsSource="{x:Bind ViewModel.Episodes, Mode=OneWay}">
ItemsSource="{x:Bind ViewModel.Episodes, Mode=OneWay}"
StretchContentForSingleRow="False">
<ctk:AdaptiveGridView.ItemTemplate>
<DataTemplate x:DataType="am:AiredEpisode">
<Grid x:Name="MainGrid" Margin="3">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ctk:ImageEx
Grid.Row="0"
CacheMode="BitmapCache"
IsCacheEnabled="True"
PlaceholderSource="/Assets/placeholder.jpg"
PlaceholderStretch="Fill"
Source="{x:Bind Image}"
Stretch="UniformToFill" />

<ctk:SwitchPresenter Grid.Row="0" Value="{Binding ElementName=Page, Path=ViewModel.DontUseImageEx, Mode=OneWay}">
<ctk:Case IsDefault="True" Value="{StaticResource False}">
<ctk:ImageEx
CacheMode="BitmapCache"
EnableLazyLoading="True"
IsCacheEnabled="True"
PlaceholderSource="/Assets/placeholder.jpg"
PlaceholderStretch="Fill"
Source="{x:Bind Image}"
Stretch="UniformToFill" />
</ctk:Case>
<ctk:Case Value="{StaticResource True}">
<Image
Grid.Row="0"
Source="{x:Bind Image}"
Stretch="UniformToFill" />
</ctk:Case>
</ctk:SwitchPresenter>

<Grid
x:Name="Time"
Expand Down Expand Up @@ -162,6 +202,9 @@
x:Name="LoadingControl"
Grid.Row="2"
IsLoading="{x:Bind ViewModel.IsLoading, Mode=TwoWay}">
<ctk:Loading.Background>
<AcrylicBrush TintColor="Black" TintOpacity="0.2" />
</ctk:Loading.Background>
<ProgressRing Margin="0,0,12,0" IsActive="True" />
</ctk:Loading>

Expand Down
10 changes: 7 additions & 3 deletions Totoro.WinUI/Views/SchedulePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:animations="using:CommunityToolkit.WinUI.UI.Animations"
xmlns:cm="using:Totoro.Core.Models"
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:model="using:Totoro.Core.Models"
Expand Down Expand Up @@ -49,12 +50,15 @@
</ListBox.ItemsPanel>
</ListBox>

<GridView
<ctk:AdaptiveGridView
x:Name="AnimeListView"
Grid.Row="1"
animations:ItemsReorderAnimation.Duration="00:00:00.4000000"
DesiredWidth="240"
IsItemClickEnabled="True"
ItemsSource="{x:Bind ViewModel.Anime}">
ItemHeight="380"
ItemsSource="{x:Bind ViewModel.Anime}"
StretchContentForSingleRow="False">
<GridView.ItemTemplate>
<DataTemplate x:DataType="cm:ScheduledAnimeModel">
<uc:AnimeCard x:Name="Card" Anime="{x:Bind}">
Expand Down Expand Up @@ -86,7 +90,7 @@
<Setter Property="Margin" Value="10" />
</Style>
</GridView.ItemContainerStyle>
</GridView>
</ctk:AdaptiveGridView>


</Grid>
Expand Down

0 comments on commit 3282f33

Please sign in to comment.