/src/XamlViewer/ViewModels/DataViewModel.cs

https://github.com/huangjia2107/XamlViewer · C# · 244 lines · 198 code · 46 blank · 0 comment · 11 complexity · e6fd9b85b328bef6c55a295fff3f28eb MD5 · raw file

  1. using System;
  2. using System.Windows;
  3. using System.Text.RegularExpressions;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using XamlService.Events;
  10. using XamlService.Payloads;
  11. using XamlUtil.Common;
  12. using XamlUtil.Net;
  13. using XamlTheme.Controls;
  14. using XamlViewer.Models;
  15. using XamlViewer.Utils;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18. namespace XamlViewer.ViewModels
  19. {
  20. public class DataViewModel : BindableBase
  21. {
  22. private AppData _appData = null;
  23. private IEventAggregator _eventAggregator = null;
  24. private IDialogService _dialogService = null;
  25. private TextEditorEx _textEditor = null;
  26. public DelegateCommand<TextEditorEx> LoadedCommand { get; private set; }
  27. public DelegateCommand DelayArrivedCommand { get; private set; }
  28. public DelegateCommand ClearCommand { get; private set; }
  29. public DelegateCommand FormatCommand { get; private set; }
  30. public DelegateCommand RequestCommand { get; private set; }
  31. public DataViewModel(IContainerExtension container, IEventAggregator eventAggregator)
  32. {
  33. _appData = container.Resolve<AppData>();
  34. _eventAggregator = eventAggregator;
  35. _dialogService = container.Resolve<IDialogService>();
  36. InitEvent();
  37. InitCommand();
  38. }
  39. #region Init
  40. private void InitEvent()
  41. {
  42. _eventAggregator.GetEvent<SettingChangedEvent>().Subscribe(OnSettingChanged, ThreadOption.PublisherThread, false);
  43. }
  44. private void InitCommand()
  45. {
  46. LoadedCommand = new DelegateCommand<TextEditorEx>(Loaded);
  47. DelayArrivedCommand = new DelegateCommand(DelayArrived);
  48. ClearCommand = new DelegateCommand(Clear);
  49. FormatCommand = new DelegateCommand(Format);
  50. RequestCommand = new DelegateCommand(RequestAsync);
  51. }
  52. #endregion
  53. #region Event
  54. private void OnSettingChanged(ValueWithGuid<EditorSetting> valueWithGuid)
  55. {
  56. FontFamily = valueWithGuid.Value.FontFamily;
  57. FontSize = valueWithGuid.Value.FontSize;
  58. WordWrap = valueWithGuid.Value.WordWrap;
  59. }
  60. #endregion
  61. #region Command
  62. private void Loaded(TextEditorEx textEditor)
  63. {
  64. _textEditor = textEditor;
  65. if(_textEditor != null)
  66. {
  67. _textEditor.LoadSyntaxHighlighting(AppDomain.CurrentDomain.BaseDirectory + "Assets\\Json.xshd");
  68. _textEditor.Text = JsonString;
  69. }
  70. UpdateByJsonString();
  71. if (IsSyncDataSource)
  72. UpdateDataSource(true);
  73. }
  74. private void DelayArrived()
  75. {
  76. JsonString = _textEditor?.Text;
  77. }
  78. private void Clear()
  79. {
  80. _textEditor.Text = JsonString = string.Empty;
  81. }
  82. private void Format()
  83. {
  84. var text = _textEditor.Text;
  85. if(string.IsNullOrWhiteSpace(text))
  86. return;
  87. JsonString = JToken.Parse(text).ToString(Formatting.Indented);
  88. _textEditor.Text = JsonString;
  89. }
  90. private async void RequestAsync()
  91. {
  92. try
  93. {
  94. if (string.IsNullOrWhiteSpace(RestApi) || !Regex.IsMatch(RestApi, @"^https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]$"))
  95. {
  96. _dialogService.ShowMessage("Please check the rest api and try again.", MessageButton.OK, MessageType.Error);
  97. return;
  98. }
  99. CanFetch = false;
  100. var json = await HttpUtil.GetString(RestApi);
  101. if(!string.IsNullOrWhiteSpace(json))
  102. json = JToken.Parse(json).ToString(Formatting.Indented);
  103. JsonString = json;
  104. _textEditor.Text = JsonString;
  105. CanFetch = true;
  106. }
  107. catch(Exception ex)
  108. {
  109. System.Diagnostics.Trace.TraceError("[ Http GetString ] " + XamlUtil.Common.Common.GetExceptionStringFormat(ex));
  110. _dialogService.ShowMessage(ex.Message, MessageButton.OK, MessageType.Error);
  111. }
  112. finally
  113. {
  114. CanFetch = true;
  115. }
  116. }
  117. #endregion
  118. private string _fontFamily = "Calibri";
  119. public string FontFamily
  120. {
  121. get { return _fontFamily; }
  122. set { SetProperty(ref _fontFamily, value); }
  123. }
  124. private double _fontSize = 12d;
  125. public double FontSize
  126. {
  127. get { return _fontSize; }
  128. set { SetProperty(ref _fontSize, value); }
  129. }
  130. private bool _wordWrap = false;
  131. public bool WordWrap
  132. {
  133. get { return _wordWrap; }
  134. set { SetProperty(ref _wordWrap, value); }
  135. }
  136. public bool IsSyncDataSource
  137. {
  138. get { return _appData.Config.IsSyncDataSource; }
  139. set
  140. {
  141. if(_appData.Config.IsSyncDataSource == value)
  142. return;
  143. _appData.Config.IsSyncDataSource = value;
  144. RaisePropertyChanged();
  145. UpdateDataSource(value);
  146. }
  147. }
  148. private bool _canClear = true;
  149. public bool CanClear
  150. {
  151. get { return _canClear; }
  152. set { SetProperty(ref _canClear, value); }
  153. }
  154. private bool _canFetch;
  155. public bool CanFetch
  156. {
  157. get { return _canFetch; }
  158. set { SetProperty(ref _canFetch, value); }
  159. }
  160. private string _restApi;
  161. public string RestApi
  162. {
  163. get { return _restApi; }
  164. set
  165. {
  166. SetProperty(ref _restApi, value);
  167. CanFetch = !string.IsNullOrWhiteSpace(_restApi);
  168. }
  169. }
  170. public string JsonString
  171. {
  172. get { return _appData.Config.DataSourceJsonString; }
  173. set
  174. {
  175. if (_appData.Config.DataSourceJsonString == value)
  176. return;
  177. _appData.Config.DataSourceJsonString = value;
  178. UpdateByJsonString();
  179. UpdateDataSource(IsSyncDataSource);
  180. }
  181. }
  182. private Visibility _jsonTipVisibility = Visibility.Visible;
  183. public Visibility JsonTipVisibility
  184. {
  185. get { return _jsonTipVisibility; }
  186. set { SetProperty(ref _jsonTipVisibility, value); }
  187. }
  188. private void UpdateDataSource(bool isSync)
  189. {
  190. _eventAggregator.GetEvent<SyncDataSourceEvent>().Publish(isSync ? JsonString?.Trim() : null);
  191. }
  192. private void UpdateByJsonString()
  193. {
  194. CanClear = !string.IsNullOrEmpty(JsonString);
  195. JsonTipVisibility = CanClear ? Visibility.Collapsed : Visibility.Visible;
  196. }
  197. }
  198. }