123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using Microsoft.Office.Core;
- using Microsoft.Win32;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.Properties;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Threading;
- namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
- {
- public class ChatGPTAITranslationContentViewModel : BindableBase, INavigationAware
- {
- public HomeContentViewModel homeContentViewModel = null;
- public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
- public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
- private int fromlanguageIndex = 0;
- public int FromlanguageIndex
- {
- get { return fromlanguageIndex; }
- set
- {
- SetProperty(ref fromlanguageIndex, value);
- fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
- }
- }
- private int tolanguageIndex = 0;
- public int TolanguageIndex
- {
- get { return tolanguageIndex; }
- set
- {
- SetProperty(ref tolanguageIndex, value);
- tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
- }
- }
- private Visibility errorTipVisible=Visibility.Collapsed;
- public Visibility ErrorTipVisible
- {
- get
- {
- return errorTipVisible;
- }
- set
- {
- SetProperty(ref errorTipVisible, value);
- if (value == Visibility.Visible) {
- dispatcherTimer.Start();
- }
- }
- }
- private string errorTipText = "The uploaded file cannot exceed 10MB";
- public string ErrorTipText
- {
- get
- {
- return errorTipText;
- }
- set
- {
- SetProperty(ref errorTipText, value);
- }
- }
- public static List<string> FromlanguageFamily { set; get; } = new List<string>();
- private void GetFromlanguageOrigin()
- {
- FromlanguageFamily.Clear();
- FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
- }
- public List<string> TolanguageFamily { set; get; } = new List<string>();
- private DispatcherTimer dispatcherTimer = new DispatcherTimer();
- private void GetTolanguageOrigin()
- {
- TolanguageFamily.Clear();
- TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
- }
- public DelegateCommand SelectFilesCommand { get; set; }
- public ChatGPTAITranslationContentViewModel()
- {
- SelectFilesCommand = new DelegateCommand(selectFiles);
- dispatcherTimer.Interval = TimeSpan.FromSeconds(5);
- dispatcherTimer.Tick += Dispatchertimer_Tick;
- init();
- }
- private void init()
- {
- GetFromlanguageOrigin();
- GetTolanguageOrigin();
- }
- private void Dispatchertimer_Tick(object sender, EventArgs e)
- {
- ErrorTipVisible = Visibility.Collapsed;
- dispatcherTimer.Stop();
- }
- public async void selectFiles()
- {
- string word = Properties.Resources.wordex;
- string pdf = Properties.Resources.pdf;
- string allfiles = pdf + word;
- OpenFileDialog dialog = new OpenFileDialog();
- //dialog.Multiselect = true;
- dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
- $"Pdf({pdf})|{pdf}|" +
- $"Microsoft Office Word({word})|{word}");
- if ((bool)dialog.ShowDialog())
- {
- FileInfo fileInfo = new FileInfo(dialog.FileName);
- if ((((float)fileInfo.Length) / 1024 / 1024) > 30) {
- ErrorTipVisible = Visibility.Visible;
- return;
- }
- string newfile = "";
- await Task.Run(async delegate
- {
- newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
-
- });
- if (!string.IsNullOrEmpty(newfile))
- {
- if (File.Exists(newfile))
- {
- if (homeContentViewModel != null)
- {
- homeContentViewModel.OpenFile(new string[] { newfile });
- return;
- }
- }
- }
- //newfile code
- ErrorTipVisible =Visibility.Visible;
- }
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- }
- }
|