two fixes

This commit is contained in:
iTob 2024-09-05 18:06:53 +02:00
parent 60ec37b2c7
commit e7a54b99ea
3 changed files with 12 additions and 4 deletions

View File

@ -7,14 +7,14 @@ namespace MauiApp2.ViewModels
{ {
internal class NotesViewModel : IQueryAttributable internal class NotesViewModel : IQueryAttributable
{ {
public ObservableCollection<ViewModels.NoteViewModel> AllNotes { get; } public ObservableCollection<NoteViewModel> AllNotes { get; }
public ICommand NewCommand { get; } public ICommand NewCommand { get; }
public ICommand SelectNoteCommand { get; } public ICommand SelectNoteCommand { get; }
public NotesViewModel() public NotesViewModel()
{ {
AllNotes = new ObservableCollection<ViewModels.NoteViewModel>(Models.Note.LoadAll().Select(n => new NoteViewModel(n))); AllNotes = new ObservableCollection<NoteViewModel>(Note.LoadAll().Select(n => new NoteViewModel(n)));
NewCommand = new AsyncRelayCommand(NewNoteAsync); NewCommand = new AsyncRelayCommand(NewNoteAsync);
SelectNoteCommand = new AsyncRelayCommand<ViewModels.NoteViewModel>(SelectNoteAsync); SelectNoteCommand = new AsyncRelayCommand<NoteViewModel>(SelectNoteAsync);
} }
private async Task NewNoteAsync() private async Task NewNoteAsync()
@ -45,11 +45,14 @@ namespace MauiApp2.ViewModels
// If note is found, update it // If note is found, update it
if (matchedNote != null) if (matchedNote != null)
{
matchedNote.Reload(); matchedNote.Reload();
AllNotes.Move(AllNotes.IndexOf(matchedNote), 0);
}
// If note isn't found, it's new; add it. // If note isn't found, it's new; add it.
else else
AllNotes.Add(new NoteViewModel(Note.Load(noteId))); AllNotes.Insert(0, new NoteViewModel(Models.Note.Load(noteId)));
} }
} }
} }

View File

@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp2.Views.AllNotesPage" x:Class="MauiApp2.Views.AllNotesPage"
xmlns:viewModels="clr-namespace:MauiApp2.ViewModels" xmlns:viewModels="clr-namespace:MauiApp2.ViewModels"
NavigatedTo="ContentPage_NavigatedTo"
Title="Your Notes"> Title="Your Notes">
<ContentPage.BindingContext> <ContentPage.BindingContext>
<viewModels:NotesViewModel /> <viewModels:NotesViewModel />

View File

@ -6,4 +6,8 @@ public partial class AllNotesPage : ContentPage
{ {
InitializeComponent(); InitializeComponent();
} }
private void ContentPage_NavigatedTo(object sender, NavigatedToEventArgs e)
{
notesCollection.SelectedItem = null;
}
} }