Files
downkyicore/DownKyi/ViewModels/ViewUserSpaceViewModel.cs
2023-11-25 21:59:48 +08:00

607 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
using DownKyi.Core.BiliApi.Users;
using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.Storage;
using DownKyi.Core.Utils;
using DownKyi.Events;
using DownKyi.Images;
using DownKyi.Utils;
using DownKyi.ViewModels.UserSpace;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
namespace DownKyi.ViewModels;
public class ViewUserSpaceViewModel : ViewModelBase
{
public const string Tag = "PageUserSpace";
private readonly IRegionManager regionManager;
// mid
private long mid = -1;
#region
private VectorImage arrowBack;
public VectorImage ArrowBack
{
get => arrowBack;
set => SetProperty(ref arrowBack, value);
}
private bool loading;
public bool Loading
{
get => loading;
set => SetProperty(ref loading, value);
}
private bool noDataVisibility;
public bool NoDataVisibility
{
get => noDataVisibility;
set => SetProperty(ref noDataVisibility, value);
}
private bool loadingVisibility;
public bool LoadingVisibility
{
get => loadingVisibility;
set => SetProperty(ref loadingVisibility, value);
}
private bool viewVisibility;
public bool ViewVisibility
{
get => viewVisibility;
set => SetProperty(ref viewVisibility, value);
}
private bool contentVisibility;
public bool ContentVisibility
{
get => contentVisibility;
set => SetProperty(ref contentVisibility, value);
}
private string topNavigationBg;
public string TopNavigationBg
{
get => topNavigationBg;
set => SetProperty(ref topNavigationBg, value);
}
private Bitmap background;
public Bitmap Background
{
get => background;
set => SetProperty(ref background, value);
}
private Bitmap header;
public Bitmap Header
{
get => header;
set => SetProperty(ref header, value);
}
private string userName;
public string UserName
{
get => userName;
set => SetProperty(ref userName, value);
}
private Bitmap sex;
public Bitmap Sex
{
get => sex;
set => SetProperty(ref sex, value);
}
private Bitmap level;
public Bitmap Level
{
get => level;
set => SetProperty(ref level, value);
}
private bool vipTypeVisibility;
public bool VipTypeVisibility
{
get => vipTypeVisibility;
set => SetProperty(ref vipTypeVisibility, value);
}
private string vipType;
public string VipType
{
get => vipType;
set => SetProperty(ref vipType, value);
}
private string sign;
public string Sign
{
get => sign;
set => SetProperty(ref sign, value);
}
private string isFollowed;
public string IsFollowed
{
get => isFollowed;
set => SetProperty(ref isFollowed, value);
}
private ObservableCollection<TabLeftBanner> tabLeftBanners;
public ObservableCollection<TabLeftBanner> TabLeftBanners
{
get => tabLeftBanners;
set => SetProperty(ref tabLeftBanners, value);
}
private ObservableCollection<TabRightBanner> tabRightBanners;
public ObservableCollection<TabRightBanner> TabRightBanners
{
get => tabRightBanners;
set => SetProperty(ref tabRightBanners, value);
}
private int selectedRightBanner;
public int SelectedRightBanner
{
get => selectedRightBanner;
set => SetProperty(ref selectedRightBanner, value);
}
#endregion
public ViewUserSpaceViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) : base(
eventAggregator)
{
this.regionManager = regionManager;
#region
// 返回按钮
ArrowBack = NavigationIcon.Instance().ArrowBack;
ArrowBack.Fill = DictionaryResource.GetColor("ColorTextDark");
// 初始化loading
Loading = true;
TopNavigationBg = "#00FFFFFF"; // 透明
TabLeftBanners = new ObservableCollection<TabLeftBanner>();
TabRightBanners = new ObservableCollection<TabRightBanner>();
#endregion
}
#region
// 返回事件
private DelegateCommand backSpaceCommand;
public DelegateCommand BackSpaceCommand =>
backSpaceCommand ?? (backSpaceCommand = new DelegateCommand(ExecuteBackSpace));
/// <summary>
/// 返回事件
/// </summary>
private void ExecuteBackSpace()
{
NavigationParam parameter = new NavigationParam
{
ViewName = ParentView,
ParentViewName = null,
Parameter = null
};
eventAggregator.GetEvent<NavigationEvent>().Publish(parameter);
}
// 左侧tab点击事件
private DelegateCommand<object> tabLeftBannersCommand;
public DelegateCommand<object> TabLeftBannersCommand => tabLeftBannersCommand ??
(tabLeftBannersCommand =
new DelegateCommand<object>(
ExecuteTabLeftBannersCommand));
/// <summary>
/// 左侧tab点击事件
/// </summary>
/// <param name="parameter"></param>
private void ExecuteTabLeftBannersCommand(object parameter)
{
if (!(parameter is TabLeftBanner banner))
{
return;
}
NavigationParameters param = new NavigationParameters()
{
{ "object", banner.Object },
{ "mid", mid },
};
switch (banner.Id)
{
case 0: // 投稿
regionManager.RequestNavigate("UserSpaceContentRegion", ViewArchiveViewModel.Tag, param);
break;
case 1: // 频道(弃用)
regionManager.RequestNavigate("UserSpaceContentRegion", ViewChannelViewModel.Tag, param);
break;
case 2: // 合集和列表
regionManager.RequestNavigate("UserSpaceContentRegion", UserSpace.ViewSeasonsSeriesViewModel.Tag,
param);
break;
}
}
// 右侧tab点击事件
private DelegateCommand<object> tabRightBannersCommand;
public DelegateCommand<object> TabRightBannersCommand => tabRightBannersCommand ??
(tabRightBannersCommand =
new DelegateCommand<object>(
ExecuteTabRightBannersCommand));
/// <summary>
/// 右侧tab点击事件
/// </summary>
private void ExecuteTabRightBannersCommand(object parameter)
{
if (!(parameter is TabRightBanner banner))
{
return;
}
Dictionary<string, object> data = new Dictionary<string, object>
{
{ "mid", mid },
{ "friendId", 0 }
};
string parentViewName;
if (ParentView == ViewFriendsViewModel.Tag)
{
parentViewName = ViewIndexViewModel.Tag;
}
else
{
parentViewName = Tag;
}
switch (banner.Id)
{
case 0:
data["friendId"] = 0;
NavigateToView.NavigationView(eventAggregator, ViewFriendsViewModel.Tag, parentViewName, data);
break;
case 1:
data["friendId"] = 1;
NavigateToView.NavigationView(eventAggregator, ViewFriendsViewModel.Tag, parentViewName, data);
break;
}
SelectedRightBanner = -1;
}
#endregion
/// <summary>
/// 初始化页面
/// </summary>
private void InitView()
{
TopNavigationBg = "#00FFFFFF"; // 透明
ArrowBack.Fill = DictionaryResource.GetColor("ColorTextDark");
Background = null;
Header = null;
UserName = "";
Sex = null;
Level = null;
VipTypeVisibility = false;
VipType = "";
Sign = "";
TabLeftBanners.Clear();
TabRightBanners.Clear();
SelectedRightBanner = -1;
// 将内容置空,使其不指向任何页面
regionManager.RequestNavigate("UserSpaceContentRegion", "");
ContentVisibility = false;
ViewVisibility = false;
LoadingVisibility = true;
NoDataVisibility = false;
}
/// <summary>
/// 更新用户信息
/// </summary>
private async void UpdateSpaceInfo()
{
bool isNoData = true;
Uri toutuUri = null;
string headerUri = null;
Uri sexUri = null;
Uri levelUri = null;
await Task.Run(() =>
{
// 背景图片
SpaceSettings spaceSettings = Core.BiliApi.Users.UserSpace.GetSpaceSettings(mid);
if (spaceSettings != null)
{
StorageCover storageCover = new StorageCover();
string toutu = storageCover.GetCover($"https://i0.hdslb.com/{spaceSettings.Toutu.Limg}");
toutuUri = new Uri(toutu);
}
else
{
toutuUri = new Uri("avares://DownKyi/Resources/backgound/9-绿荫秘境.png");
}
// 用户信息
UserInfoForSpace userInfo = UserInfo.GetUserInfoForSpace(mid);
if (userInfo != null)
{
isNoData = false;
// 头像
StorageHeader storageHeader = new StorageHeader();
headerUri = storageHeader.GetHeader(mid, userInfo.Name, userInfo.Face);
// 用户名
UserName = userInfo.Name;
// 性别
if (userInfo.Sex == "男")
{
sexUri = new Uri("avares://DownKyi/Resources/sex/male.png");
}
else if (userInfo.Sex == "女")
{
sexUri = new Uri("avares://DownKyi/Resources/sex/female.png");
}
// 显示vip信息
if (userInfo.Vip.Label.Text == null || userInfo.Vip.Label.Text == "")
{
VipTypeVisibility = false;
}
else
{
VipTypeVisibility = true;
VipType = userInfo.Vip.Label.Text;
}
// 等级
levelUri = new Uri($"avares://DownKyi/Resources/level/lv{userInfo.Level}.png");
// 签名
Sign = userInfo.Sign;
// 是否关注此UP
PropertyChangeAsync(() =>
{
IsFollowed = userInfo.IsFollowed
? DictionaryResource.GetString("Followed")
: DictionaryResource.GetString("NotFollowed");
});
}
else
{
// 没有数据
isNoData = true;
}
});
// 是否获取到数据
if (isNoData)
{
TopNavigationBg = "#00FFFFFF"; // 透明
ArrowBack.Fill = DictionaryResource.GetColor("ColorTextDark");
Background = null;
ViewVisibility = false;
LoadingVisibility = false;
NoDataVisibility = true;
return;
}
else
{
// 头像
StorageHeader storageHeader = new StorageHeader();
Header = storageHeader.GetHeaderThumbnail(headerUri, 64, 64);
// 性别
Sex = sexUri == null ? null : ImageHelper.LoadFromResource(sexUri);
// 等级
Level = levelUri == null ? null : ImageHelper.LoadFromResource(levelUri);
ArrowBack.Fill = DictionaryResource.GetColor("ColorText");
TopNavigationBg = DictionaryResource.GetColor("ColorMask100");
Background = ImageHelper.LoadFromResource(toutuUri);
ViewVisibility = true;
LoadingVisibility = false;
NoDataVisibility = false;
}
ContentVisibility = true;
// 投稿视频
List<SpacePublicationListTypeVideoZone> publicationTypes = null;
await Task.Run(() => { publicationTypes = Core.BiliApi.Users.UserSpace.GetPublicationType(mid); });
if (publicationTypes != null && publicationTypes.Count > 0)
{
TabLeftBanners.Add(new TabLeftBanner
{
Object = publicationTypes,
Id = 0,
Icon = NormalIcon.Instance().VideoUp,
IconColor = "#FF02B5DA",
Title = DictionaryResource.GetString("Publication"),
IsSelected = true
});
}
// 频道
//List<SpaceChannelList> channelList = null;
//await Task.Run(() =>
//{
// channelList = Core.BiliApi.Users.UserSpace.GetChannelList(mid);
//});
//if (channelList != null && channelList.Count > 0)
//{
// TabLeftBanners.Add(new TabLeftBanner
// {
// Object = channelList,
// Id = 1,
// Icon = NormalIcon.Instance().Channel,
// IconColor = "#FF23C9ED",
// Title = DictionaryResource.GetString("Channel")
// });
//}
// 合集和列表
SpaceSeasonsSeries seasonsSeries = null;
await Task.Run(() => { seasonsSeries = Core.BiliApi.Users.UserSpace.GetSeasonsSeries(mid, 1, 20); });
if (seasonsSeries != null && seasonsSeries.Page.Total > 0)
{
TabLeftBanners.Add(new TabLeftBanner
{
Object = seasonsSeries,
Id = 2,
Icon = NormalIcon.Instance().Channel,
IconColor = "#FF23C9ED",
Title = DictionaryResource.GetString("SeasonsSeries")
});
}
// 收藏夹
// 订阅
// 关系状态数
UserRelationStat relationStat = null;
await Task.Run(() => { relationStat = UserStatus.GetUserRelationStat(mid); });
if (relationStat != null)
{
TabRightBanners.Add(new TabRightBanner
{
Id = 0,
IsEnabled = true,
LabelColor = DictionaryResource.GetColor("ColorPrimary"),
CountColor = DictionaryResource.GetColor("ColorPrimary"),
Label = DictionaryResource.GetString("FollowingCount"),
Count = Format.FormatNumber(relationStat.Following)
});
TabRightBanners.Add(new TabRightBanner
{
Id = 1,
IsEnabled = true,
LabelColor = DictionaryResource.GetColor("ColorPrimary"),
CountColor = DictionaryResource.GetColor("ColorPrimary"),
Label = DictionaryResource.GetString("FollowerCount"),
Count = Format.FormatNumber(relationStat.Follower)
});
}
// UP主状态数需要任意用户登录否则不会返回任何数据
UpStat upStat = null;
await Task.Run(() => { upStat = UserStatus.GetUpStat(mid); });
if (upStat != null && upStat.Archive != null && upStat.Article != null)
{
TabRightBanners.Add(new TabRightBanner
{
Id = 2,
IsEnabled = false,
LabelColor = DictionaryResource.GetColor("ColorTextGrey"),
CountColor = DictionaryResource.GetColor("ColorTextDark"),
Label = DictionaryResource.GetString("LikesCount"),
Count = Format.FormatNumber(upStat.Likes)
});
long archiveView = 0;
if (upStat.Archive != null)
{
archiveView = upStat.Archive.View;
}
TabRightBanners.Add(new TabRightBanner
{
Id = 3,
IsEnabled = false,
LabelColor = DictionaryResource.GetColor("ColorTextGrey"),
CountColor = DictionaryResource.GetColor("ColorTextDark"),
Label = DictionaryResource.GetString("ArchiveViewCount"),
Count = Format.FormatNumber(archiveView)
});
long articleView = 0;
if (upStat.Article != null)
{
articleView = upStat.Article.View;
}
TabRightBanners.Add(new TabRightBanner
{
Id = 4,
IsEnabled = false,
LabelColor = DictionaryResource.GetColor("ColorTextGrey"),
CountColor = DictionaryResource.GetColor("ColorTextDark"),
Label = DictionaryResource.GetString("ArticleViewCount"),
Count = Format.FormatNumber(articleView)
});
}
}
/// <summary>
/// 接收mid参数
/// </summary>
/// <param name="navigationContext"></param>
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
// 根据传入参数不同执行不同任务
long parameter = navigationContext.Parameters.GetValue<long>("Parameter");
if (parameter == 0)
{
return;
}
mid = parameter;
InitView();
UpdateSpaceInfo();
}
}