ChatGPTAITranslationContentViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Microsoft.Office.Core;
  2. using Microsoft.Win32;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Model;
  5. using PDF_Master.Properties;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Threading;
  17. namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
  18. {
  19. public class ChatGPTAITranslationContentViewModel : BindableBase, INavigationAware
  20. {
  21. public HomeContentViewModel homeContentViewModel = null;
  22. public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  23. public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  24. private int fromlanguageIndex = 0;
  25. public int FromlanguageIndex
  26. {
  27. get { return fromlanguageIndex; }
  28. set
  29. {
  30. SetProperty(ref fromlanguageIndex, value);
  31. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  32. }
  33. }
  34. private int tolanguageIndex = 0;
  35. public int TolanguageIndex
  36. {
  37. get { return tolanguageIndex; }
  38. set
  39. {
  40. SetProperty(ref tolanguageIndex, value);
  41. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  42. }
  43. }
  44. private Visibility errorTipVisible=Visibility.Collapsed;
  45. public Visibility ErrorTipVisible
  46. {
  47. get
  48. {
  49. return errorTipVisible;
  50. }
  51. set
  52. {
  53. SetProperty(ref errorTipVisible, value);
  54. if (value == Visibility.Visible) {
  55. dispatcherTimer.Start();
  56. }
  57. }
  58. }
  59. private string errorTipText = "The uploaded file cannot exceed 10MB";
  60. public string ErrorTipText
  61. {
  62. get
  63. {
  64. return errorTipText;
  65. }
  66. set
  67. {
  68. SetProperty(ref errorTipText, value);
  69. }
  70. }
  71. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  72. private void GetFromlanguageOrigin()
  73. {
  74. FromlanguageFamily.Clear();
  75. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  76. }
  77. public List<string> TolanguageFamily { set; get; } = new List<string>();
  78. private DispatcherTimer dispatcherTimer = new DispatcherTimer();
  79. private void GetTolanguageOrigin()
  80. {
  81. TolanguageFamily.Clear();
  82. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  83. }
  84. public DelegateCommand SelectFilesCommand { get; set; }
  85. public ChatGPTAITranslationContentViewModel()
  86. {
  87. SelectFilesCommand = new DelegateCommand(selectFiles);
  88. dispatcherTimer.Interval = TimeSpan.FromSeconds(5);
  89. dispatcherTimer.Tick += Dispatchertimer_Tick;
  90. init();
  91. }
  92. private void init()
  93. {
  94. GetFromlanguageOrigin();
  95. GetTolanguageOrigin();
  96. }
  97. private void Dispatchertimer_Tick(object sender, EventArgs e)
  98. {
  99. ErrorTipVisible = Visibility.Collapsed;
  100. dispatcherTimer.Stop();
  101. }
  102. public async void selectFiles()
  103. {
  104. ////添加付费拦截锁
  105. //if (!ViewContentViewModel.IAPBeforeFunction())
  106. //{
  107. // return;
  108. //}
  109. string word = Properties.Resources.wordex;
  110. string pdf = Properties.Resources.pdf;
  111. string allfiles = pdf + word;
  112. OpenFileDialog dialog = new OpenFileDialog();
  113. //dialog.Multiselect = true;
  114. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  115. $"Pdf({pdf})|{pdf}|" +
  116. $"Microsoft Office Word({word})|{word}");
  117. if ((bool)dialog.ShowDialog())
  118. {
  119. ErrorTipVisible = Visibility.Collapsed;
  120. FileInfo fileInfo = new FileInfo(dialog.FileName);
  121. if ((((float)fileInfo.Length) / 1024 / 1024) > 30) {
  122. ErrorTipVisible = Visibility.Visible;
  123. return;
  124. }
  125. string newfile = "";
  126. await Task.Run(async delegate
  127. {
  128. newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
  129. });
  130. if (!string.IsNullOrEmpty(newfile))
  131. {
  132. if (File.Exists(newfile))
  133. {
  134. if (homeContentViewModel != null)
  135. {
  136. homeContentViewModel.OpenFile(new string[] { newfile });
  137. return;
  138. }
  139. }
  140. }
  141. //newfile code
  142. ErrorTipText = newfile;
  143. ErrorTipVisible =Visibility.Visible;
  144. }
  145. }
  146. public void OnNavigatedTo(NavigationContext navigationContext)
  147. {
  148. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  149. }
  150. public bool IsNavigationTarget(NavigationContext navigationContext)
  151. {
  152. return true;
  153. }
  154. public void OnNavigatedFrom(NavigationContext navigationContext)
  155. {
  156. }
  157. }
  158. }