HandleRepaetItemPageViewModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TranslateTool.Managers;
  5. using TranslateTool.Model;
  6. namespace TranslateTool.ViewModel
  7. {
  8. public class HandleRepaetItemPageViewModel:ViewModel
  9. {
  10. private readonly List<RepeatItem> _items;
  11. private readonly string _languageName;
  12. private readonly ClientManager _clientManager;
  13. private RepeatItem _repeatItem;
  14. private string _key;
  15. private List<string> _contents;
  16. private bool _isLastItem;
  17. private string _selectContent;
  18. private string _repeatMessage;
  19. public string RepeatMessage
  20. {
  21. get => _repeatMessage;
  22. set
  23. {
  24. if (_repeatMessage != value)
  25. {
  26. _repeatMessage = value;
  27. OnPropertyChanged(() => RepeatMessage);
  28. }
  29. }
  30. }
  31. public string SelectContent
  32. {
  33. get => _selectContent;
  34. set
  35. {
  36. if (_selectContent != value)
  37. {
  38. _selectContent = value;
  39. OnPropertyChanged(() => SelectContent);
  40. }
  41. }
  42. }
  43. public bool IsLastItem
  44. {
  45. get => _isLastItem;
  46. set
  47. {
  48. if (_isLastItem != value)
  49. {
  50. _isLastItem = value;
  51. OnPropertyChanged(() => IsLastItem);
  52. }
  53. }
  54. }
  55. public List<string> Contents
  56. {
  57. get => _contents;
  58. set
  59. {
  60. if (_contents != value)
  61. {
  62. _contents = value;
  63. SelectContent = string.Empty;
  64. OnPropertyChanged(() => Contents);
  65. }
  66. }
  67. }
  68. public string Key
  69. {
  70. get => _key;
  71. set
  72. {
  73. if (_key != value)
  74. {
  75. _key = value;
  76. OnPropertyChanged(() => Key);
  77. }
  78. }
  79. }
  80. public ButtonCommand NextCommand { get; }
  81. public ButtonCommand FinishCommand { get; }
  82. public HandleRepaetItemPageViewModel(string languageName, List<RepeatItem> repeatItems)
  83. {
  84. _clientManager = ClientManager.CreateInstance();
  85. _languageName = languageName;
  86. _items = repeatItems;
  87. NextCommand = new ButtonCommand(OnNext, "Next");
  88. FinishCommand = new ButtonCommand(OnFinish, "Finish");
  89. _repeatItem = repeatItems.First();
  90. Key = _repeatItem.Key;
  91. Contents = new List<string>(_repeatItem.Contents);
  92. RepeatMessage = $"有{_items.Count}条重复内容,请选择要使用的内容!";
  93. }
  94. private void OnFinish(object obj)
  95. {
  96. if (!string.IsNullOrEmpty(_selectContent))
  97. {
  98. _clientManager.UpdateItemValue(_languageName,_key,_selectContent);
  99. }
  100. OnClosedRequest();
  101. }
  102. private void OnNext(object obj)
  103. {
  104. if (!string.IsNullOrEmpty(_selectContent))
  105. {
  106. _clientManager.UpdateItemValue(_languageName, _key, _selectContent);
  107. }
  108. var index = _items.IndexOf(_repeatItem);
  109. if (index < _items.Count)
  110. {
  111. if (index == _items.Count - 2 )
  112. {
  113. IsLastItem = true;
  114. }
  115. _repeatItem = _items[index + 1];
  116. Key = _repeatItem.Key;
  117. Contents = new List<string>(_repeatItem.Contents);
  118. }
  119. }
  120. }
  121. }