Hello
I am trying to improve my note app, for my final project for school.
I am trying to communicate VMs but I am using the messaging center, but it is kind of slow
This is my Notebook VM
public NotebooksVM() {
NotebooksCollection = new ObservableCollection<Notebook>();
SelectedNoteBookCommand = new Command(async () => {
await Application.Current.MainPage.Navigation.PushAsync(new NotesPage());
MessagingCenter.Send(this, "data", SelectedNotebook);
SelectedNotebook = null;
});
ChangeLayoutCommand = new Command(OnChangeLayout);
}
private async Task CreateNote() {
var result = await Application.Current.MainPage.DisplayPromptAsync(string.Empty,
"Notebook name?");
if (!string.IsNullOrEmpty(result)) {
var notebook = new Notebook() {
Name = result,
UserId = App.UserId,
CreatedAt = DateTime.Now.ToString("ddd, MMMM yyyy")
};
await Database.InsertAsync(notebook);
GetNotebooksAsync();
await Application.Current.MainPage.Navigation.PushAsync(new NotesPage());
MessagingCenter.Send(this, "data", result);
} else {
return;
}
}
As you can see in both cases I am sending the Notebook (Selected or New One) to the new Notes VM, So I can get all the notes from that notebook
Notes VM
public Notebook RecivedNotebook { get; set; }
public LayoutBase LayoutBase { get; set; }
public Note SelectedNote { get; set; }
public ICommand SelectedNoteCommand { get; set; }
public FontImageSource Glyph { get; set; }
public NotesVM() {
Glyph = new FontImageSource() {
Glyph = IconFonts.FormatListBulleted,
FontFamily = "material",
Size = 44
};
LayoutBase = new LinearLayout();
NotesCollection = new ObservableCollection<Note>();
CreateNewNoteCommand = new Command(async () => {
var note = await CreateNote(RecivedNotebook);
await Application.Current.MainPage.Navigation.PushAsync(new EditorPage());
MessagingCenter.Send(this, "note", note);
await GetNotes(RecivedNotebook);
});
SelectedNoteCommand = new Command(async () => {
await Application.Current.MainPage.Navigation.PushAsync(new EditorPage());
MessagingCenter.Send(this, "note", SelectedNote);
SelectedNote = null;
});
MessagingCenter.Subscribe<NotebooksVM, Notebook>(this, "data",
async (sender, data) => {
RecivedNotebook = data;
await GetNotes(RecivedNotebook);
});
ChangeLayoutCommand = new Command(OnChangeLayout);
}
private async Task GetNotes(Notebook notebook) {
var notes = await Database.ReadAsync<Note>();
if (notes != null) {
var newNotes = notes.Where(n => n.NotebookId == notebook.Id);
NotesCollection.Clear();
foreach (var item in newNotes) {
NotesCollection.Add(item);
}
}
}
private async Task<Note> CreateNote(Notebook recivedNotebook) {
var result = await Application.Current.MainPage.DisplayPromptAsync(string.Empty,
"Note name?");
if (!string.IsNullOrEmpty(result)) {
var note = new Note() {
NotebookId = recivedNotebook.Id,
Title = result,
CreatedAt = DateTime.Now.ToString("ddd, MMMM yyyy")
};
await Database.InsertAsync(note);
return note;
} else {
return null;
}
As you can see, y receive the note, using the messeging center, and send it again to another VM
public Note RecivedNote { get; set; }
public EditorVM() {
SaveContent = new Command(() => { UpdateNote(); });
MessagingCenter.Subscribe<NotesVM, Note>(this, "note", (obj, item) => {
RecivedNote = item;
});
}
So I was trying to figure out, how to do this without the messaging center and ZERO code-behind
This is my project if someone wants to see it
'
https://github.com/eduardoagr/safe.Xamarin/tree/main/Safe
By the way, any features or suggestions are welcome