/// <summary> /// Executes the command. /// </summary> /// <param name="parameter"></param> public void Execute(object parameter) { // Create a new note Models.NoteModel newNote = new Models.NoteModel(); // Add the new note to the notes lists noteViewModel.Notes.Add(newNote); noteViewModel.NotesForLV.Add(newNote); // Make the new note the selected note // Then update the UI to go to the new note noteViewModel.SelectedNote = newNote; noteViewModel.FirePropertyChanged("SelectedNote"); }
/// <summary> /// Executes the command. /// </summary> /// <param name="parameter"></param> public async void Execute(object parameter) { string saveNoteTitle = noteViewModel.SelectedNote.Title; // Check if the note has just been created if (noteViewModel.SelectedNote.Title == "Untitled Note") { // Create a save note dialog Views.SaveNoteDialog save = new Views.SaveNoteDialog(); ContentDialogResult result; // This loop makes sure that the user doesn't enter // a duplicate, invalid, or empty title for a note while (true) { result = await save.ShowAsync(); // Save the title given by the user saveNoteTitle = save.NoteTitle; // Check for empty error bool empty = IsFileNameEmpty(saveNoteTitle); // Check for invalid error bool invalid = IsFileNameInvalid(saveNoteTitle); // Check for duplicate error bool duplicate = IsFileNameDuplicate(saveNoteTitle); // Look for an error string content; if (empty) { content = "The title cannot be empty. Please enter a title."; } else if (duplicate) { content = "That title already exists. Please enter a new unique title."; } else if (invalid) { content = "The title contains invalid character(s). Please enter a new title."; } else { break; // Skip, if there are none } // Don't show an error if the user clicks Cancel if (result == ContentDialogResult.Secondary) { break; } // Show a dialog that there is an error ContentDialog error = new ContentDialog() { Title = "Error Occurred", Content = content, PrimaryButtonText = "OK", }; await error.ShowAsync(); } if (result == ContentDialogResult.Primary) { // Get the new note title from the dialog noteViewModel.SelectedNote.Title = saveNoteTitle; // Check if the selected note is a buffer // Add it to the notes lists first if (noteViewModel.SelectedNote == noteViewModel.Buffer) { noteViewModel.Notes.Add(noteViewModel.SelectedNote); noteViewModel.NotesForLV.Add(noteViewModel.SelectedNote); // Set the buffer back to null noteViewModel.Buffer = null; } // Update the notes list order base on note titles // THIS IS BUGGY (OPTIONAL FIX) //noteViewModel.UpdateNotesLists(); // Notify that the selected note and the selected note's title changed noteViewModel.FirePropertyChanged("SelectedNote"); noteViewModel.SelectedNote.FirePropertyChanged("Title"); } // Do not continue if the user clicks 'Cancel' else { return; } // Save the note data to file //Repositories.NotesRepo.SaveNoteToFile(noteViewModel.SelectedNote); // Save the note to the database Repositories.DatabaseRepo.AddNote(noteViewModel.SelectedNote); } // Note to be saved already exists else { // Double checking if (await Repositories.DatabaseRepo.NoteExist(noteViewModel.SelectedNote)) { Repositories.DatabaseRepo.UpdateNote(noteViewModel.SelectedNote); } } // Show a dialog that the save was successful ContentDialog dialog = new ContentDialog() { Title = "Saved successfully", Content = "'" + saveNoteTitle + "'" + " has been saved.", PrimaryButtonText = "OK", }; await dialog.ShowAsync(); // After saving defaults... AfterSavingDefaults(); }