using DownKyi.Core.BiliApi.BiliUtils; using DownKyi.Core.FileName; using DownKyi.Core.Settings; using DownKyi.Core.Settings.Models; using DownKyi.Events; using DownKyi.Models; using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; namespace DownKyi.ViewModels.Settings { public class ViewVideoViewModel : BaseViewModel { public const string Tag = "PageSettingsVideo"; private bool isOnNavigatedTo; #region 页面属性申明 private List videoCodecs; public List VideoCodecs { get => videoCodecs; set => SetProperty(ref videoCodecs, value); } private string selectedVideoCodec; public string SelectedVideoCodec { get => selectedVideoCodec; set => SetProperty(ref selectedVideoCodec, value); } private List videoQualityList; public List VideoQualityList { get => videoQualityList; set => SetProperty(ref videoQualityList, value); } private Quality selectedVideoQuality; public Quality SelectedVideoQuality { get => selectedVideoQuality; set => SetProperty(ref selectedVideoQuality, value); } private List audioQualityList; public List AudioQualityList { get => audioQualityList; set => SetProperty(ref audioQualityList, value); } private Quality selectedAudioQuality; public Quality SelectedAudioQuality { get => selectedAudioQuality; set => SetProperty(ref selectedAudioQuality, value); } private bool isTranscodingFlvToMp4; public bool IsTranscodingFlvToMp4 { get => isTranscodingFlvToMp4; set => SetProperty(ref isTranscodingFlvToMp4, value); } private bool isUseDefaultDirectory; public bool IsUseDefaultDirectory { get => isUseDefaultDirectory; set => SetProperty(ref isUseDefaultDirectory, value); } private string saveVideoDirectory; public string SaveVideoDirectory { get => saveVideoDirectory; set => SetProperty(ref saveVideoDirectory, value); } private bool downloadAll; public bool DownloadAll { get { return downloadAll; } set { SetProperty(ref downloadAll, value); } } private bool downloadAudio; public bool DownloadAudio { get { return downloadAudio; } set { SetProperty(ref downloadAudio, value); } } private bool downloadVideo; public bool DownloadVideo { get { return downloadVideo; } set { SetProperty(ref downloadVideo, value); } } private bool downloadDanmaku; public bool DownloadDanmaku { get { return downloadDanmaku; } set { SetProperty(ref downloadDanmaku, value); } } private bool downloadSubtitle; public bool DownloadSubtitle { get { return downloadSubtitle; } set { SetProperty(ref downloadSubtitle, value); } } private bool downloadCover; public bool DownloadCover { get { return downloadCover; } set { SetProperty(ref downloadCover, value); } } private ObservableCollection selectedFileName; public ObservableCollection SelectedFileName { get => selectedFileName; set => SetProperty(ref selectedFileName, value); } private ObservableCollection optionalFields; public ObservableCollection OptionalFields { get => optionalFields; set => SetProperty(ref optionalFields, value); } private int selectedOptionalField; public int SelectedOptionalField { get => selectedOptionalField; set => SetProperty(ref selectedOptionalField, value); } private List fileNamePartTimeFormatList; public List FileNamePartTimeFormatList { get => fileNamePartTimeFormatList; set => SetProperty(ref fileNamePartTimeFormatList, value); } private string selectedFileNamePartTimeFormat; public string SelectedFileNamePartTimeFormat { get => selectedFileNamePartTimeFormat; set => SetProperty(ref selectedFileNamePartTimeFormat, value); } private List orderFormatList; public List OrderFormatList { get => orderFormatList; set => SetProperty(ref orderFormatList, value); } private OrderFormatDisplay orderFormatDisplay; public OrderFormatDisplay OrderFormatDisplay { get => orderFormatDisplay; set => SetProperty(ref orderFormatDisplay, value); } #endregion public ViewVideoViewModel(IEventAggregator eventAggregator) : base(eventAggregator) { #region 属性初始化 // 优先下载的视频编码 VideoCodecs = new List { "H.264/AVC", "H.265/HEVC", }; // 优先下载画质 VideoQualityList = Constant.GetResolutions(); // 优先下载音质 AudioQualityList = Constant.GetAudioQualities(); AudioQualityList.RemoveAt(3); // 文件命名格式 SelectedFileName = new ObservableCollection(); SelectedFileName.CollectionChanged += new NotifyCollectionChangedEventHandler((sender, e) => { // 当前显示的命名格式part List fileName = new List(); foreach (DisplayFileNamePart item in SelectedFileName) { fileName.Add(item.Id); } bool isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName); PublishTip(isSucceed); }); OptionalFields = new ObservableCollection(); foreach (FileNamePart item in Enum.GetValues(typeof(FileNamePart))) { string display = DisplayFileNamePart(item); OptionalFields.Add(new DisplayFileNamePart { Id = item, Title = display }); } SelectedOptionalField = -1; // 文件命名中的时间格式 FileNamePartTimeFormatList = new List { "yyyy-MM-dd", "yyyy.MM.dd", }; // 文件命名中的序号格式 OrderFormatList = new List { new OrderFormatDisplay{ Name = DictionaryResource.GetString("OrderFormatNatural"), OrderFormat = OrderFormat.NATURAL }, new OrderFormatDisplay{ Name = DictionaryResource.GetString("OrderFormatLeadingZeros"), OrderFormat = OrderFormat.LEADING_ZEROS }, }; #endregion } /// /// 导航到VideoDetail页面时执行 /// /// public override void OnNavigatedTo(NavigationContext navigationContext) { base.OnNavigatedTo(navigationContext); isOnNavigatedTo = true; // 优先下载的视频编码 VideoCodecs videoCodecs = SettingsManager.GetInstance().GetVideoCodecs(); SelectedVideoCodec = GetVideoCodecsString(videoCodecs); // 优先下载画质 int quality = SettingsManager.GetInstance().GetQuality(); SelectedVideoQuality = VideoQualityList.FirstOrDefault(t => { return t.Id == quality; }); // 优先下载音质 int audioQuality = SettingsManager.GetInstance().GetAudioQuality(); SelectedAudioQuality = AudioQualityList.FirstOrDefault(t => { return t.Id == audioQuality; }); // 是否下载flv视频后转码为mp4 AllowStatus isTranscodingFlvToMp4 = SettingsManager.GetInstance().IsTranscodingFlvToMp4(); IsTranscodingFlvToMp4 = isTranscodingFlvToMp4 == AllowStatus.YES; // 是否使用默认下载目录 AllowStatus isUseSaveVideoRootPath = SettingsManager.GetInstance().IsUseSaveVideoRootPath(); IsUseDefaultDirectory = isUseSaveVideoRootPath == AllowStatus.YES; // 默认下载目录 SaveVideoDirectory = SettingsManager.GetInstance().GetSaveVideoRootPath(); // 下载内容 VideoContentSettings videoContent = SettingsManager.GetInstance().GetVideoContent(); DownloadAudio = videoContent.DownloadAudio; DownloadVideo = videoContent.DownloadVideo; DownloadDanmaku = videoContent.DownloadDanmaku; DownloadSubtitle = videoContent.DownloadSubtitle; DownloadCover = videoContent.DownloadCover; if (DownloadAudio && DownloadVideo && DownloadDanmaku && DownloadSubtitle && DownloadCover) { DownloadAll = true; } else { DownloadAll = false; } // 文件命名格式 List fileNameParts = SettingsManager.GetInstance().GetFileNameParts(); SelectedFileName.Clear(); foreach (FileNamePart item in fileNameParts) { string display = DisplayFileNamePart(item); SelectedFileName.Add(new DisplayFileNamePart { Id = item, Title = display }); } // 文件命名中的时间格式 SelectedFileNamePartTimeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); // 文件命名中的序号格式 OrderFormat orderFormat = SettingsManager.GetInstance().GetOrderFormat(); OrderFormatDisplay = OrderFormatList.FirstOrDefault(t => { return t.OrderFormat == orderFormat; }); isOnNavigatedTo = false; } #region 命令申明 // 优先下载的视频编码事件 private DelegateCommand videoCodecsCommand; public DelegateCommand VideoCodecsCommand => videoCodecsCommand ?? (videoCodecsCommand = new DelegateCommand(ExecuteVideoCodecsCommand)); /// /// 优先下载的视频编码事件 /// /// private void ExecuteVideoCodecsCommand(string parameter) { VideoCodecs videoCodecs = GetVideoCodecs(parameter); bool isSucceed = SettingsManager.GetInstance().SetVideoCodecs(videoCodecs); PublishTip(isSucceed); } // 优先下载画质事件 private DelegateCommand videoQualityCommand; public DelegateCommand VideoQualityCommand => videoQualityCommand ?? (videoQualityCommand = new DelegateCommand(ExecuteVideoQualityCommand)); /// /// 优先下载画质事件 /// /// private void ExecuteVideoQualityCommand(object parameter) { if (!(parameter is Quality resolution)) { return; } bool isSucceed = SettingsManager.GetInstance().SetQuality(resolution.Id); PublishTip(isSucceed); } // 优先下载音质事件 private DelegateCommand audioQualityCommand; public DelegateCommand AudioQualityCommand => audioQualityCommand ?? (audioQualityCommand = new DelegateCommand(ExecuteAudioQualityCommand)); /// /// 优先下载音质事件 /// /// private void ExecuteAudioQualityCommand(object parameter) { if (!(parameter is Quality quality)) { return; } bool isSucceed = SettingsManager.GetInstance().SetAudioQuality(quality.Id); PublishTip(isSucceed); } // 是否下载flv视频后转码为mp4事件 private DelegateCommand isTranscodingFlvToMp4Command; public DelegateCommand IsTranscodingFlvToMp4Command => isTranscodingFlvToMp4Command ?? (isTranscodingFlvToMp4Command = new DelegateCommand(ExecuteIsTranscodingFlvToMp4Command)); /// /// 是否下载flv视频后转码为mp4事件 /// private void ExecuteIsTranscodingFlvToMp4Command() { AllowStatus isTranscodingFlvToMp4 = IsTranscodingFlvToMp4 ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().IsTranscodingFlvToMp4(isTranscodingFlvToMp4); PublishTip(isSucceed); } // 是否使用默认下载目录事件 private DelegateCommand isUseDefaultDirectoryCommand; public DelegateCommand IsUseDefaultDirectoryCommand => isUseDefaultDirectoryCommand ?? (isUseDefaultDirectoryCommand = new DelegateCommand(ExecuteIsUseDefaultDirectoryCommand)); /// /// 是否使用默认下载目录事件 /// private void ExecuteIsUseDefaultDirectoryCommand() { AllowStatus isUseDefaultDirectory = IsUseDefaultDirectory ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().IsUseSaveVideoRootPath(isUseDefaultDirectory); PublishTip(isSucceed); } // 修改默认下载目录事件 private DelegateCommand changeSaveVideoDirectoryCommand; public DelegateCommand ChangeSaveVideoDirectoryCommand => changeSaveVideoDirectoryCommand ?? (changeSaveVideoDirectoryCommand = new DelegateCommand(ExecuteChangeSaveVideoDirectoryCommand)); /// /// 修改默认下载目录事件 /// private void ExecuteChangeSaveVideoDirectoryCommand() { string directory = DialogUtils.SetDownloadDirectory(); if (directory == "") { return; } bool isSucceed = SettingsManager.GetInstance().SetSaveVideoRootPath(directory); PublishTip(isSucceed); if (isSucceed) { SaveVideoDirectory = directory; } } // 所有内容选择事件 private DelegateCommand downloadAllCommand; public DelegateCommand DownloadAllCommand => downloadAllCommand ?? (downloadAllCommand = new DelegateCommand(ExecuteDownloadAllCommand)); /// /// 所有内容选择事件 /// private void ExecuteDownloadAllCommand() { if (DownloadAll) { DownloadAudio = true; DownloadVideo = true; DownloadDanmaku = true; DownloadSubtitle = true; DownloadCover = true; } else { DownloadAudio = false; DownloadVideo = false; DownloadDanmaku = false; DownloadSubtitle = false; DownloadCover = false; } SetVideoContent(); } // 音频选择事件 private DelegateCommand downloadAudioCommand; public DelegateCommand DownloadAudioCommand => downloadAudioCommand ?? (downloadAudioCommand = new DelegateCommand(ExecuteDownloadAudioCommand)); /// /// 音频选择事件 /// private void ExecuteDownloadAudioCommand() { if (!DownloadAudio) { DownloadAll = false; } if (DownloadAudio && DownloadVideo && DownloadDanmaku && DownloadSubtitle && DownloadCover) { DownloadAll = true; } SetVideoContent(); } // 视频选择事件 private DelegateCommand downloadVideoCommand; public DelegateCommand DownloadVideoCommand => downloadVideoCommand ?? (downloadVideoCommand = new DelegateCommand(ExecuteDownloadVideoCommand)); /// /// 视频选择事件 /// private void ExecuteDownloadVideoCommand() { if (!DownloadVideo) { DownloadAll = false; } if (DownloadAudio && DownloadVideo && DownloadDanmaku && DownloadSubtitle && DownloadCover) { DownloadAll = true; } SetVideoContent(); } // 弹幕选择事件 private DelegateCommand downloadDanmakuCommand; public DelegateCommand DownloadDanmakuCommand => downloadDanmakuCommand ?? (downloadDanmakuCommand = new DelegateCommand(ExecuteDownloadDanmakuCommand)); /// /// 弹幕选择事件 /// private void ExecuteDownloadDanmakuCommand() { if (!DownloadDanmaku) { DownloadAll = false; } if (DownloadAudio && DownloadVideo && DownloadDanmaku && DownloadSubtitle && DownloadCover) { DownloadAll = true; } SetVideoContent(); } // 字幕选择事件 private DelegateCommand downloadSubtitleCommand; public DelegateCommand DownloadSubtitleCommand => downloadSubtitleCommand ?? (downloadSubtitleCommand = new DelegateCommand(ExecuteDownloadSubtitleCommand)); /// /// 字幕选择事件 /// private void ExecuteDownloadSubtitleCommand() { if (!DownloadSubtitle) { DownloadAll = false; } if (DownloadAudio && DownloadVideo && DownloadDanmaku && DownloadSubtitle && DownloadCover) { DownloadAll = true; } SetVideoContent(); } // 封面选择事件 private DelegateCommand downloadCoverCommand; public DelegateCommand DownloadCoverCommand => downloadCoverCommand ?? (downloadCoverCommand = new DelegateCommand(ExecuteDownloadCoverCommand)); /// /// 封面选择事件 /// private void ExecuteDownloadCoverCommand() { if (!DownloadCover) { DownloadAll = false; } if (DownloadAudio && DownloadVideo && DownloadDanmaku && DownloadSubtitle && DownloadCover) { DownloadAll = true; } SetVideoContent(); } // 选中文件名字段右键点击事件 private DelegateCommand selectedFileNameRightCommand; public DelegateCommand SelectedFileNameRightCommand => selectedFileNameRightCommand ?? (selectedFileNameRightCommand = new DelegateCommand(ExecuteSelectedFileNameRightCommand)); /// /// 选中文件名字段右键点击事件 /// /// private void ExecuteSelectedFileNameRightCommand(object parameter) { if (parameter == null) { return; } bool isSucceed = SelectedFileName.Remove((DisplayFileNamePart)parameter); if (!isSucceed) { PublishTip(isSucceed); return; } //List fileName = new List(); //foreach (DisplayFileNamePart item in SelectedFileName) //{ // fileName.Add(item.Id); //} //isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName); //PublishTip(isSucceed); SelectedOptionalField = -1; } // 可选文件名字段点击事件 private DelegateCommand optionalFieldsCommand; public DelegateCommand OptionalFieldsCommand => optionalFieldsCommand ?? (optionalFieldsCommand = new DelegateCommand(ExecuteOptionalFieldsCommand)); /// /// 可选文件名字段点击事件 /// /// private void ExecuteOptionalFieldsCommand(object parameter) { if (SelectedOptionalField == -1) { return; } SelectedFileName.Add((DisplayFileNamePart)parameter); List fileName = new List(); foreach (DisplayFileNamePart item in SelectedFileName) { fileName.Add(item.Id); } bool isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName); PublishTip(isSucceed); SelectedOptionalField = -1; } // 重置选中文件名字段 private DelegateCommand resetCommand; public DelegateCommand ResetCommand => resetCommand ?? (resetCommand = new DelegateCommand(ExecuteResetCommand)); /// /// 重置选中文件名字段 /// private void ExecuteResetCommand() { bool isSucceed = SettingsManager.GetInstance().SetFileNameParts(null); PublishTip(isSucceed); List fileNameParts = SettingsManager.GetInstance().GetFileNameParts(); SelectedFileName.Clear(); foreach (FileNamePart item in fileNameParts) { string display = DisplayFileNamePart(item); SelectedFileName.Add(new DisplayFileNamePart { Id = item, Title = display }); } SelectedOptionalField = -1; } // 文件命名中的时间格式事件 private DelegateCommand fileNamePartTimeFormatCommand; public DelegateCommand FileNamePartTimeFormatCommand => fileNamePartTimeFormatCommand ?? (fileNamePartTimeFormatCommand = new DelegateCommand(ExecuteFileNamePartTimeFormatCommand)); /// /// 文件命名中的时间格式事件 /// /// private void ExecuteFileNamePartTimeFormatCommand(object parameter) { if (!(parameter is string timeFormat)) { return; } bool isSucceed = SettingsManager.GetInstance().SetFileNamePartTimeFormat(timeFormat); PublishTip(isSucceed); } // 文件命名中的序号格式事件 private DelegateCommand orderFormatCommand; public DelegateCommand OrderFormatCommand => orderFormatCommand ?? (orderFormatCommand = new DelegateCommand(ExecuteOrderFormatCommandCommand)); /// /// 文件命名中的序号格式事件 /// /// private void ExecuteOrderFormatCommandCommand(object parameter) { if (!(parameter is OrderFormatDisplay orderFormatDisplay)) { return; } bool isSucceed = SettingsManager.GetInstance().SetOrderFormat(orderFormatDisplay.OrderFormat); PublishTip(isSucceed); } #endregion /// /// 返回VideoCodecs的字符串 /// /// /// private string GetVideoCodecsString(VideoCodecs videoCodecs) { string codec; switch (videoCodecs) { case Core.Settings.VideoCodecs.NONE: codec = ""; break; case Core.Settings.VideoCodecs.AVC: codec = "H.264/AVC"; break; case Core.Settings.VideoCodecs.HEVC: codec = "H.265/HEVC"; break; default: codec = ""; break; } return codec; } /// /// 返回VideoCodecs /// /// /// private VideoCodecs GetVideoCodecs(string str) { VideoCodecs videoCodecs; switch (str) { case "H.264/AVC": videoCodecs = Core.Settings.VideoCodecs.AVC; break; case "H.265/HEVC": videoCodecs = Core.Settings.VideoCodecs.HEVC; break; default: videoCodecs = Core.Settings.VideoCodecs.NONE; break; } return videoCodecs; } /// /// 保存下载视频内容到设置 /// private void SetVideoContent() { VideoContentSettings videoContent = new VideoContentSettings { DownloadAudio = DownloadAudio, DownloadVideo = DownloadVideo, DownloadDanmaku = DownloadDanmaku, DownloadSubtitle = DownloadSubtitle, DownloadCover = DownloadCover }; bool isSucceed = SettingsManager.GetInstance().SetVideoContent(videoContent); PublishTip(isSucceed); } /// /// 文件名字段显示 /// /// /// private string DisplayFileNamePart(FileNamePart item) { string display = string.Empty; switch (item) { case FileNamePart.ORDER: display = DictionaryResource.GetString("DisplayOrder"); break; case FileNamePart.SECTION: display = DictionaryResource.GetString("DisplaySection"); break; case FileNamePart.MAIN_TITLE: display = DictionaryResource.GetString("DisplayMainTitle"); break; case FileNamePart.PAGE_TITLE: display = DictionaryResource.GetString("DisplayPageTitle"); break; case FileNamePart.VIDEO_ZONE: display = DictionaryResource.GetString("DisplayVideoZone"); break; case FileNamePart.AUDIO_QUALITY: display = DictionaryResource.GetString("DisplayAudioQuality"); break; case FileNamePart.VIDEO_QUALITY: display = DictionaryResource.GetString("DisplayVideoQuality"); break; case FileNamePart.VIDEO_CODEC: display = DictionaryResource.GetString("DisplayVideoCodec"); break; case FileNamePart.VIDEO_PUBLISH_TIME: display = DictionaryResource.GetString("DisplayVideoPublishTime"); break; case FileNamePart.AVID: display = "avid"; break; case FileNamePart.BVID: display = "bvid"; break; case FileNamePart.CID: display = "cid"; break; case FileNamePart.UP_MID: display = DictionaryResource.GetString("DisplayUpMid"); break; case FileNamePart.UP_NAME: display = DictionaryResource.GetString("DisplayUpName"); break; } if (((int)item) >= 100) { display = HyphenSeparated.Hyphen[(int)item]; } if (display == " ") { display = DictionaryResource.GetString("DisplaySpace"); } return display; } /// /// 发送需要显示的tip /// /// private void PublishTip(bool isSucceed) { if (isOnNavigatedTo) { return; } if (isSucceed) { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingUpdated")); } else { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingFailed")); } } } }