ShakerSettingViewModel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Markup.Xaml;
  6. using CommunityToolkit.Mvvm.Input;
  7. using Shaker.Models;
  8. using ShakerApp.Models;
  9. using ShakerApp.Views;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics.CodeAnalysis;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Windows.Input;
  17. namespace ShakerApp.ViewModels
  18. {
  19. public class ShakerSettingViewModel : DisplayViewModelBase<Models.ShakerSettingModel>
  20. {
  21. public override bool CanResize => false;
  22. public override double Width => 960;
  23. public override double Height => 660;
  24. public const string LANGUAGECHANGEDEVENT = "LanguageChangedEvnet";
  25. private static string PlugnsPath =Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "Language");
  26. public Models.WorkingMode WorkingMode
  27. {
  28. get => Model.WorkingMode;
  29. set
  30. {
  31. SetProperty(ref Model.WorkingMode, value);
  32. if(value == Models.WorkingMode.Remote)
  33. {
  34. EnabledRemote = false;
  35. if(ServieIsStart)
  36. {
  37. StopService();
  38. }
  39. }
  40. else
  41. {
  42. }
  43. MenuViewModel.Instance["MenuManger"]!.IsVisible = value == WorkingMode.Local;
  44. MenuViewModel.Instance["MenuConnect"]!.IsVisible = value == WorkingMode.Remote;
  45. }
  46. }
  47. [PropertyAssociation(nameof(ShakerSettingModel.RemoteIP))]
  48. public string RemoteIP { get => Model.RemoteIP; set => SetProperty(ref Model.RemoteIP, value); }
  49. [PropertyAssociation(nameof(ShakerSettingModel.RemotePort))]
  50. public int RemotePort { get => Model.RemotePort; set => SetProperty(ref Model.RemotePort, value); }
  51. [PropertyAssociation(nameof(ShakerSettingModel.RemoteControlModel))]
  52. public Models.RemoteControlModel RemoteControlModel { get => Model.RemoteControlModel; set => SetProperty(ref Model.RemoteControlModel, value); }
  53. [PropertyAssociation(nameof(ShakerSettingModel.EnabledRemote))]
  54. public bool EnabledRemote { get => Model.EnabledRemote; set => SetProperty(ref Model.EnabledRemote, value); }
  55. [PropertyAssociation(nameof(ShakerSettingModel.AutoStartService))]
  56. public bool AutoStartService { get => Model.AutoStartService; set => SetProperty(ref Model.AutoStartService, value); }
  57. public bool ServieIsStart { get => servieIsStart; set =>SetProperty(ref servieIsStart ,value); }
  58. [AllowNull]
  59. private ILanguage.ILanguage currentLanguage;
  60. private bool servieIsStart = false;
  61. public AvaloniaList<ILanguage.ILanguage> Languages { get; } = new AvaloniaList<ILanguage.ILanguage>();
  62. public ILanguage.ILanguage CurrentLanguage
  63. {
  64. get => currentLanguage;
  65. set
  66. {
  67. var temp = value;
  68. if (temp == null) return;
  69. if (currentLanguage == value) return;
  70. ApplyLanguage(value);
  71. SetProperty(ref currentLanguage, value);
  72. GetEvent(LANGUAGECHANGEDEVENT).Publish(this, null);
  73. if (currentLanguage != null)
  74. {
  75. Language = currentLanguage.Key;
  76. }
  77. }
  78. }
  79. public override void Init()
  80. {
  81. LoadAllLanguages();
  82. }
  83. public ICommand SelectDirectoryCommand => new RelayCommand<string?>(SelectDirectory);
  84. private async void SelectDirectory(string? p)
  85. {
  86. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  87. {
  88. var folder = await TopLevel.GetTopLevel(desktop.MainWindow)!.StorageProvider.OpenFolderPickerAsync(new Avalonia.Platform.Storage.FolderPickerOpenOptions()
  89. {
  90. Title = p ==null ?"": App.Current?.FindResource(p) + "",
  91. AllowMultiple = false,
  92. });
  93. if(folder!=null && folder.Count>0)
  94. {
  95. DataDirectory = folder.First().Path.LocalPath;
  96. }
  97. }
  98. }
  99. private void ApplyLanguage(ILanguage.ILanguage language)
  100. {
  101. var rd = Avalonia.Application.Current?.Resources;
  102. if (rd == null || language == null) return;
  103. //RemoveLangThemes();
  104. //将资源加载在最后,优先使用;
  105. rd.MergedDictionaries.Add((ResourceDictionary)AvaloniaXamlLoader.Load(new Uri(language.ResourcePath, UriKind.Absolute)));
  106. var list = Languages.Where(x => x != language).ToList();
  107. List<ResourceDictionary> removeList = new List<ResourceDictionary>();
  108. foreach (var dictionary in rd.MergedDictionaries.Cast<ResourceDictionary>())
  109. {
  110. bool isExists = list.Exists(x => dictionary.TryGetResource("Name", null, out var val) && val + "" == x.Name);
  111. if (isExists)
  112. {
  113. removeList.Add(dictionary);
  114. }
  115. }
  116. foreach (var val in removeList)
  117. {
  118. rd.MergedDictionaries.Remove(val);
  119. }
  120. }
  121. private void LoadAllLanguages()
  122. {
  123. Languages.Clear();
  124. Tools.PluginsLoader.Defalut.Load<ILanguage.ILanguage>(PlugnsPath)
  125. .ForEach(x => Languages.Add(x));
  126. if (Languages.Count == 0)
  127. {
  128. MainViewModel.Default.ShowError("没有语言文件,程序错误退出");
  129. return;
  130. }
  131. CurrentLanguage = Languages.FirstOrDefault(x => x.Key == Model.Language) ?? Languages.First();
  132. }
  133. private readonly string ConfigPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "Config"), "ShakerConfig.json");
  134. [PropertyAssociation(nameof(ShakerSettingModel.Language))]
  135. public string Language { get => Model.Language; set => SetProperty(ref Model.Language, value); }
  136. [PropertyAssociation(nameof(ShakerSettingModel.DataDirectory))]
  137. public string DataDirectory
  138. {
  139. get
  140. {
  141. if(!System.IO.Directory.Exists(Model.DataDirectory))
  142. {
  143. Directory.CreateDirectory(Model.DataDirectory);
  144. }
  145. return Model.DataDirectory;
  146. }
  147. set => SetProperty(ref Model.DataDirectory, value);
  148. }
  149. private ShakerSettingViewModel()
  150. {
  151. Content = typeof(Views.ShakerSettingView);
  152. Pages.AddRange(this.GetType().Assembly.GetTypes()
  153. .Where(x => x.GetInterface(typeof(Views.ISettingPageView).FullName!) != null && x.IsAnsiClass && !x.IsAbstract && x.GetCustomAttribute<SettingPageAttribute>()!=null)
  154. .Select(x=>
  155. {
  156. var att = x.GetCustomAttribute<SettingPageAttribute>();
  157. return (att!.Index, x);
  158. })
  159. .OrderBy(x=>x.Index)
  160. .Select(x=>x.x));
  161. var fileinfp = new FileInfo(ConfigPath);
  162. if (!System.IO.Directory.Exists(fileinfp.DirectoryName)) Directory.CreateDirectory(fileinfp.DirectoryName!);
  163. var model = Tools.Tools.ReadConfig<Models.ShakerSettingModel>(ConfigPath);
  164. if (model == null)
  165. {
  166. Save();
  167. }
  168. }
  169. static ShakerSettingViewModel()
  170. {
  171. }
  172. public AvaloniaList<Type> Pages { get; } = new AvaloniaList<Type>();
  173. public override void InitData()
  174. {
  175. base.InitData();
  176. }
  177. public ICommand StartServiceCommand => new RelayCommand(StartService);
  178. public ICommand StopServiceCommand => new RelayCommand(StopService);
  179. private void StartService()
  180. {
  181. CommunicationViewModel.Instance.StartService("0.0.0.0", RemotePort);
  182. }
  183. public void StopService()
  184. {
  185. CommunicationViewModel.Instance.StopService();
  186. }
  187. protected override void Save()
  188. {
  189. base.Save();
  190. Tools.Tools.SaveConfig(Model, ConfigPath);
  191. }
  192. protected override bool SkipProperty(string? propertyName)
  193. {
  194. return (propertyName == nameof(Pages)
  195. || propertyName == nameof(ConfigPath)
  196. || propertyName == nameof(SelectDirectoryCommand)
  197. || propertyName == nameof(ServieIsStart));
  198. }
  199. public static ShakerSettingViewModel Instance { get; } = new ShakerSettingViewModel();
  200. }
  201. }