/// <summary>
        /// Builds a ViewModel aimed at editing an existing SrsEntry,
        /// or adding a pre-composed SrsEntry.
        /// </summary>
        /// <param name="entity">Entity to edit.</param>
        public SrsEntryViewModel(SrsEntry entity)
        {
            // Initialize fields.
            _entry = new ExtendedSrsEntry(entity);
            _originalNextReviewDate = entity.NextAnswerDate;
            _originalLevelValue     = entity.CurrentGrade;
            _associatedKanjiString  = Entry.AssociatedKanji;
            _associatedVocabString  = Entry.AssociatedVocab;
            _srsEntryDao            = new SrsEntryDao();
            _kanjiDao = new KanjiDao();
            _vocabDao = new VocabDao();
            if (IsNew)
            {
                Entry.Tags = Properties.Settings.Default.LastSrsTagsValue;
            }

            // Create the relay commands.
            SubmitCommand               = new RelayCommand(OnSubmit);
            CancelCommand               = new RelayCommand(OnCancel);
            SrsProgressResetCommand     = new RelayCommand(OnSrsProgressReset);
            ApplyAssociatedKanjiCommand = new RelayCommand(OnApplyAssociatedKanji);
            ApplyAssociatedVocabCommand = new RelayCommand(OnApplyAssociatedVocab);
            ToggleSuspendCommand        = new RelayCommand(OnToggleSuspend);
            DeleteCommand               = new RelayCommand(OnDelete);
            ToggleDateEditCommand       = new RelayCommand(OnToggleDateEdit);
            DateToNowCommand            = new RelayCommand(OnDateToNow);
            DateToNeverCommand          = new RelayCommand(OnDateToNever);

            // Get the associated kanji or vocab.
            GetAssociatedKanji();
            GetAssociatedVocab();

            // Initialize the VM.
            _isFirstSrsLevelSelect             = true;
            SrsLevelPickerVm                   = new SrsLevelPickerViewModel();
            SrsLevelPickerVm.SrsLevelSelected += OnSrsLevelSelected;
            SrsLevelPickerVm.Initialize(_entry.CurrentGrade);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Called when the AddToSrsCommand is fired.
        /// Opens the SRS entry window.
        /// </summary>
        private void OnAddToSrs()
        {
            // Prepare the new entry.
            SrsEntry entry = new SrsEntry();

            entry.LoadFromKanji(_kanjiEntity.DbKanji);

            // 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 &&
                result.AssociatedKanji == _kanjiEntity.DbKanji.Character)
            {
                // The result exists and is still associated with this kanji.
                // We can use it in this ViewModel.
                SrsEntry = result;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Builds a kanji details ViewModel handling the given kanji.
        /// </summary>
        /// <param name="kanjiEntity">Kanji to handle.</param>
        public KanjiDetailsViewModel(ExtendedKanji kanjiEntity)
        {
            KanjiEntity = kanjiEntity;

            if (KanjiEntity.DbKanji.SrsEntries.Any())
            {
                SrsEntry = new ExtendedSrsEntry(
                    KanjiEntity.DbKanji.SrsEntries.First());
            }

            VocabFilter filter = new VocabFilter()
            {
                Kanji = new KanjiEntity[] { _kanjiEntity.DbKanji }
            };

            CategoryFilterVm = new CategoryFilterViewModel();
            CategoryFilterVm.PropertyChanged += OnCategoryChanged;

            VocabListVm = new VocabListViewModel(filter);
            VocabListVm.KanjiNavigated += OnKanjiNavigated;
            VocabFilterVm = new VocabFilterViewModel(filter);
            VocabFilterVm.FilterChanged   += OnVocabFilterChanged;
            VocabFilterVm.PropertyChanged += OnVocabPropertyChanged;

            ToggleDetailsCommand = new RelayCommand(OnToggleDetails);
            AddToSrsCommand      = new RelayCommand(OnAddToSrs);
            EditSrsEntryCommand  = new RelayCommand(OnEditSrsEntry);
            FilterReadingCommand = new RelayCommand <string>(OnFilterReading);

            NextStrokeCommand     = new RelayCommand(OnNextStroke);
            PreviousStrokeCommand = new RelayCommand(OnPreviousStroke);
            LastStrokeCommand     = new RelayCommand(OnLastStroke);
            FirstStrokeCommand    = new RelayCommand(OnFirstStroke);
            WaniKaniCommand       = new RelayCommand(OnWaniKani);

            PrepareSvg();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Command callback.
        /// Calls for the SRS item edition window to edit the SRS item
        /// referred by the current question.
        /// </summary>
        private void OnEditSrsEntry()
        {
            if (CurrentQuestion != null)
            {
                // Show the modal entry edition window.
                EditSrsEntryWindow wnd = new EditSrsEntryWindow(CurrentQuestionGroup.Reference.Clone());
                wnd.ShowDialog();

                // When it is closed, get the result.
                ExtendedSrsEntry result = wnd.Result;
                if (wnd.IsSaved)
                {
                    if (result != null &&
                        result.NextAnswerDate.HasValue &&
                        result.NextAnswerDate.Value.ToLocalTime() <= DateTime.Now &&
                        !result.SuspensionDate.HasValue)
                    {
                        // The result exists and is still due for review.
                        // Update the current group.
                        CurrentQuestionGroup.Reference = result.Reference;
                        RaisePropertyChanged("CurrentQuestion");
                    }
                    else
                    {
                        // The result has been deleted or is no longer due
                        // for review. Remove the question group from the batch.
                        ReviewState = SrsReviewStateEnum.Ignore;
                        _currentBatch.Remove(CurrentQuestionGroup);
                        FillCurrentBatch();

                        AnsweredReviewsCount++;
                        ToNextQuestion();
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Background task work method.
        /// Sends the entity to the database.
        /// </summary>
        private void DoSendEntity(object sender, DoWorkEventArgs e)
        {
            SrsEntryOperationEnum operationType = (SrsEntryOperationEnum)e.Argument;

            // Acquire the send object. Thus, only one send operation can occur
            // at any given time.
            lock (_sendLock)
            {
                // Once the lock is acquired, check that the work is not done.
                // You wouldn't want to submit the item again if it worked
                // the first time.
                if (!_isDone)
                {
                    // The work has still not been done.
                    // Set the IsSending value and start the job.
                    IsSending = true;

                    if (operationType == SrsEntryOperationEnum.Update)
                    {
                        // Update
                        try
                        {
                            _isDone = _srsEntryDao.Update(_entry.Reference);
                            if (!_isDone)
                            {
                                ErrorMessage = R.SrsItem_EditFailure;
                            }
                        }
                        catch (Exception ex)
                        {
                            // An exception occured.
                            // Log the exception and set the error message.
                            LogHelper.GetLogger(this.GetType().Name)
                            .Error("Could not update the entity.", ex);
                            ErrorMessage = R.SrsItem_EditFailure;
                        }
                    }
                    else if (operationType == SrsEntryOperationEnum.Add)
                    {
                        // Add
                        try
                        {
                            // Sets some properties
                            Entry.Reference.CreationDate = DateTime.UtcNow;

                            // Add the entity to the database.
                            _srsEntryDao.Add(_entry.Reference);
                            _isDone = true;

                            // Saves the value of the tags
                            Properties.Settings.Default.LastSrsTagsValue = Entry.Tags;
                        }
                        catch (Exception ex)
                        {
                            // An exception occured.
                            // Log the exception and set the error message.
                            LogHelper.GetLogger(this.GetType().Name)
                            .Error("Could not add the entity.", ex);
                            ErrorMessage = R.SrsItem_EditFailure;
                        }
                    }
                    else
                    {
                        // Delete
                        try
                        {
                            _isDone = _srsEntryDao.Delete(_entry.Reference);
                            if (!_isDone)
                            {
                                ErrorMessage = R.SrsItem_EditFailure;
                            }
                        }
                        catch (Exception ex)
                        {
                            // An exception occured.
                            // Log the exception and set the error message.
                            LogHelper.GetLogger(this.GetType().Name)
                            .Error("Could not delete the entity.", ex);
                            ErrorMessage = R.SrsItem_EditFailure;
                        }
                    }

                    // After the job.
                    if (_isDone && FinishedEditing != null)
                    {
                        // If the job has been done, raise the event.
                        ExtendedSrsEntry result =
                            (operationType == SrsEntryOperationEnum.Delete ?
                             null : Entry);

                        FinishedEditing(this, new SrsEntryEditedEventArgs(result, true));
                    }

                    IsSending = false;
                }
            }
        }