using DownKyi.Core.Aria2cNet.Server; using DownKyi.Core.Settings; using DownKyi.Core.Utils.Validator; using DownKyi.Events; using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; using System.Collections.Generic; namespace DownKyi.ViewModels.Settings { public class ViewNetworkViewModel : BaseViewModel { public const string Tag = "PageSettingsNetwork"; private bool isOnNavigatedTo; #region 页面属性申明 private int ariaListenPort; public int AriaListenPort { get { return ariaListenPort; } set { SetProperty(ref ariaListenPort, value); } } private List ariaLogLevels; public List AriaLogLevels { get { return ariaLogLevels; } set { SetProperty(ref ariaLogLevels, value); } } private string selectedAriaLogLevel; public string SelectedAriaLogLevel { get { return selectedAriaLogLevel; } set { SetProperty(ref selectedAriaLogLevel, value); } } private List ariaMaxConcurrentDownloads; public List AriaMaxConcurrentDownloads { get { return ariaMaxConcurrentDownloads; } set { SetProperty(ref ariaMaxConcurrentDownloads, value); } } private int selectedAriaMaxConcurrentDownload; public int SelectedAriaMaxConcurrentDownload { get { return selectedAriaMaxConcurrentDownload; } set { SetProperty(ref selectedAriaMaxConcurrentDownload, value); } } private List ariaSplits; public List AriaSplits { get { return ariaSplits; } set { SetProperty(ref ariaSplits, value); } } private int selectedAriaSplit; public int SelectedAriaSplit { get { return selectedAriaSplit; } set { SetProperty(ref selectedAriaSplit, value); } } private int ariaMaxOverallDownloadLimit; public int AriaMaxOverallDownloadLimit { get { return ariaMaxOverallDownloadLimit; } set { SetProperty(ref ariaMaxOverallDownloadLimit, value); } } private int ariaMaxDownloadLimit; public int AriaMaxDownloadLimit { get { return ariaMaxDownloadLimit; } set { SetProperty(ref ariaMaxDownloadLimit, value); } } private bool isAriaHttpProxy; public bool IsAriaHttpProxy { get { return isAriaHttpProxy; } set { SetProperty(ref isAriaHttpProxy, value); } } private string ariaHttpProxy; public string AriaHttpProxy { get { return ariaHttpProxy; } set { SetProperty(ref ariaHttpProxy, value); } } private int ariaHttpProxyPort; public int AriaHttpProxyPort { get { return ariaHttpProxyPort; } set { SetProperty(ref ariaHttpProxyPort, value); } } private List ariaFileAllocations; public List AriaFileAllocations { get { return ariaFileAllocations; } set { SetProperty(ref ariaFileAllocations, value); } } private string selectedAriaFileAllocation; public string SelectedAriaFileAllocation { get { return selectedAriaFileAllocation; } set { SetProperty(ref selectedAriaFileAllocation, value); } } #endregion public ViewNetworkViewModel(IEventAggregator eventAggregator) : base(eventAggregator) { #region 属性初始化 // Aria的日志等级 AriaLogLevels = new List { "DEBUG", "INFO", "NOTICE", "WARN", "ERROR" }; // Aria同时下载数 AriaMaxConcurrentDownloads = new List(); for (int i = 1; i <= 10; i++) { AriaMaxConcurrentDownloads.Add(i); } // Aria最大线程数 AriaSplits = new List(); for (int i = 1; i <= 10; i++) { AriaSplits.Add(i); } // Aria文件预分配 AriaFileAllocations = new List { "NONE", "PREALLOC", "FALLOC" }; #endregion } /// /// 导航到VideoDetail页面时执行 /// /// public override void OnNavigatedTo(NavigationContext navigationContext) { base.OnNavigatedTo(navigationContext); isOnNavigatedTo = true; // Aria服务器端口 AriaListenPort = SettingsManager.GetInstance().GetAriaListenPort(); // Aria的日志等级 AriaConfigLogLevel ariaLogLevel = SettingsManager.GetInstance().GetAriaLogLevel(); SelectedAriaLogLevel = ariaLogLevel.ToString("G"); // Aria同时下载数 SelectedAriaMaxConcurrentDownload = SettingsManager.GetInstance().GetAriaMaxConcurrentDownloads(); // Aria最大线程数 SelectedAriaSplit = SettingsManager.GetInstance().GetAriaSplit(); // Aria下载速度限制 AriaMaxOverallDownloadLimit = SettingsManager.GetInstance().GetAriaMaxOverallDownloadLimit(); // Aria下载单文件速度限制 AriaMaxDownloadLimit = SettingsManager.GetInstance().GetAriaMaxDownloadLimit(); // 是否开启Aria http代理 AllowStatus isAriaHttpProxy = SettingsManager.GetInstance().IsAriaHttpProxy(); IsAriaHttpProxy = isAriaHttpProxy == AllowStatus.YES; // Aria的http代理的地址 AriaHttpProxy = SettingsManager.GetInstance().GetAriaHttpProxy(); // Aria的http代理的端口 AriaHttpProxyPort = SettingsManager.GetInstance().GetAriaHttpProxyListenPort(); // Aria文件预分配 AriaConfigFileAllocation ariaFileAllocation = SettingsManager.GetInstance().GetAriaFileAllocation(); SelectedAriaFileAllocation = ariaFileAllocation.ToString("G"); isOnNavigatedTo = false; } #region 命令申明 // Aria服务器端口事件 private DelegateCommand ariaListenPortCommand; public DelegateCommand AriaListenPortCommand => ariaListenPortCommand ?? (ariaListenPortCommand = new DelegateCommand(ExecuteAriaListenPortCommand)); /// /// Aria服务器端口事件 /// /// private void ExecuteAriaListenPortCommand(string parameter) { int listenPort = (int)Number.GetInt(parameter); AriaListenPort = listenPort; bool isSucceed = SettingsManager.GetInstance().SetAriaListenPort(AriaListenPort); PublishTip(isSucceed); } // Aria的日志等级事件 private DelegateCommand ariaLogLevelsCommand; public DelegateCommand AriaLogLevelsCommand => ariaLogLevelsCommand ?? (ariaLogLevelsCommand = new DelegateCommand(ExecuteAriaLogLevelsCommand)); /// /// Aria的日志等级事件 /// /// private void ExecuteAriaLogLevelsCommand(string parameter) { AriaConfigLogLevel ariaLogLevel; switch (parameter) { case "DEBUG": ariaLogLevel = AriaConfigLogLevel.DEBUG; break; case "INFO": ariaLogLevel = AriaConfigLogLevel.INFO; break; case "NOTICE": ariaLogLevel = AriaConfigLogLevel.NOTICE; break; case "WARN": ariaLogLevel = AriaConfigLogLevel.WARN; break; case "ERROR": ariaLogLevel = AriaConfigLogLevel.ERROR; break; default: ariaLogLevel = AriaConfigLogLevel.INFO; break; } bool isSucceed = SettingsManager.GetInstance().SetAriaLogLevel(ariaLogLevel); PublishTip(isSucceed); } // Aria同时下载数事件 private DelegateCommand ariaMaxConcurrentDownloadsCommand; public DelegateCommand AriaMaxConcurrentDownloadsCommand => ariaMaxConcurrentDownloadsCommand ?? (ariaMaxConcurrentDownloadsCommand = new DelegateCommand(ExecuteAriaMaxConcurrentDownloadsCommand)); /// /// Aria同时下载数事件 /// /// private void ExecuteAriaMaxConcurrentDownloadsCommand(object parameter) { SelectedAriaMaxConcurrentDownload = (int)parameter; bool isSucceed = SettingsManager.GetInstance().SetAriaMaxConcurrentDownloads(SelectedAriaMaxConcurrentDownload); PublishTip(isSucceed); } // Aria最大线程数事件 private DelegateCommand ariaSplitsCommand; public DelegateCommand AriaSplitsCommand => ariaSplitsCommand ?? (ariaSplitsCommand = new DelegateCommand(ExecuteAriaSplitsCommand)); /// /// Aria最大线程数事件 /// /// private void ExecuteAriaSplitsCommand(object parameter) { SelectedAriaSplit = (int)parameter; bool isSucceed = SettingsManager.GetInstance().SetAriaSplit(SelectedAriaSplit); PublishTip(isSucceed); } // Aria下载速度限制事件 private DelegateCommand ariaMaxOverallDownloadLimitCommand; public DelegateCommand AriaMaxOverallDownloadLimitCommand => ariaMaxOverallDownloadLimitCommand ?? (ariaMaxOverallDownloadLimitCommand = new DelegateCommand(ExecuteAriaMaxOverallDownloadLimitCommand)); /// /// Aria下载速度限制事件 /// /// private void ExecuteAriaMaxOverallDownloadLimitCommand(string parameter) { int downloadLimit = (int)Number.GetInt(parameter); AriaMaxOverallDownloadLimit = downloadLimit; bool isSucceed = SettingsManager.GetInstance().SetAriaMaxOverallDownloadLimit(AriaMaxOverallDownloadLimit); PublishTip(isSucceed); } // Aria下载单文件速度限制事件 private DelegateCommand ariaMaxDownloadLimitCommand; public DelegateCommand AriaMaxDownloadLimitCommand => ariaMaxDownloadLimitCommand ?? (ariaMaxDownloadLimitCommand = new DelegateCommand(ExecuteAriaMaxDownloadLimitCommand)); /// /// Aria下载单文件速度限制事件 /// /// private void ExecuteAriaMaxDownloadLimitCommand(string parameter) { int downloadLimit = (int)Number.GetInt(parameter); AriaMaxDownloadLimit = downloadLimit; bool isSucceed = SettingsManager.GetInstance().SetAriaMaxDownloadLimit(AriaMaxDownloadLimit); PublishTip(isSucceed); } // 是否开启Aria http代理事件 private DelegateCommand isAriaHttpProxyCommand; public DelegateCommand IsAriaHttpProxyCommand => isAriaHttpProxyCommand ?? (isAriaHttpProxyCommand = new DelegateCommand(ExecuteIsAriaHttpProxyCommand)); /// /// 是否开启Aria http代理事件 /// private void ExecuteIsAriaHttpProxyCommand() { AllowStatus isAriaHttpProxy = IsAriaHttpProxy ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().IsAriaHttpProxy(isAriaHttpProxy); PublishTip(isSucceed); } // Aria的http代理的地址事件 private DelegateCommand ariaHttpProxyCommand; public DelegateCommand AriaHttpProxyCommand => ariaHttpProxyCommand ?? (ariaHttpProxyCommand = new DelegateCommand(ExecuteAriaHttpProxyCommand)); /// /// Aria的http代理的地址事件 /// /// private void ExecuteAriaHttpProxyCommand(string parameter) { bool isSucceed = SettingsManager.GetInstance().SetAriaHttpProxy(parameter); PublishTip(isSucceed); } // Aria的http代理的端口事件 private DelegateCommand ariaHttpProxyPortCommand; public DelegateCommand AriaHttpProxyPortCommand => ariaHttpProxyPortCommand ?? (ariaHttpProxyPortCommand = new DelegateCommand(ExecuteAriaHttpProxyPortCommand)); /// /// Aria的http代理的端口事件 /// /// private void ExecuteAriaHttpProxyPortCommand(string parameter) { int httpProxyPort = (int)Number.GetInt(parameter); AriaHttpProxyPort = httpProxyPort; bool isSucceed = SettingsManager.GetInstance().SetAriaHttpProxyListenPort(AriaHttpProxyPort); PublishTip(isSucceed); } // Aria文件预分配事件 private DelegateCommand ariaFileAllocationsCommand; public DelegateCommand AriaFileAllocationsCommand => ariaFileAllocationsCommand ?? (ariaFileAllocationsCommand = new DelegateCommand(ExecuteAriaFileAllocationsCommand)); /// /// Aria文件预分配事件 /// /// private void ExecuteAriaFileAllocationsCommand(string parameter) { AriaConfigFileAllocation ariaFileAllocation; switch (parameter) { case "NONE": ariaFileAllocation = AriaConfigFileAllocation.NONE; break; case "PREALLOC": ariaFileAllocation = AriaConfigFileAllocation.PREALLOC; break; case "FALLOC": ariaFileAllocation = AriaConfigFileAllocation.FALLOC; break; default: ariaFileAllocation = AriaConfigFileAllocation.PREALLOC; break; } bool isSucceed = SettingsManager.GetInstance().SetAriaFileAllocation(ariaFileAllocation); PublishTip(isSucceed); } #endregion /// /// 发送需要显示的tip /// /// private void PublishTip(bool isSucceed) { if (isOnNavigatedTo) { return; } if (isSucceed) { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingUpdated")); } else { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingFailed")); } } } }