Notes/Notes.Data/ViewModels/AllNotesViewModel.cs

77 lines
1.8 KiB
C#
Raw Normal View History

2023-12-04 15:49:31 +08:00
using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Notes.Models;
namespace Notes.ViewModels;
public partial class AllNotesViewModel: ViewModelBase
{
private readonly IServices _services;
[ObservableProperty]
private ObservableCollection<Note> _notes;
2023-12-06 11:03:26 +08:00
2023-12-04 15:49:31 +08:00
[ObservableProperty]
2023-12-06 11:03:26 +08:00
private ObservableCollection<Note> _selected;
2023-12-04 15:49:31 +08:00
private Models.Notes _originNotes;
[RelayCommand]
public void NewNote()
{
2023-12-06 11:03:26 +08:00
this._services.GetSafeAs<MainViewModel>().Navitation<NoteViewModel>();
2023-12-04 15:49:31 +08:00
}
2023-12-06 11:03:26 +08:00
2023-12-04 15:49:31 +08:00
[RelayCommand]
2023-12-06 11:03:26 +08:00
public void DeleteNote()
2023-12-04 15:49:31 +08:00
{
2023-12-06 11:03:26 +08:00
}
public void DeleteNote(IEnumerable<NoteViewModel> selection)
{
foreach (var noteViewModel in selection)
2023-12-04 15:49:31 +08:00
{
2023-12-06 11:03:26 +08:00
var note = noteViewModel.Note;
Notes.Remove(note);
2023-12-04 15:49:31 +08:00
_originNotes.Delete(note);
}
}
[RelayCommand]
public void OpenAbout()
{
2023-12-06 11:03:26 +08:00
this._services.GetSafeAs<MainViewModel>().Navitation<AboutViewModel>();
2023-12-04 15:49:31 +08:00
}
2023-12-06 11:03:26 +08:00
[RelayCommand]
public void OpenNote(Note note)
2023-12-04 15:49:31 +08:00
{
2023-12-06 11:03:26 +08:00
this._services.GetSafeAs<MainViewModel>().OpenNote(note);
2023-12-04 15:49:31 +08:00
}
2023-12-06 11:03:26 +08:00
2023-12-04 15:49:31 +08:00
public AllNotesViewModel(IServices services)
{
this._services = services;
this._originNotes = new Models.Notes();
_notes = new ObservableCollection<Note>(this._originNotes.notes);
}
public void AddNote(NoteViewModel viewModel)
{
var viewModelNote = viewModel.Note;
2023-12-06 11:03:26 +08:00
if (Notes.Contains(viewModelNote))
2023-12-04 15:49:31 +08:00
{
return;
}
if (this.Notes.Any(x=>x.Filename == viewModel.Note.Filename))
{
return;
}
Notes.Add(viewModelNote);
this._originNotes.notes.Add(viewModelNote);
}
}