ChatGPTAITranslationContentViewModel.cs 4.5 KB

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