ChatGPTAITranslationContentViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. string word = Properties.Resources.wordex;
  105. string pdf = Properties.Resources.pdf;
  106. string allfiles = pdf + word;
  107. OpenFileDialog dialog = new OpenFileDialog();
  108. //dialog.Multiselect = true;
  109. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  110. $"Pdf({pdf})|{pdf}|" +
  111. $"Microsoft Office Word({word})|{word}");
  112. if ((bool)dialog.ShowDialog())
  113. {
  114. FileInfo fileInfo = new FileInfo(dialog.FileName);
  115. if ((((float)fileInfo.Length) / 1024 / 1024) > 30) {
  116. ErrorTipVisible = Visibility.Visible;
  117. return;
  118. }
  119. string newfile = "";
  120. await Task.Run(async delegate
  121. {
  122. newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
  123. });
  124. if (!string.IsNullOrEmpty(newfile))
  125. {
  126. if (File.Exists(newfile))
  127. {
  128. if (homeContentViewModel != null)
  129. {
  130. homeContentViewModel.OpenFile(new string[] { newfile });
  131. return;
  132. }
  133. }
  134. }
  135. //newfile code
  136. ErrorTipVisible =Visibility.Visible;
  137. }
  138. }
  139. public void OnNavigatedTo(NavigationContext navigationContext)
  140. {
  141. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  142. }
  143. public bool IsNavigationTarget(NavigationContext navigationContext)
  144. {
  145. return true;
  146. }
  147. public void OnNavigatedFrom(NavigationContext navigationContext)
  148. {
  149. }
  150. }
  151. }