Skip to content

Commit

Permalink
remove pivot from about anime screen.
Browse files Browse the repository at this point in the history
fix crash when navigation from watch page while watching streams with vlc
  • Loading branch information
insomniachi committed Mar 31, 2023
1 parent d89f599 commit 487db54
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 22 deletions.
67 changes: 58 additions & 9 deletions Totoro.Core/ViewModels/AboutAnimeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

public class AboutAnimeViewModel : NavigatableViewModel
{
private readonly ObservableAsPropertyHelper<AnimeModel> _anime;
private readonly ObservableAsPropertyHelper<bool> _hasTracking;
private readonly ObservableAsPropertyHelper<IList<AnimeSound>> _sounds;
public ObservableCollection<PivotItemModel> Pages { get; } = new()
{
new PivotItemModel { Header = "Previews" },
new PivotItemModel { Header = "Related" },
new PivotItemModel { Header = "Recommended" },
new PivotItemModel { Header = "OST"}
};

public AboutAnimeViewModel(IAnimeServiceContext animeService,
INavigationService navigationService,
Expand All @@ -23,23 +27,61 @@ public AboutAnimeViewModel(IAnimeServiceContext animeService,
this.ObservableForProperty(x => x.Id, x => x)
.Where(id => id > 0)
.SelectMany(animeService.GetInformation)
.ToProperty(this, nameof(Anime), out _anime, scheduler: RxApp.MainThreadScheduler);
.ToPropertyEx(this, x => x.Anime, scheduler: RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.Anime)
.WhereNotNull()
.Select(anime => anime.Tracking is { })
.ToProperty(this, nameof(HasTracking), out _hasTracking, scheduler: RxApp.MainThreadScheduler);
.ToPropertyEx(this, x => x.HasTracking, scheduler: RxApp.MainThreadScheduler);

this.ObservableForProperty(x => x.Id, x => x)
.Where(id => id > 0)
.Select(animeSoundService.GetThemes)
.ToProperty(this, nameof(Sounds), out _sounds, scheduler: RxApp.MainThreadScheduler);
.ToPropertyEx(this, x => x.Sounds, scheduler: RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.Sounds)
.WhereNotNull()
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(sounds =>
{
if (sounds is { Count: > 0 })
{
return;
}

Pages.Remove(Pages.First(x => x.Header == "OST"));

});

this.WhenAnyValue(x => x.Anime)
.WhereNotNull()
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(anime =>
{
if(anime.Videos is not { Count : >0})
{
Pages.Remove(Pages.First(x => x.Header == "Previews"));
}
if(anime.Related is not { Length: > 0 })
{
Pages.Remove(Pages.First(x => x.Header == "Related"));
}
if (anime.Recommended is not { Length: > 0 })
{
Pages.Remove(Pages.First(x => x.Header == "Recommended"));
}
});

this.WhenAnyValue(x => x.SelectedPage)
.Where(x => x is null && Pages.Any(x => x.Visible))
.Subscribe(_ => SelectedPage = Pages.First(x => x.Visible));
}

[Reactive] public long Id { get; set; }
public AnimeModel Anime => _anime.Value;
public bool HasTracking => _hasTracking.Value;
public IList<AnimeSound> Sounds => _sounds.Value;
[Reactive] public PivotItemModel SelectedPage { get; set; }
[ObservableAsProperty] public AnimeModel Anime { get; }
[ObservableAsProperty] public bool HasTracking { get; }
[ObservableAsProperty] public IList<AnimeSound> Sounds { get; }
public ICommand WatchEpidoes { get; }
public ICommand UpdateStatus { get; }
public ICommand PlaySound { get; }
Expand All @@ -52,3 +94,10 @@ public override Task OnNavigatedTo(IReadOnlyDictionary<string, object> parameter
}

}

public class PivotItemModel : ReactiveObject
{
public string Header { get; set; }
[Reactive] public bool Visible { get; set; } = true;
}

2 changes: 1 addition & 1 deletion Totoro.WinUI/Media/Vlc/LibVLCMediaPlayerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class LibVLCMediaPlayerWrapper : IMediaPlayer

public void Dispose()
{
_mp?.Dispose();
_mp?.Pause();
}

public void Pause()
Expand Down
42 changes: 31 additions & 11 deletions Totoro.WinUI/Views/AboutAnimePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@
xmlns:ctk="using:CommunityToolkit.WinUI.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:help="using:Totoro.WinUI.Helpers"
xmlns:labs="using:CommunityToolkit.Labs.WinUI"
xmlns:local="using:Totoro.WinUI.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:media="using:CommunityToolkit.WinUI.UI.Media"
xmlns:models="using:Totoro.Core.Models"
xmlns:root="using:Totoro.WinUI"
xmlns:ui="using:CommunityToolkit.WinUI.UI"
xmlns:usercontrols="using:Totoro.WinUI.UserControls"
xmlns:vm="using:Totoro.Core.ViewModels"
Name="Page"
mc:Ignorable="d">

<local:AboutAnimePageBase.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///CommunityToolkit.Labs.WinUI.SegmentedControl/Segmented/Segmented.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</local:AboutAnimePageBase.Resources>

<Grid Margin="0,25,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
Expand Down Expand Up @@ -196,9 +206,19 @@

</Grid>
</Grid>

<Pivot>
<PivotItem Header="Previews">
<labs:Segmented
DisplayMemberPath="Header"
ItemsSource="{x:Bind ViewModel.Pages}"
SelectedItem="{x:Bind ViewModel.SelectedPage, Mode=TwoWay}"
Style="{StaticResource PivotSegmentedStyle}">
<labs:Segmented.ItemContainerStyle>
<Style TargetType="labs:SegmentedItem">
<Setter Property="FontSize" Value="20" />
</Style>
</labs:Segmented.ItemContainerStyle>
</labs:Segmented>
<ctk:SwitchPresenter Value="{x:Bind ViewModel.SelectedPage.Header, Mode=OneWay}">
<ctk:Case Value="Previews">
<ctk:AdaptiveGridView
Margin="0,10"
ui:ListViewExtensions.Command="{x:Bind root:App.Commands.PlayVideo}"
Expand All @@ -225,8 +245,8 @@
</DataTemplate>
</GridView.ItemTemplate>
</ctk:AdaptiveGridView>
</PivotItem>
<PivotItem Header="Related">
</ctk:Case>
<ctk:Case Value="Related">
<ctk:AdaptiveGridView
Margin="0,10"
animations:ItemsReorderAnimation.Duration="00:00:00.4000000"
Expand All @@ -241,8 +261,8 @@
</DataTemplate>
</ctk:AdaptiveGridView.ItemTemplate>
</ctk:AdaptiveGridView>
</PivotItem>
<PivotItem Header="Recommended">
</ctk:Case>
<ctk:Case Value="Recommended">
<ctk:AdaptiveGridView
Margin="0,10"
animations:ItemsReorderAnimation.Duration="00:00:00.4000000"
Expand All @@ -257,8 +277,8 @@
</DataTemplate>
</ctk:AdaptiveGridView.ItemTemplate>
</ctk:AdaptiveGridView>
</PivotItem>
<PivotItem Header="OST">
</ctk:Case>
<ctk:Case Value="OST">
<ItemsControl ItemsSource="{x:Bind ViewModel.Sounds, Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:AnimeSound">
Expand All @@ -276,8 +296,8 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</PivotItem>
</Pivot>
</ctk:Case>
</ctk:SwitchPresenter>
</StackPanel>
</Grid>
</ScrollViewer>
Expand Down
2 changes: 1 addition & 1 deletion Totoro.WinUI/Views/AboutAnimePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public AboutAnimePage()
.DisposeWith(d);
});
}
}
}

0 comments on commit 487db54

Please sign in to comment.