Skip to content

Commit

Permalink
refactor: 改进视频投稿页面,先展示完信息后设置图片
Browse files Browse the repository at this point in the history
  • Loading branch information
NLick47 committed Nov 25, 2024
1 parent bbfdddb commit 6853eb4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
9 changes: 9 additions & 0 deletions DownKyi.Core/Storage/StorageCover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public Bitmap GetCoverThumbnail(long avid, string bvid, long cid, string url, in
return GetCoverThumbnail(header, width, height);
}

public Task<Bitmap> GetCoverThumbnailAsync(long avid, string bvid, long cid, string url, int width, int height)
{
return Task.Run(() =>
{
var header = GetCover(avid, bvid, cid, url);
return GetCoverThumbnail(header, width, height);
});
}

/// <summary>
/// 获取封面缩略图
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions DownKyi/ViewModels/PageViewModels/PublicationMedia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public PublicationMedia(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
}
public string CoverUrl { get; set; }

public long Avid { get; set; }
public string Bvid { get; set; }
Expand Down
60 changes: 31 additions & 29 deletions DownKyi/ViewModels/ViewPublicationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private void ExecuteBackSpace()

// 结束任务
_tokenSource?.Cancel();

_tokenSource?.Dispose();
_tokenSource = null;
var parameter = new NavigationParam
{
ViewName = ParentView,
Expand Down Expand Up @@ -384,7 +385,7 @@ private bool OnCurrentChanged_Pager(int old, int current)
LoadingVisibility = true;
NoDataVisibility = false;

UpdatePublication(current);
_ = UpdatePublication(current);

return true;
}
Expand All @@ -402,18 +403,19 @@ private static string StringToUnicode(string s)
return sb.ToString();
}

private async void UpdatePublication(int current)
private async Task UpdatePublication(int current)
{
_tokenSource?.Cancel();
// 是否正在获取数据
// 在所有的退出分支中都需要设为true
IsEnabled = false;

_tokenSource = new CancellationTokenSource();
var cancellationToken = _tokenSource.Token;
var defaultPic = ImageHelper.LoadFromResource(new Uri("avares://DownKyi/Resources/video-placeholder.png"));
var tab = TabHeaders[SelectTabId];

await Task.Run(() =>
await Task.Run(async () =>
{
var cancellationToken = _tokenSource.Token;

var publications = Core.BiliApi.Users.UserSpace.GetPublication(_mid, current, _videoNumberInPage, tab.Id);
if (publications == null)
{
Expand All @@ -436,22 +438,7 @@ await Task.Run(() =>
{
// 查询、保存封面
var coverUrl = video.Pic;
Bitmap cover;
if (coverUrl == null || coverUrl == "")
{
cover = null; // new BitmapImage(new Uri($"pack://application:,,,/Resources/video-placeholder.png"));
}
else
{
if (!coverUrl.ToLower().StartsWith("http"))
{
coverUrl = $"https:{video.Pic}";
}

var storageCover = new StorageCover();
cover = storageCover.GetCoverThumbnail(video.Aid, video.Bvid, -1, coverUrl, 200, 125);
}


// 播放数
var play = string.Empty;
if (video.Play > 0)
Expand All @@ -473,26 +460,41 @@ await Task.Run(() =>
{
Avid = video.Aid,
Bvid = video.Bvid,
Cover = cover ?? ImageHelper.LoadFromResource(new Uri("avares://DownKyi/Resources/video-placeholder.png")),
Cover = defaultPic,
Duration = video.Length,
Title = video.Title,
PlayNumber = play,
CreateTime = ctime
CreateTime = ctime,
CoverUrl = coverUrl
};
_medias.Add(media);

LoadingVisibility = false;
NoDataVisibility = false;
});

// 判断是否该结束线程,若为true,跳出循环
if (cancellationToken.IsCancellationRequested)
{
break;
return;
}
}
}, (_tokenSource = new CancellationTokenSource()).Token);
IsEnabled = true;
IsEnabled = true;
await UpdateMediaCovers(cancellationToken);
}, cancellationToken).ContinueWith(t => { });
}

private async Task UpdateMediaCovers(CancellationToken cancellationToken)
{
var storageCover = new StorageCover();
var currentMedias = _medias.ToList();
foreach (var media in currentMedias)
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
media.Cover = await storageCover.GetCoverThumbnailAsync(media.Avid, media.Bvid, -1, media.CoverUrl, 200, 125);
}
}

/// <summary>
Expand Down

0 comments on commit 6853eb4

Please sign in to comment.