using CommunityToolkit.Mvvm.Input; using NotesNet8.Models; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace NotesNet8.ViewModels { internal class NotesViewModel : IQueryAttributable { public ObservableCollection AllNotes { get; } public ICommand NewCommand { get; } public ICommand SelectNoteCommand { get; } public NotesViewModel() { AllNotes = new ObservableCollection(Note.LoadAll().Select(n => new NoteViewModel(n))); NewCommand = new AsyncRelayCommand(NewNoteAsync); SelectNoteCommand = new AsyncRelayCommand(SelectNoteAsync); } public void ApplyQueryAttributes(IDictionary query) { if (query.ContainsKey("deleted")) { string noteId = query["deleted"].ToString(); NoteViewModel matchedNote = AllNotes.Where(n => n.Identifier == noteId).FirstOrDefault(); if (matchedNote != null) AllNotes.Remove(matchedNote); } else if (query.ContainsKey("saved")) { string noteId = query["saved"].ToString(); NoteViewModel matchedNote = AllNotes.Where(n => n.Identifier == noteId).FirstOrDefault(); if (matchedNote != null) { matchedNote.Reload(); AllNotes.Move(AllNotes.IndexOf(matchedNote), 0); } else AllNotes.Insert(0, new NoteViewModel(Note.Load(noteId))); } } private async Task NewNoteAsync() { await Shell.Current.GoToAsync(nameof(Views.NotePage)); } private async Task SelectNoteAsync(ViewModels.NoteViewModel note) { if (note != null) await Shell.Current.GoToAsync($"{nameof(Views.NotePage)}?load={note.Identifier}"); } } }