private void ShowLesson()
        {
            ShowModifyLessonSubView = true;
            using (var db = Database.Instance.GetConnection())
            {
                var lessonsInDatabase = db.Table <Lesson>().Where(l => l.UserId == CurrentUser.Id && l.Name == CurrentLesson.Name);
                if (lessonsInDatabase.Count() != 1)
                {
                    CurrentLessonWords.Clear();
                    Lessons.Remove(CurrentLesson);
                    CurrentLesson = null;
                    return;
                }
                CurrentLesson = lessonsInDatabase.First();


                CurrentLessonWords = new ObservableCollection <Word>();
                CurrentLessonWords.CollectionChanged += CurrentLessonWords_CollectionChanged;

                foreach (var word in db.Table <Word>().Where(w => w.LessonId == CurrentLesson.Id))
                {
                    CurrentLessonWords.Add(word);
                }
                CurrentEditingWord = new Word();
                OnPropertyChanged("CurrentEditingWord");
                OnPropertyChanged("CurrentLessonWords");
                OnPropertyChanged("CurrentLesson");
            }
        }
 private bool CanSaveLesson()
 {
     return(CurrentEditingWord != null && CurrentLesson != null &&
            !CurrentEditingWord.HasErrors && CurrentLessonWords.All <Word>((w) => !w.HasErrors) &&
            !String.IsNullOrWhiteSpace(CurrentLesson.FirstLangName) &&
            !String.IsNullOrWhiteSpace(CurrentLesson.SecondLangName) &&
            !CurrentLesson.HasErrors && CurrentLessonWords.Count() > 0);
 }
        public CreateNewLessonsViewModel(User currentUser)
        {
            CurrentUser = currentUser;
            RevertToPreviousViewModelCmd = new RelayCommand((p) => ViewModelManager.Instance.ReturnToPreviousModel());
            AddNewLessonCmd       = new RelayCommand((p) => StartAddingNewLesson(), (p) => CanStartAddingNewLesson());
            ShowLessonCmd         = new RelayCommand((p) => ShowLesson(), (p) => CurrentLesson != null);
            SaveLessonCmd         = new RelayCommand((p) => SaveLesson(), (p) => CanSaveLesson());
            DeleteSelectedWordCmd = new RelayCommand((p) => CurrentLessonWords.Remove(CurrentEditingWord),
                                                     (p) => CurrentEditingWord != null && CurrentLessonWords.Count() > 1);
            AddNewWordToLessonCmd   = new RelayCommand((p) => AddNewWordToLesson(), (p) => CanAddNewWordToLesson());
            DeleteSelectedLessonCmd = new RelayCommand((p) => DeleteSelectedLesson(p), (p) => CanDeleteSelectedLesson(p));
            using (var db = Database.Instance.GetConnection())
            {
                Lessons = new ObservableCollection <Lesson>();
                foreach (var lesson in db.Table <Lesson>().Where(lesson => lesson.UserId == currentUser.Id))
                {
                    Lessons.Add(lesson);
                }
            }
            _langNameValidationService = new ValidateLanguageDefinitionService();

            _newLessonValidationService = (p) =>
            {
                var ret = new List <string>();
                int count;
                using (var db = Database.Instance.GetConnection())
                {
                    count = db.Table <Lesson>().Where(l => l.UserId == currentUser.Id && l.Name.Equals(p)).Count();
                }
                if (count >= 1 || (CurrentLesson != null && CurrentLesson.Name.Equals(p as string)))
                {
                    ret.Add("Lesson name has to be unique");
                }
                else if ((p as string).Length < Constants.MinLessonNameLength ||
                         (p as string).Length > Constants.MaxLessonNameLength)
                {
                    ret.Add(String.Format("Length has to be between {0} and {1}", Constants.MinLessonNameLength, Constants.MaxLessonNameLength));
                }

                return(ret);
            };

            ShowModifyLessonSubView = false;
            CurrentLessonWords      = new ObservableCollection <Word>();
            CurrentEditingWord      = new Word()
            {
            };

            CurrentLessonWords.CollectionChanged += CurrentLessonWords_CollectionChanged;
            CurrentLessonWordsChanged             = false;
        }
        private void SaveLesson()
        {
            if (CurrentLessonWords.Count() == 0 || CurrentLessonWordsChanged == false)
            {
                return;
            }

            using (var db = Database.Instance.GetConnection())
            {
                RemoveLessonIfExists(db, CurrentLesson);
                db.Insert(CurrentLesson);

                CurrentLesson = db.Table <Lesson>().Where(l => l.Name == CurrentLesson.Name).First();


                foreach (var word in CurrentLessonWords)
                {
                    try
                    {
                        db.Delete <Word>(word.Id);
                    }
                    catch (SQLiteException e)
                    {
                        Console.WriteLine(e.Message + " Couldn't delete for some reason");
                        return;
                    }
                    db.Insert(new Word()
                    {
                        FirstLangDefinition  = word.FirstLangDefinition,
                        SecondLangDefinition = word.SecondLangDefinition,
                        Level    = word.Level,
                        LessonId = CurrentLesson.Id
                    });
                }
            }
        }
 private void AddNewWordToLesson()
 {
     CurrentLessonWords.Add(Word.CopyFrom(CurrentEditingWord));
 }