Skip to content

Commit

Permalink
refactor: 投稿视频页图片展示代码改进
Browse files Browse the repository at this point in the history
  • Loading branch information
NLick47 committed Nov 28, 2024
1 parent 6853eb4 commit fb1b5cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
41 changes: 32 additions & 9 deletions DownKyi.Core/Storage/StorageCover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DownKyi.Core.Logging;
using DownKyi.Core.Storage.Database;
using DownKyi.Core.Utils.Encryptor;
using System.Threading;
using Console = DownKyi.Core.Utils.Debugging.Console;

namespace DownKyi.Core.Storage;
Expand Down Expand Up @@ -32,15 +33,6 @@ 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 Expand Up @@ -184,6 +176,37 @@ public string GetCover(long avid, string bvid, long cid, string url)
}
}

public async Task<Bitmap?> GetCoverAsync(string url,TimeSpan? timeOut = null)
{
timeOut = timeOut ?? TimeSpan.FromSeconds(1.5);
using var stream = await GetImageBytesAsync(url, timeOut);
return stream == null ? null : new Bitmap(stream);
}


public async Task<Stream?> GetImageBytesAsync(string imageUrl,TimeSpan? timeOut = null)
{
try
{
using (var httpClient = new HttpClient())
{
if (timeOut is not null)
{
httpClient.Timeout = timeOut.Value;
}
var response = await httpClient.GetAsync(imageUrl);
response.EnsureSuccessStatusCode();

var stream = await response.Content.ReadAsStreamAsync();
return stream;
}
}
catch (Exception e)
{
return null;
}
}

/// <summary>
/// 下载图片
/// </summary>
Expand Down
25 changes: 13 additions & 12 deletions DownKyi/ViewModels/ViewPublicationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using DownKyi.Utils;
using DownKyi.ViewModels.PageViewModels;
using DownKyi.ViewModels.UserSpace;
using DryIoc;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
Expand Down Expand Up @@ -389,7 +390,7 @@ private bool OnCurrentChanged_Pager(int old, int current)

return true;
}

private static string StringToUnicode(string s)
{
var charbuffers = s.ToCharArray();
Expand All @@ -402,7 +403,7 @@ private static string StringToUnicode(string s)
}
return sb.ToString();
}

private readonly Bitmap defaultPic = ImageHelper.LoadFromResource(new Uri("avares://DownKyi/Resources/video-placeholder.png"));
private async Task UpdatePublication(int current)
{
_tokenSource?.Cancel();
Expand All @@ -411,9 +412,9 @@ private async Task UpdatePublication(int current)
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];

var tab = TabHeaders[SelectTabId];
var storageCover = new StorageCover();
await Task.Run(async () =>
{
var publications = Core.BiliApi.Users.UserSpace.GetPublication(_mid, current, _videoNumberInPage, tab.Id);
Expand All @@ -438,7 +439,7 @@ await Task.Run(async () =>
{
// 查询、保存封面
var coverUrl = video.Pic;

// 播放数
var play = string.Empty;
if (video.Play > 0)
Expand All @@ -453,7 +454,6 @@ await Task.Run(async () =>
var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
var dateCTime = startTime.AddSeconds(video.Created);
var ctime = dateCTime.ToString("yyyy-MM-dd");

App.PropertyChangeAsync(() =>
{
var media = new PublicationMedia(EventAggregator)
Expand Down Expand Up @@ -482,19 +482,21 @@ await Task.Run(async () =>
await UpdateMediaCovers(cancellationToken);
}, cancellationToken).ContinueWith(t => { });
}

private readonly StorageCover storageCover = new StorageCover();
private async Task UpdateMediaCovers(CancellationToken cancellationToken)
{
var storageCover = new StorageCover();
var currentMedias = _medias.ToList();
foreach (var media in currentMedias)
var tasks = currentMedias.Select(async media =>
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
media.Cover = await storageCover.GetCoverThumbnailAsync(media.Avid, media.Bvid, -1, media.CoverUrl, 200, 125);
}
var coverUrl = $"{media.CoverUrl}@{200}w_{125}h_1c_!web-space-index-myvideo.webp";
var bitmap = await storageCover.GetCoverAsync(coverUrl) ?? defaultPic;
media.Cover = bitmap;
});
await Task.WhenAll(tasks);
}

/// <summary>
Expand Down Expand Up @@ -529,7 +531,6 @@ public override void OnNavigatedTo(NavigationContext navigationContext)
{
return;
}

InitView();

_mid = (long)parameter["mid"];
Expand Down

0 comments on commit fb1b5cb

Please sign in to comment.