Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
支持关联视频回退 (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy committed Oct 25, 2021
1 parent ffabce6 commit 3ff24cd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/App/Controls/App/AppTitleBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<Button
x:Name="BackButton"
Style="{StaticResource TitleBarButtonStyle}"
Click="OnBackButtonClick"
Click="OnBackButtonClickAsync"
TabIndex="1">
<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="Back" IsEnabled="{x:Bind ViewModel.IsBackButtonEnabled, Mode=OneWay}" />
Expand Down
14 changes: 9 additions & 5 deletions src/App/Controls/App/AppTitleBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Richasy.Bili.ViewModels.Uwp;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -43,7 +44,7 @@ public AppViewModel ViewModel
/// 尝试回退.
/// </summary>
/// <returns>是否调用了返回命令.</returns>
public bool TryBack()
public async Task<bool> TryBackAsync()
{
if (BackButton.Visibility != Visibility.Visible)
{
Expand All @@ -52,8 +53,11 @@ public bool TryBack()

if (ViewModel.IsOpenPlayer)
{
ViewModel.IsOpenPlayer = false;
return true;
if (await PlayerViewModel.Instance.CheckBackAsync())
{
ViewModel.IsOpenPlayer = false;
return true;
}
}
else if (ViewModel.IsShowOverlay)
{
Expand Down Expand Up @@ -110,9 +114,9 @@ private void OnMenuButtonClick(object sender, RoutedEventArgs e)
ViewModel.IsNavigatePaneOpen = !ViewModel.IsNavigatePaneOpen;
}

private void OnBackButtonClick(object sender, RoutedEventArgs e)
private async void OnBackButtonClickAsync(object sender, RoutedEventArgs e)
{
TryBack();
await TryBackAsync();
}

private void CheckBackButtonVisibility()
Expand Down
15 changes: 8 additions & 7 deletions src/App/Pages/RootPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Richasy.Bili.App.Controls;
using Richasy.Bili.Models.App.Args;
using Richasy.Bili.ViewModels.Uwp;
Expand All @@ -26,9 +27,9 @@ public RootPage()
this.InitializeComponent();
this.Loaded += OnLoadedAsync;
this.CoreViewModel.RequestShowTip += OnRequestShowTip;
this.CoreViewModel.RequestBack += OnRequestBack;
this.CoreViewModel.RequestBack += OnRequestBackAsync;
SizeChanged += OnSizeChanged;
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequestedAsync;
}

/// <summary>
Expand Down Expand Up @@ -81,7 +82,7 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
base.OnPointerReleased(e);
}

private bool TryBack()
private async Task<bool> TryBackAsync()
{
if (HolderContainer.Children.Count > 0)
{
Expand All @@ -94,13 +95,13 @@ private bool TryBack()
}
else if (CoreViewModel.IsBackButtonEnabled)
{
return TitleBar.TryBack();
return await TitleBar.TryBackAsync();
}

return false;
}

private void OnRequestBack(object sender, System.EventArgs e) => TryBack();
private async void OnRequestBackAsync(object sender, System.EventArgs e) => await TryBackAsync();

private async void OnLoadedAsync(object sender, RoutedEventArgs e)
{
Expand All @@ -110,9 +111,9 @@ private async void OnLoadedAsync(object sender, RoutedEventArgs e)
await AccountViewModel.Instance.TrySignInAsync(true);
}

private void OnBackRequested(object sender, BackRequestedEventArgs e)
private async void OnBackRequestedAsync(object sender, BackRequestedEventArgs e)
{
if (TryBack())
if (await TryBackAsync())
{
e.Handled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public partial class PlayerViewModel
private readonly IFileToolkit _fileToolkit;
private readonly ILoggerModule _logger;

private readonly List<string> _historyVideoList;
private readonly FFmpegInteropConfig _liveFFConfig;

private long _videoId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public PlayerViewModel()
_videoList = new List<DashItem>();
_subtitleList = new List<SubtitleItem>();
_lastReportProgress = TimeSpan.Zero;
_historyVideoList = new List<string>();

_liveFFConfig = new FFmpegInteropConfig();
_liveFFConfig.FFmpegOptions.Add("rtsp-transport", "tcp");
Expand Down Expand Up @@ -89,6 +90,32 @@ public void ApplyMediaControl(MediaPlayerElement playerControl)
BiliPlayer = playerControl;
}

/// <summary>
/// 检查是否可以返回到主页,或是返回至上一个视频.
/// </summary>
/// <returns>是否可以返回到主页.</returns>
public async Task<bool> CheckBackAsync()
{
_progressTimer.Stop();
if (_historyVideoList.Count > 0)
{
var lastVideo = _historyVideoList.Last();
_historyVideoList.Remove(lastVideo);
IsDetailCanLoaded = true;
DanmakuViewModel.Instance.Reset();
IsPlayInformationError = false;

await LoadVideoDetailAsync(lastVideo, true);

_progressTimer.Start();
InitDownload();
Loaded?.Invoke(this, EventArgs.Empty);
return false;
}

return true;
}

/// <summary>
/// 视频加载.
/// </summary>
Expand All @@ -100,10 +127,13 @@ public async Task LoadAsync(object vm, bool isRefresh = false)
var videoId = string.Empty;
var seasonId = 0;

var isReleated = false;

if (vm is VideoViewModel videoVM)
{
videoId = videoVM.VideoId;
_videoType = videoVM.VideoType;
isReleated = videoVM.IsRelated;
}
else if (vm is SeasonViewModel seasonVM)
{
Expand All @@ -128,6 +158,20 @@ public async Task LoadAsync(object vm, bool isRefresh = false)
DanmakuViewModel.Instance.Reset();
IsPlayInformationError = false;

if (!isReleated)
{
_historyVideoList.Clear();
}
else
{
if (_historyVideoList.Contains(AvId))
{
_historyVideoList.Remove(AvId);
}

_historyVideoList.Add(AvId);
}

switch (_videoType)
{
case VideoType.Video:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public partial class VideoViewModel
/// </summary>
public string SourceCoverUrl { get; set; }

/// <summary>
/// 是否为关联视频.
/// </summary>
public bool IsRelated { get; set; }

/// <inheritdoc/>
public override bool Equals(object obj) => obj is VideoViewModel model && VideoId == model.VideoId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public VideoViewModel(Relate relate)
AdditionalText = relate.Rating.ToString();
LimitCover(relate.Pic);
Source = relate;
IsRelated = true;
VideoType = relate.Goto.Equals(ServiceConstants.Av, StringComparison.OrdinalIgnoreCase) ?
Models.Enums.VideoType.Video : Models.Enums.VideoType.Pgc;
}
Expand Down

0 comments on commit 3ff24cd

Please sign in to comment.