You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

OnlineConfigViewModel.cs 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using ReactiveUI;
  2. using ReactiveUI.Fody.Helpers;
  3. using ReactiveUI.Validation.Extensions;
  4. using ReactiveUI.Validation.Helpers;
  5. using Shadowsocks.WPF.Localization;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Reactive;
  11. using System.Reactive.Linq;
  12. using System.Text;
  13. using System.Windows;
  14. namespace Shadowsocks.WPF.ViewModels
  15. {
  16. public class OnlineConfigViewModel : ReactiveValidationObject
  17. {
  18. public OnlineConfigViewModel()
  19. {
  20. Sources = new ObservableCollection<string>();
  21. SelectedSource = "";
  22. Address = "";
  23. // TODO in v5: if http:// show warning as materialDesign:HintAssist.HelperText
  24. AddressRule = this.ValidationRule(
  25. viewModel => viewModel.Address,
  26. address => address.StartsWith("http://"),
  27. "Warning: getting online configuration from plain HTTP sources is NOT secure!");
  28. var canUpdateCopyRemove = this.WhenAnyValue(
  29. x => x.SelectedSource,
  30. selectedSource => !string.IsNullOrWhiteSpace(selectedSource));
  31. var canUpdateAll = this.WhenAnyValue(
  32. x => x.Sources.Count,
  33. count => count > 0);
  34. var canAdd = this.WhenAnyValue(
  35. x => x.Address,
  36. address => Uri.IsWellFormedUriString(address, UriKind.Absolute) &&
  37. (address.StartsWith("https://") || address.StartsWith("http://")));
  38. //Update = ReactiveCommand.CreateFromTask(() => _controller.UpdateOnlineConfig(SelectedSource), canUpdateCopyRemove);
  39. //UpdateAll = ReactiveCommand.CreateFromTask(_controller.UpdateAllOnlineConfig, canUpdateAll);
  40. CopyLink = ReactiveCommand.Create(() => Clipboard.SetText(SelectedSource), canUpdateCopyRemove);
  41. Remove = ReactiveCommand.Create(() =>
  42. {
  43. bool result;
  44. var urlToRemove = SelectedSource; // save it here because SelectedSource is lost once we remove the selection
  45. do
  46. {
  47. result = Sources.Remove(urlToRemove);
  48. } while (result);
  49. //_controller.RemoveOnlineConfig(urlToRemove);
  50. }, canUpdateCopyRemove);
  51. Add = ReactiveCommand.Create(() =>
  52. {
  53. Sources.Add(Address);
  54. SelectedSource = Address;
  55. //_controller.SaveOnlineConfigSource(Sources.ToList());
  56. Address = "";
  57. }, canAdd);
  58. // TODO in v5: use MaterialDesignThemes snackbar messages
  59. this.WhenAnyObservable(x => x.Update)
  60. .Subscribe(x =>
  61. {
  62. if (x)
  63. MessageBox.Show(LocalizationProvider.GetLocalizedValue<string>("sip008UpdateSuccess"));
  64. else
  65. MessageBox.Show(LocalizationProvider.GetLocalizedValue<string>("sip008UpdateFailure"));
  66. });
  67. this.WhenAnyObservable(x => x.UpdateAll)
  68. .Subscribe(x =>
  69. {
  70. if (x.Count == 0)
  71. MessageBox.Show(LocalizationProvider.GetLocalizedValue<string>("sip008UpdateAllSuccess"));
  72. else
  73. {
  74. var stringBuilder = new StringBuilder(LocalizationProvider.GetLocalizedValue<string>("sip008UpdateAllFailure"));
  75. foreach (var url in x)
  76. stringBuilder.AppendLine(url);
  77. MessageBox.Show(stringBuilder.ToString());
  78. }
  79. });
  80. }
  81. public ValidationHelper AddressRule { get; }
  82. public ReactiveCommand<Unit, bool> Update { get; }
  83. public ReactiveCommand<Unit, List<string>> UpdateAll { get; }
  84. public ReactiveCommand<Unit, Unit> CopyLink { get; }
  85. public ReactiveCommand<Unit, Unit> Remove { get; }
  86. public ReactiveCommand<Unit, Unit> Add { get; }
  87. [Reactive]
  88. public ObservableCollection<string> Sources { get; private set; }
  89. [Reactive]
  90. public string SelectedSource { get; set; }
  91. [Reactive]
  92. public string Address { get; set; }
  93. }
  94. }