ChatGPTAITranslationContentViewModel.cs 5.8 KB

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