SelectedTranslationDialogViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Master.Model;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using PDF_Master.Helper;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using static System.Resources.ResXFileRef;
  11. using static System.Net.Mime.MediaTypeNames;
  12. using System.Windows.Forms;
  13. using Microsoft.Office.Core;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using PDF_Master.Properties;
  17. using PDF_Master.CustomControl;
  18. using DialogResult = Prism.Services.Dialogs.DialogResult;
  19. namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
  20. {
  21. public class SelectedTranslationDialogViewModel : BindableBase, IDialogAware
  22. {
  23. #region 文案
  24. private string T_title;
  25. public string T_Title
  26. {
  27. get { return T_title; }
  28. set
  29. {
  30. SetProperty(ref T_title, value);
  31. }
  32. }
  33. private string T_limitTip;
  34. public string T_LimitTip
  35. {
  36. get { return T_limitTip; }
  37. set
  38. {
  39. SetProperty(ref T_limitTip, value);
  40. }
  41. }
  42. private string T_textBoxWatermark;
  43. public string T_TextBoxWatermark
  44. {
  45. get { return T_textBoxWatermark; }
  46. set
  47. {
  48. SetProperty(ref T_textBoxWatermark, value);
  49. }
  50. }
  51. private string T_copy;
  52. public string T_Copy
  53. {
  54. get { return T_copy; }
  55. set
  56. {
  57. SetProperty(ref T_copy, value);
  58. }
  59. }
  60. private string T_result;
  61. public string T_Result
  62. {
  63. get { return T_result; }
  64. set
  65. {
  66. SetProperty(ref T_result, value);
  67. }
  68. }
  69. private string T_translationBtn;
  70. public string T_TranslationBtn
  71. {
  72. get { return T_translationBtn; }
  73. set
  74. {
  75. SetProperty(ref T_translationBtn, value);
  76. }
  77. }
  78. private string T_translationLanguage;
  79. public string T_TranslationLanguage
  80. {
  81. get { return T_translationLanguage; }
  82. set
  83. {
  84. SetProperty(ref T_translationLanguage, value);
  85. }
  86. }
  87. private string T_copied;
  88. public string T_Copied
  89. {
  90. get { return T_copied; }
  91. set
  92. {
  93. SetProperty(ref T_copied, value);
  94. }
  95. }
  96. private void InitString()
  97. {
  98. T_Title = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationTitle");
  99. T_LimitTip = App.HomePageLoader.GetString("AISelectTranslationBtn_LimitTip");
  100. T_TextBoxWatermark = App.HomePageLoader.GetString("ChatGTPAI_TextBoxWatermark");
  101. T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage");
  102. T_Copy = App.HomePageLoader.GetString("ChatGTPAI_Copy");
  103. T_Result = App.HomePageLoader.GetString("ChatGTPAI_Result");
  104. T_TranslationBtn = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationBtn");
  105. T_Copied = App.HomePageLoader.GetString("ChatGTPAI_Copied");
  106. }
  107. #endregion
  108. #region 参数和属性
  109. public string Title => "";
  110. private string selectedText;
  111. public string SelectedText
  112. {
  113. get { return selectedText; }
  114. set
  115. {
  116. SetProperty(ref selectedText, value);
  117. }
  118. }
  119. private string translateText;
  120. public string TranslateText
  121. {
  122. get { return translateText; }
  123. set
  124. {
  125. SetProperty(ref translateText, value);
  126. }
  127. }
  128. private string errorTipText;
  129. public string ErrorTipText
  130. {
  131. get { return errorTipText; }
  132. set
  133. {
  134. SetProperty(ref errorTipText, value);
  135. }
  136. }
  137. /// <summary>
  138. /// 错误框显示
  139. /// </summary>
  140. private Visibility errorVisible = Visibility.Collapsed;
  141. public Visibility ErrorVisible
  142. {
  143. get
  144. {
  145. return errorVisible;
  146. }
  147. set
  148. {
  149. SetProperty(ref errorVisible, value);
  150. }
  151. }
  152. /// <summary>
  153. /// 进度条显示
  154. /// </summary>
  155. private Visibility progressVisible = Visibility.Collapsed;
  156. public Visibility ProgressVisible
  157. {
  158. get
  159. {
  160. return progressVisible;
  161. }
  162. set
  163. {
  164. SetProperty(ref progressVisible, value);
  165. }
  166. }
  167. private double processvalue = 0;
  168. /// <summary>
  169. /// 进度条当前值
  170. /// </summary>
  171. public double Value
  172. {
  173. get { return processvalue; }
  174. set
  175. {
  176. SetProperty(ref processvalue, value);
  177. }
  178. }
  179. private double maxValue = 10;
  180. /// <summary>
  181. /// 进度条最大值
  182. /// </summary>
  183. public double MaxValue
  184. {
  185. get { return maxValue; }
  186. set
  187. {
  188. SetProperty(ref maxValue, value);
  189. }
  190. }
  191. private Visibility showTip = Visibility.Collapsed;
  192. public Visibility ShowTip
  193. {
  194. get
  195. {
  196. return showTip;
  197. }
  198. set
  199. {
  200. SetProperty(ref showTip, value);
  201. }
  202. }
  203. private string fromlanguage = "";
  204. private string tolanguage = "";
  205. private int fromlanguageIndex = 0;
  206. public int FromlanguageIndex
  207. {
  208. get { return fromlanguageIndex; }
  209. set
  210. {
  211. SetProperty(ref fromlanguageIndex, value);
  212. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  213. }
  214. }
  215. /// <summary>
  216. /// 初始化翻译到语言列表
  217. /// </summary>
  218. private int tolanguageIndex = 0;
  219. public int TolanguageIndex
  220. {
  221. get { return tolanguageIndex; }
  222. set
  223. {
  224. SetProperty(ref tolanguageIndex, value);
  225. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  226. }
  227. }
  228. /// <summary>
  229. /// 初始化翻译前语言列表
  230. /// </summary>
  231. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  232. private void GetFromlanguageOrigin()
  233. {
  234. FromlanguageFamily.Clear();
  235. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  236. }
  237. public List<string> TolanguageFamily { set; get; } = new List<string>();
  238. private void GetTolanguageOrigin()
  239. {
  240. TolanguageFamily.Clear();
  241. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  242. }
  243. #endregion
  244. #region 委托声明
  245. public DelegateCommand TranslateCommand { get; set; }
  246. public DelegateCommand CopyCommand { get; set; }
  247. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  248. public IDialogService dialogs;
  249. #endregion
  250. public SelectedTranslationDialogViewModel(IDialogService dialogService)
  251. {
  252. dialogs = dialogService;
  253. TranslateCommand = new DelegateCommand(translate);
  254. CopyCommand = new DelegateCommand(copy);
  255. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  256. init();
  257. InitString();
  258. }
  259. #region 逻辑函数
  260. /// <summary>
  261. /// 初始化下拉列表,或其他
  262. /// </summary>
  263. private void init()
  264. {
  265. GetFromlanguageOrigin();
  266. GetTolanguageOrigin();
  267. }
  268. /// <summary>
  269. /// 检查文字是否超出范围
  270. /// </summary>
  271. /// <param name="e"></param>
  272. private void textBoxEnterCharactersTextChanged(object e)
  273. {
  274. var ConverterPreview = e as TextBoxEx;
  275. if (ConverterPreview != null)
  276. {
  277. //结果置空
  278. TranslateText = "";
  279. if (ConverterPreview.Text.Length > 150)
  280. {
  281. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  282. ErrorVisible = Visibility.Visible;
  283. }
  284. else {
  285. ErrorVisible = Visibility.Collapsed;
  286. }
  287. }
  288. }
  289. /// <summary>
  290. /// 文本翻译
  291. /// </summary>
  292. public async void translate()
  293. {
  294. //添加付费拦截锁
  295. if (!ServiceHelper.IAPBeforeFunction())
  296. {
  297. return;
  298. }
  299. #region 权益弹窗暂时关闭
  300. //bool flg = false;
  301. ////权益弹窗
  302. //if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  303. //{
  304. // DialogParameters value = new DialogParameters();
  305. // value.Add(ParameterNames.Open, "AI");
  306. // dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  307. // {
  308. // if (dialogResult.Result == ButtonResult.OK)
  309. // {
  310. // flg = true;
  311. // }
  312. // else
  313. // {
  314. // flg = false;
  315. // }
  316. // });
  317. // if (flg == false)
  318. // {
  319. // return;
  320. // }
  321. //}
  322. #endregion
  323. ErrorVisible = Visibility.Collapsed;
  324. ProgressVisible = Visibility.Visible;
  325. Value = 1;
  326. string translatetext = "";
  327. string Code = "";
  328. Value = 4;
  329. await Task.Run(delegate
  330. {
  331. Code = ChatGTPAIHelper.textTranslate(SelectedText, fromlanguage, tolanguage, ref translatetext);
  332. });
  333. Value = 7;
  334. if (Code != "200")
  335. {
  336. ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(Code);
  337. ErrorVisible = Visibility.Visible;
  338. }
  339. Value = 9;
  340. ProgressVisible = Visibility.Collapsed;
  341. TranslateText = translatetext;
  342. }
  343. /// <summary>
  344. /// 复制到剪切板
  345. /// </summary>
  346. public void copy()
  347. {
  348. try
  349. {
  350. System.Windows.Forms.Clipboard.SetText(TranslateText);
  351. ShowTip = Visibility.Visible;
  352. }
  353. catch { }
  354. }
  355. #endregion
  356. #region 构架行为
  357. public event Action<IDialogResult> RequestClose;
  358. public bool CanCloseDialog()
  359. {
  360. return true;
  361. }
  362. public void OnDialogClosed()
  363. {
  364. }
  365. public void OnDialogOpened(IDialogParameters parameters)
  366. {
  367. CPDFViewer pdfViewer = null;
  368. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  369. if (pdfViewer != null)
  370. {
  371. //获取选择的文字
  372. SelectedText = pdfViewer.GetSelectedText();
  373. if (SelectedText.Length > 150)
  374. {
  375. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  376. ErrorVisible = Visibility.Visible;
  377. }
  378. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  379. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  380. }
  381. }
  382. #endregion
  383. }
  384. }