Skip to content

Commit

Permalink
Merge pull request #178 from NLick47/re_01
Browse files Browse the repository at this point in the history
refactor: 改进视频投稿页面,先展示完信息后设置图片
  • Loading branch information
yaobiao131 authored Dec 2, 2024
2 parents 2e1a195 + fb1b5cb commit 707da52
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
32 changes: 32 additions & 0 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 @@ -175,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
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
67 changes: 35 additions & 32 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 @@ -180,7 +181,8 @@ private void ExecuteBackSpace()

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

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

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

return true;
}

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

private async void UpdatePublication(int current)
private readonly Bitmap defaultPic = ImageHelper.LoadFromResource(new Uri("avares://DownKyi/Resources/video-placeholder.png"));
private async Task UpdatePublication(int current)
{
_tokenSource?.Cancel();
// 是否正在获取数据
// 在所有的退出分支中都需要设为true
IsEnabled = false;
_tokenSource = new CancellationTokenSource();
var cancellationToken = _tokenSource.Token;

var tab = TabHeaders[SelectTabId];

await Task.Run(() =>
var storageCover = new StorageCover();
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,21 +439,6 @@ 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;
Expand All @@ -466,33 +454,49 @@ await Task.Run(() =>
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)
{
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 readonly StorageCover storageCover = new StorageCover();
private async Task UpdateMediaCovers(CancellationToken cancellationToken)
{
var currentMedias = _medias.ToList();
var tasks = currentMedias.Select(async media =>
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
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 @@ -527,7 +531,6 @@ public override void OnNavigatedTo(NavigationContext navigationContext)
{
return;
}

InitView();

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

0 comments on commit 707da52

Please sign in to comment.