ShakerSettingViewModel.cs 7.6 KB

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