Comments etc.

This commit is contained in:
2024-10-13 12:28:41 +02:00
parent 15f1961ab2
commit c305bc63b7
6 changed files with 116 additions and 55 deletions

View File

@@ -6,46 +6,49 @@ using System.Windows.Input;
namespace Jugenddienst_Stunden.ViewModels;
internal class NotesViewModel : IQueryAttributable {
public ObservableCollection<ViewModels.NoteViewModel> AllNotes { get; }
public ObservableCollection<NoteViewModel> AllNotes { get; }
public ICommand NewCommand { get; }
public ICommand SelectNoteCommand { get; }
public NotesViewModel() {
AllNotes = new ObservableCollection<ViewModels.NoteViewModel>(Models.Note.LoadAll().Select(n => new NoteViewModel(n)));
AllNotes = new ObservableCollection<NoteViewModel>(Models.Note.LoadAll().Select(n => new NoteViewModel(n)));
NewCommand = new AsyncRelayCommand(NewNoteAsync);
SelectNoteCommand = new AsyncRelayCommand<ViewModels.NoteViewModel>(SelectNoteAsync);
SelectNoteCommand = new AsyncRelayCommand<NoteViewModel?>(SelectNoteAsync);
}
private async Task NewNoteAsync() {
await Shell.Current.GoToAsync(nameof(Views.NotePage));
}
private async Task SelectNoteAsync(ViewModels.NoteViewModel note) {
private async Task SelectNoteAsync(NoteViewModel? note) {
if (note != null)
await Shell.Current.GoToAsync($"{nameof(Views.NotePage)}?load={note.Identifier}");
}
void IQueryAttributable.ApplyQueryAttributes(IDictionary<string, object> query) {
if (query.ContainsKey("deleted")) {
string noteId = query["deleted"].ToString();
NoteViewModel matchedNote = AllNotes.Where((n) => n.Identifier == noteId).FirstOrDefault();
string? noteId = query["deleted"]?.ToString();
if (noteId != null) {
NoteViewModel? matchedNote = AllNotes.Where((n) => n.Identifier == noteId).FirstOrDefault();
// If note exists, delete it
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 note is found, update it
if (matchedNote != null) {
matchedNote.Reload();
AllNotes.Move(AllNotes.IndexOf(matchedNote), 0);
// If note exists, delete it
if (matchedNote != null)
AllNotes.Remove(matchedNote);
}
} else if (query.ContainsKey("saved")) {
string? noteId = query["saved"]?.ToString();
if (noteId != null) {
NoteViewModel? matchedNote = AllNotes.FirstOrDefault((n) => n.Identifier == noteId);
// If note isn't found, it's new; add it.
else
AllNotes.Insert(0, new NoteViewModel(Note.Load(noteId)));
// If note is found, update it
if (matchedNote != null) {
matchedNote.Reload();
AllNotes.Move(AllNotes.IndexOf(matchedNote), 0);
}
// If note isn't found, it's new; add it.
else { AllNotes.Insert(0, new NoteViewModel(Note.Load(noteId))); }
}
}
}
}