/// <summary> /// Handle Delete button click. /// </summary> private async void OnDeleteClauseBtn_Click(object sender, RoutedEventArgs e) { if (Keyboard.Modifiers != ModifierKeys.Control && MessageBox.Show(this, String.Format(PrgResources.TheClauseDeletionConfirmation, wordEdit.Text), PrgResources.QuestionCaption, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) { return; } int id = clause.Id; await dbFacade.RemoveClausesAsync(id); SoundManager.RemoveFromCache(id, clause.Sound, dbFacade.DataSource); //Remove clause's cache dataWasUpdated = true; //Data was changed int idx = clausesIdsLst.IndexOf(id); if (idx != -1) { clausesIdsLst.RemoveAt(idx); } ClausesWereUpdated?.Invoke(); if (clausesIdsLst.Count == 0) { //There are no clauses to move at, so leave empty window ClearWindow(); CreateNewClause(); UpdateWindowInfo(); return; } //Move to another clause ClearWindow(); LoadClauseData(clausesIdsLst[idx < clausesIdsLst.Count ? idx : clausesIdsLst.Count - 1]); UpdateWindowInfo(); }
/// <summary> /// Handle Save button click. /// </summary> private async void OnSaveClauseBtn_Click(object sender, RoutedEventArgs e) { saveClauseBtn.IsEnabled = false; var clauseDTO = new ClauseUpdateDTO { Id = clause.Id, Context = contextEdit.Text, Group = ((CheckBoxItem <WordGroup>)groupCBox.SelectedItem).ItemValue, Sound = clause.Sound, Transcription = transcriptionEdit.Text, Word = wordEdit.Text }; int check = await dbFacade.GetClauseIdByWordAsync(clauseDTO.Word); if (check != 0 && check != clauseDTO.Id) { MessageBox.Show(this, String.Format(PrgResources.WordAlreadyPresents, clauseDTO.Word), PrgResources.InformationCaption, MessageBoxButton.OK, MessageBoxImage.Information); return; //Can't save it } if (clause.Id == 0) //Remove "temporary" clause's cache for a new clause { SoundManager.RemoveFromCache(clause.Id, clause.Sound, dbFacade.DataSource); } clause.Id = await dbFacade.AddOrUpdateClauseAsync(clauseDTO, !watchedClauses.Contains(clause.Id)); //To prevent multiple updates of the clause's watch data //Handle asterisk await dbFacade.SetAsteriskAsync(clause.Id, ((CheckBoxItem <AsteriskType>)asteriskCBox.SelectedItem).ItemValue); //Handle relations await dbFacade.RemoveRelationsAsync(clause.Relations.Select(o => o.Id) .Except(relations.Select(o => o.Id)) .ToArray()); foreach (RelationDTO rel in relations.Where(o => o.Id == 0 || o.DescriptionWasChanged)) { rel.Id = await dbFacade.AddOrUpdateRelationAsync(rel.Id, clause.Id, rel.ToWordId, rel.Description); if (rel.MakeInterconnected) //Add relation to the other side { await dbFacade.AddOrUpdateRelationAsync(0, rel.ToWordId, clause.Id, rel.Description); } } //Handle translations await dbFacade.RemoveTranslationsAsync(clause.Translations.Select(o => o.Id) .Except(translations.Select(o => o.Id)) .ToArray()); for (int i = 0; i < translations.Count; i++) { translations[i].Index = i; //Correcting items indices according to collection translations[i].Id = await dbFacade.AddOrUpdateTranslationAsync(translations[i], clause.Id); } dataWasUpdated = true; //Data was changed if (clauseDTO.Id == 0) { //Adding the new clause to the scroll list clausesIdsLst.Add(clause.Id); UpdateDeleteButtonState(); } ClausesWereUpdated?.Invoke(); }