Ejemplo n.º 1
0
        /// <summary>
        /// Command callback.
        /// Called to directly add the calling vocab to the SRS.
        /// </summary>
        /// <param name="vocab">Calling vocab.</param>
        private void OnQuickAddToSrs(ExtendedVocab vocab)
        {
            SrsEntry entry = new SrsEntry();

            entry.LoadFromVocab(vocab.DbVocab);
            entry.Tags = Kanji.Interface.Properties.Settings.Default.LastSrsTagsValue;

            SrsLevel startLevel = SrsLevelStore.Instance.GetLevelByValue(0);

            if (startLevel != null)
            {
                entry.NextAnswerDate = DateTime.Now + startLevel.Delay;
            }

            // Sets some properties
            entry.CreationDate = DateTime.UtcNow;

            try
            {
                // Add the entity to the database.
                new SrsEntryDao().Add(entry);
                vocab.SrsEntry = new ExtendedSrsEntry(entry);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(
                    NavigationActor.Instance.ActiveWindow,
                    string.Format("An error occured: {0}", ex.Message),
                    "Quick add error",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);

                LogHelper.GetLogger("Quick add").Error("An error occured during quick add.", ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Command callback.
        /// Called to open the SRS item edition window to add the
        /// calling vocab to the SRS.
        /// </summary>
        /// <param name="vocab">Calling vocab.</param>
        private void OnAddToSrs(ExtendedVocab vocab)
        {
            // Prepare the new entry.
            SrsEntry entry = new SrsEntry();

            entry.LoadFromVocab(vocab.DbVocab);

            // Show the modal entry edition window.
            EditSrsEntryWindow wnd = new EditSrsEntryWindow(entry);

            wnd.ShowDialog();

            // When it is closed, get the result.
            ExtendedSrsEntry result = wnd.Result;

            if (wnd.IsSaved && result != null &&
                ((!string.IsNullOrEmpty(vocab.DbVocab.KanjiWriting) &&
                  result.AssociatedVocab == vocab.DbVocab.KanjiWriting) ||
                 (string.IsNullOrEmpty(vocab.DbVocab.KanjiWriting) &&
                  result.AssociatedVocab == vocab.DbVocab.KanaWriting)))
            {
                // The result exists and is still associated with this kanji.
                // We can use it in this ViewModel.
                vocab.SrsEntry = result;
            }
        }
        /// <summary>
        /// Command callback.
        /// Called to directly add the calling vocab to the SRS.
        /// </summary>
        /// <param name="vocab">Calling vocab.</param>
        private async void OnQuickAddToSrs(ExtendedVocab vocab)
        {
            SrsEntry entry = new SrsEntry();

            entry.LoadFromVocab(vocab.DbVocab);
            entry.Tags = Kanji.Interface.Properties.Settings.Default.LastSrsTagsValue;

            SrsLevel startLevel = SrsLevelStore.Instance.GetLevelByValue(0);

            if (startLevel != null)
            {
                entry.NextAnswerDate = DateTime.Now + startLevel.Delay;
            }

            // Sets some properties
            entry.CreationDate = DateTime.UtcNow;

            try
            {
                // Add the entity to the database.
                new SrsEntryDao().Add(entry);
                vocab.SrsEntry = new ExtendedSrsEntry(entry);
            }
            catch (Exception ex)
            {
                await MessageBoxManager.GetMessageBoxStandardWindow(new MessageBoxStandardParams
                {
                    ContentTitle      = "Quick add error",
                    ContentMessage    = $"An error occurred: {ex.Message}",
                    Icon              = Icon.Error,
                    ButtonDefinitions = ButtonEnum.Ok,
                }).ShowDialog(NavigationActor.Instance.MainWindow);

                LogHelper.GetLogger("Quick add").Error("An error occured during quick add.", ex);
            }
        }
        /// <summary>
        /// Reads an SRS item from a field row using the parameters of the view model.
        /// </summary>
        /// <param name="row">Row to read.</param>
        /// <param name="log">Log under the form of a stringbuilder to inform the user about how everything goes.</param>
        /// <returns>The SRS item read if successful. A null value otherwise.</returns>
        private SrsEntry ReadEntry(List <string> row, VocabDao vocabDao, KanjiDao kanjiDao, StringBuilder log)
        {
            try
            {
                SrsEntry entry        = new SrsEntry();
                string   kanjiReading = row[_kanjiReadingColumn];
                if (string.IsNullOrEmpty(kanjiReading))
                {
                    log.AppendFormat("Empty kanji reading. Skipping.");
                    return(null);
                }

                // Figure out item type.
                CsvItemType itemType = ReadItemType(row);
                if (itemType == CsvItemType.Kanji ||
                    (itemType == CsvItemType.Unspecified &&
                     (_noTypeBehavior == CsvImportNoTypeBehavior.AllKanji ||
                      (_noTypeBehavior == CsvImportNoTypeBehavior.Auto && kanjiReading.Length == 1))))
                {
                    // Three solutions to set as kanji:
                    // 1. Item is manually specified as kanji in source data.
                    // 2. Item is not specified and no type behavior is set to AllKanji.
                    // 3. Item is not specified and no type behavior is set to Auto and the length of kanjiReading is exactly 1.
                    entry.AssociatedKanji = kanjiReading;
                    log.AppendFormat("Kanji: \"{0}\". ", kanjiReading);
                    itemType = CsvItemType.Kanji;
                }
                else
                {
                    // All other cases will lead to vocab.
                    entry.AssociatedVocab = kanjiReading;
                    log.AppendFormat("Vocab: \"{0}\". ", kanjiReading);
                    itemType = CsvItemType.Vocab;
                }

                string readings = ReadAcceptedReadings(row);

                long?sequenceNumber = ReadSequenceNumber(row);

                if (ReadingAutofill || MeaningAutofill)
                {
                    switch (itemType)
                    {
                    case CsvItemType.Kanji:
                        var kanji = kanjiDao.GetFirstMatchingKanji(kanjiReading);
                        if (kanji == null)
                        {
                            log.Append("Can't find kanji in database. Skipping.");
                            return(null);
                        }
                        entry.LoadFromKanji(kanji);
                        break;

                    case CsvItemType.Vocab:
                        var vocabs = string.IsNullOrEmpty(readings) ? vocabDao.GetMatchingVocab(kanjiReading)
                                        : vocabDao.GetVocabByReadings(kanjiReading, readings);
                        if (sequenceNumber.HasValue)
                        {
                            vocabs = vocabs.Where(v => v.Seq == sequenceNumber);
                        }
                        var vocab = vocabs.FirstOrDefault();
                        if (vocab == null)
                        {
                            log.Append("Can't find vocab in database. Skipping.");
                            return(null);
                        }
                        entry.LoadFromVocab(vocab);
                        entry.AssociatedVocab = kanjiReading;
                        entry.Readings        = default(string);
                        break;
                    }
                }

                // Find readings.
                if (!ReadingAutofill || !string.IsNullOrEmpty(readings))
                {
                    entry.Readings = readings;
                }
                if (itemType == CsvItemType.Kanji && string.IsNullOrEmpty(entry.Readings))
                {
                    log.Append("Empty readings. Skipping.");
                    return(null);
                }

                // Find meanings.
                string meanings = ReadAcceptedMeanings(row);
                if (!MeaningAutofill || !string.IsNullOrEmpty(meanings))
                {
                    entry.Meanings = meanings;
                }
                if (string.IsNullOrEmpty(entry.Meanings))
                {
                    log.Append("Empty meanings. Skipping.");
                    return(null);
                }

                // Find all optional info.
                entry.MeaningNote    = ReadMeaningNotes(row);
                entry.ReadingNote    = ReadReadingNotes(row);
                entry.Tags           = ReadTags(row);
                entry.CurrentGrade   = ReadStartLevel(row);
                entry.NextAnswerDate = ReadNextReviewDate(row);

                log.Append("OK.");
                return(entry);
            }
            catch (Exception ex)
            {
                log.AppendFormat("Unknown error: \"{0}\". Skipping.", ex.Message);
            }

            return(null);
        }