private void TranslatedTextChanged() { //Mainly responsible for button if (SelectedItem != null) { if (SelectedItem.Translated != TranslatedTextBox || !SelectedItem.IsTranslated) // if the translation doesnt match with the saved tr. or it isnt even saved { SubmitEnabled = true; } else { SubmitEnabled = false; } } if (TranslatedTextBox != null && OriginalTextBox != null) { WordCount = $"Word count: {OriginalTextBox.Length}/{TranslatedTextBox.Length}"; } //Responsible for autosave feature if (CurrentSettings.IsAutosave) { if (SelectedItem != null && autosaveCounter == 50) { autosaveCounter = 0; Task.Factory.StartNew(() => { TransaltionItemManager.DoAutosave(); }); Task.Factory.StartNew(() => { ActionInProgressText = "Autosaving..."; Thread.Sleep(2000); ActionInProgressText = ""; }); } else if (autosaveCounter < 51) { autosaveCounter++; } } }
public MainWindowViewModel(MainWindow mWindow) { //OriginalTextBox = "Welcome to out application. \n Here will display your unranslated text."; // TranslatedTextBox = "And here will be your translation if available."; TranslationItemListDisplay = new ObservableCollection <TranslationItem>(); TranslationItemListFull = new ObservableCollection <TranslationItem>(); DictionaryItemListDisplay = new ObservableCollection <DictionaryItem>(); DictionaryItemListFull = new ObservableCollection <DictionaryItem>(); TransaltionItemManager.Initialise(this); PersonalDictionaryManager.Initialise(this); SettingsManager.Initialise(this); CurrentSettings = SettingsManager.LoadSettings(); PersonalDictionaryManager.ManagePDLoad(PersonalDictionaryManager.OperationType.Load); foreach (var item in DictionaryItemListFull) { DictionaryItemListDisplay.Add(item); } DictionaryHasItems = DictionaryItemListFull.Count > 0; mWindow.UpdateSelectedText += (sender, e) => { if (e != null) { SelectedTextOriginal = e; } }; IsAutosaveEnabled = false; #region Command Declarations //Window control buttons MinimizeWindowCommand = new RelayCommand(() => { if (mWindow.WindowState != WindowState.Minimized) { mWindow.WindowState = WindowState.Minimized; } }); MaximizeWindowCommand = new RelayCommand(() => mWindow.WindowState ^= WindowState.Maximized); LoadFileCommand = new RelayCommand(() => { OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "Xliff, xml (*.xliff,*.xml)|*.xliff;*.xml;"; MessageBoxResult result = MessageBoxResult.None; if (file != null) { result = TransaltionItemManager.OptimisedSaveAll(); } if (result != MessageBoxResult.Cancel) { if (fd.ShowDialog() == true) { if (!string.IsNullOrEmpty(fd.FileName)) { TranslationItemListFull.Clear(); TranslationItemListDisplay.Clear(); bool isLoadSuccesful = TransaltionItemManager.LoadXmlFile(fd.FileName); DucumentListHasItems = TranslationItemListDisplay.Count > 0; Task.Factory.StartNew(() => { if (isLoadSuccesful) { ActionInProgressText = "Loaded Sussecfully!"; } else { ActionInProgressText = "Failed To load the file! There might be a problem!"; } Thread.Sleep(2000); ActionInProgressText = ""; }); } } } }); SaveTranslationCommand = new RelayCommand(() => { //bool translationHappened = false; TransaltionItemManager.SubmitTranslation(); }); CopyTextCommand = new RelayCommand(() => { if (!string.IsNullOrEmpty(TranslatedTextBox)) { Clipboard.SetText(TranslatedTextBox); Task.Factory.StartNew(() => { ActionInProgressText = "Text Copied Succesfully!"; Thread.Sleep(2000); ActionInProgressText = ""; }); } }); SaveAllCommand = new RelayCommand(() => { TransaltionItemManager.OptimisedSaveAllNoDialog(); PersonalDictionaryManager.ManageSavePD(); OrderTranslationList(); Task.Factory.StartNew(() => { ActionInProgressText = "Saving..."; Thread.Sleep(2000); ActionInProgressText = ""; }); }); SaveAsCommand = new RelayCommand(() => { if (file != null) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.DefaultExt = ".xliff"; saveFileDialog.Filter = "Xliff, xml (*.xliff,*.xml)|*.xliff;*.xml;"; if (saveFileDialog.ShowDialog() == true) { saveFileDialog.AddExtension = true; if (!string.IsNullOrEmpty(saveFileDialog.FileName)) { TransaltionItemManager.UnoptimisedSaveAllCustomPth(saveFileDialog.FileName); } } } }); ExitCommand = new RelayCommand(() => { if (CurrentSettings.IsAutosave) { TransaltionItemManager.OptimisedSaveAllNoDialog(); closeExecuted = true; mWindow.Close(); } if (TransaltionItemManager.OptimisedSaveAll() != MessageBoxResult.Cancel) { closeExecuted = true; mWindow.Close(); } PersonalDictionaryManager.ManageSavePD(); if (!UpdateManager.IsDeletedSettings) { SettingsManager.HandleSettingsSave(); } }); //Because it's first needs to be declared CloseWindowCommand = ExitCommand; AddPDItem = new RelayCommand(() => { PersonalDictionaryManager.ManageDictionaryItemAdding(); // MessageBox.Show(DictionaryItemListFull.Count.ToString()); }); DeletePDItemCommand = new RelayCommand(() => { if (SelectedPDItem != null) { DictionaryItemListFull.Remove(DictionaryItemListFull.Single(itm => itm.Translation.Equals(SelectedPDItem.Translation) && itm.Original.Equals(SelectedPDItem.Original))); // DictionaryItemListDisplay.Remove(SelectedPDItem); DictionaryItemListDisplay.Clear(); foreach (var item in DictionaryItemListFull) { DictionaryItemListDisplay.Add(item); } // PersonalDictionaryManager.UpdateDictionaryItemExistence(); DictionaryHasItems = DictionaryItemListFull.Count > 0; } }); SearchDefinitionCommand = new RelayCommand(() => { SearchTextBoxDictionary = SelectedTextOriginal; }); OpenSettingsCommand = new RelayCommand(() => { SettingsWindow settingsWindow = new SettingsWindow(); var dataContext = (SettingsWindowViewModel)settingsWindow.DataContext; dataContext.CurrentSettings = CurrentSettings; bool langChanged = false; if (settingsWindow.ShowDialog() == true) { if (CurrentSettings.TargetLanguage != dataContext.ReturnSettings.TargetLanguage) { PersonalDictionaryManager.ManageSavePD(); langChanged = true; } CurrentSettings = dataContext.ReturnSettings; if (langChanged) { PersonalDictionaryManager.ManagePDReload(); } } }); mWindow.Closed += (sender, e) => { if (!closeExecuted) { ExitCommand.Execute(null); } }; CheckForUpdateCommand = new RelayCommand(async() => { bool response = false; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Factory.StartNew(() => { while (!response) { ActionInProgressText = "Checking for updates. "; Thread.Sleep(500); ActionInProgressText = "Checking for updates.. "; Thread.Sleep(500); ActionInProgressText = "Checking for updates..."; Thread.Sleep(500); } ActionInProgressText = ""; }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed response = await UpdateManager.CheckForUpdateAsync(); }); #endregion }