/// <summary> /// Initializes a new instance of the CategoryEditorViewModel class. /// </summary> public CategoryEditorViewModel(ICategoryRepository categoryRepository) { CategoryRepository = categoryRepository; Categories = new ObservableCollection<Category>(_categoryRepository.FindAll()); CategoryIds = new List<Guid>(); NotesToDelete = new List<Guid>(); NotesToTrash = new List<Guid>(); NewCategoryCommand = new RelayCommand(NewCategory, () => !OnCategoryUpdate); DeleteCategoryCommand = new RelayCommand<object>(DeleteCategory, (noused) => OnCategoryUpdate && Categories.Count > 1); DeleteNotesCommand = new RelayCommand<bool?>((mark) => DeleteNotesToo = mark.Value, (noused) => OnCategoryUpdate); AcceptCategoryCommand = new RelayCommand(AcceptCategory, () => OnCategoryUpdate && IsValid); CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true); SelectBgColorCommand = new RelayCommand(SelectBgColor, () => OnCategoryUpdate); SelectFontColorCommand = new RelayCommand(SelectFontColor, () => OnCategoryUpdate); DefaultCategoryChangedCommand = new RelayCommand<Category>(DefaultCategoryChanged); // We register a default message containing a string (See SavingCatOptions) below. Messenger.Default.Register<string>(this, SavingCatOptions); // Default category _defaultCategory = Categories[0]; // The default one is always the first if (!_defaultCategory.IsDefault) // first time? _defaultCategory.IsDefault = true; }
public void ChangingCategoryNameChangeNoteCategory() { // Arrange: We create the repo's mocks var noteMock = new Mock<INoteRepository>(); var categoryMock = new Mock<ICategoryRepository>(); var category = new Category("Dummy", "#123123123", "#123123123"); categoryMock.Setup(rep => rep.FindAll()).Returns(new List<Category> { category }); categoryMock.Setup(rep => rep.GetById(category.Id)).Returns(category); noteMock.Setup(rep => rep.FindAll()).Returns(new List<Note> { new Note("Note", category) }); // Act: we create a viewModel's instance, a list with the new category changes and we send a mesage var viewModel = new MainViewModel(noteMock.Object, categoryMock.Object); category.Name = "Party"; var categoryId = new List<Guid> { category.Id }; var categoryEditorChanges = new CategoryEditorChangesMessage { CategoriesIds = categoryId }; Messenger.Default.Send(categoryEditorChanges); // Assert: and the category and the note have the new name viewModel.Categories.First(c => c.Id == category.Id).Name.ShouldEqual("Party"); viewModel.Notes.First(n => n.Content == "Note").Category.Name.ShouldEqual("Party"); }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel(INoteRepository noteRepository, ICategoryRepository categoryRepository) { _noteRepository = noteRepository; _categoryRepository = categoryRepository; Notes = new ObservableCollection<Note>(_noteRepository.FindAll()); Categories = new ObservableCollection<Category>(_categoryRepository.FindAll()); // Is there categories list empty? if (Categories.Count == 0) { // In this case, I will create a default category with a welcome note var cat = new Category(Resources.Strings.GeneralCat, "#33CC00", "#FFFFFF"); Categories.Add(cat); _categoryRepository.SaveAll(Categories); var note = new Note(Resources.Strings.WelcomeMessage, cat); Notes.Add(note); _noteRepository.Save(note); } ActualNote = new Note(); SelectedCategory = _categories[0]; // We need to this for Category's ComboBox sake. Trash = new Category("Trash", "#f8f8f8", "#777777"); AddNoteCommand = new RelayCommand(AddNote, CanAddNote); EditNoteCommand = new RelayCommand<Note>(EditNote); DeleteNoteCommand = new RelayCommand<Note>(DeleteNote); DeleteAllNotesCommand = new RelayCommand(DeleteAllNotes); CategoryOptionsCommand = new RelayCommand(OpenCategoryOptions); // We expect a message with some lists with changes. Messenger.Default.Register<CategoryEditorChangesMessage>(this, MakingNewCatChanges); }
/// <summary> /// Initializes a new instance of the <see cref="Note"/> class. /// </summary> /// <param name="content">The note content.</param> /// <param name="category">The note category.</param> public Note(string content, Category category) { _content = content; _category = category; // We need to take the actual date var dt = DateTime.Now; IFormatProvider ci = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name); _date = dt.ToString("d", ci); }
public void CanDeleteACategory() { var mockCategoryRepository = new Mock<ICategoryRepository>(); mockCategoryRepository.Setup(rep => rep.FindAll()).Returns(new List<Category> { new Category("Dummy", "#123123123", "#123123123") }); var viewModel = new CategoryEditorViewModel(mockCategoryRepository.Object); var cat = new Category("Prueba", "#ffaaffaa", "#aaffaaff"); viewModel.Categories.Add(cat); viewModel.DeleteCategoryCommand.Execute(cat); Assert.AreEqual(viewModel.Categories.Count, 1); Assert.IsFalse(viewModel.Categories.Contains(cat)); mockCategoryRepository.Verify(x => x.SaveAll(viewModel.Categories)); }
public void DeletingACategoryWithoutNotes() { // Arrange: We create the repo's mocks var noteMock = new Mock<INoteRepository>(); var categoryMock = new Mock<ICategoryRepository>(); var category = new Category("Dummy", "#123123123", "#123123123"); categoryMock.Setup(rep => rep.FindAll()).Returns(new List<Category> { category }); noteMock.Setup(rep => rep.FindAll()).Returns(new List<Note> {new Note("Note", category)}); // Act: we create a viewModel's instance, a list with the category to delete and we send a message var viewModel = new MainViewModel(noteMock.Object, categoryMock.Object); var notesToTrash = new List<Guid> {category.Id}; var categoryEditorChanges = new CategoryEditorChangesMessage { NotesToTrash = notesToTrash }; Messenger.Default.Send(categoryEditorChanges); // Assert: and the note's category should be trash viewModel.Notes.FirstOrDefault(n => n.Content == "Note").Category.Name.ShouldEqual("Trash"); }
public void DeletingACategoryWithItsNotes() { // Arrange: We create the repo's mocks var noteMock = new Mock<INoteRepository>(); var categoryMock = new Mock<ICategoryRepository>(); var category = new Category("Dummy", "#123123123", "#123123123"); categoryMock.Setup(rep => rep.FindAll()).Returns(new List<Category> { category }); noteMock.Setup(rep => rep.FindAll()).Returns(new List<Note> { new Note("Note", category) }); // Act: we create a viewModel's instance, a list with the category to delete and we send a message var viewModel = new MainViewModel(noteMock.Object, categoryMock.Object); var notesToDelete = new List<Guid> { category.Id }; var categoryEditorChanges = new CategoryEditorChangesMessage { NotesToDelete = notesToDelete }; Messenger.Default.Send(categoryEditorChanges); // Assert: and the note is gone viewModel.Notes.Count.ShouldEqual(0); }
/// <summary> /// Creates a new Category with a default name /// </summary> private void NewCategory() { var cat = new Category(Resources.Strings.Name, "#FFFFFF", "#000000"); if (!_categories.Contains(cat)) Categories.Add(cat); }
/// <summary> /// Deletes the selected category. /// It adds the category's note to a list (depending on DeletesNotesToo value) /// </summary> /// <param name="category">The category.</param> private void DeleteCategory(object category) { var cat = category as Category; if (cat != null) { if (DeleteNotesToo) NotesToDelete.Add(cat.Id); else NotesToTrash.Add(cat.Id); SelectedCategory = null; if (_defaultCategory.Equals(cat)) { Categories.Remove(cat); Categories[0].IsDefault = true; _defaultCategory = Categories[0]; } else { Categories.Remove(cat); } _categoryRepository.SaveAll(Categories); } }
/// <summary> /// Here we change the category to be the default one. /// </summary> /// <param name="cat">The cat.</param> private void DefaultCategoryChanged(Category cat) { _defaultCategory.IsDefault = false; cat.IsDefault = true; _defaultCategory = cat; _defaultCatChanged = true; }
/// <summary> /// Changes the note category. /// </summary> /// <param name="theNote">The note.</param> /// <param name="newCategory">The new category.</param> public void ChangeNoteCategory(Note theNote, Category newCategory) { // Selecting the right note var note = Notes.Single(n => n == theNote); note.Category = newCategory; _noteRepository.Save(note); }