From: Olaf Wintermann Date: Tue, 9 Jun 2026 20:30:26 +0000 (+0200) Subject: broadcast new notes to all windows X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=note.git broadcast new notes to all windows --- diff --git a/application/src/backend.rs b/application/src/backend.rs index 2bf5710..84ddcfc 100644 --- a/application/src/backend.rs +++ b/application/src/backend.rs @@ -47,6 +47,7 @@ impl Clone for BackendHandle { pub enum BroadcastMessage { NotebookStructureUpdate(Vec), NoteTitleUpdate(NoteTitleUpdate), + InsertNote(NoteUpdate) } #[derive(Clone, PartialEq, Eq)] @@ -68,6 +69,12 @@ pub struct NoteTitleUpdate { pub title: String, } +#[derive(Clone)] +pub struct NoteUpdate { + pub id: NoteId, + pub model: note::Model +} + impl Backend { pub fn new(profile: Option, host: &str, user: &str) -> Result { @@ -334,38 +341,49 @@ impl BackendHandle { let _ = self.tx.send(cmd); } - pub fn save_note(&self, note: note::ActiveModel, content: Option, callback: F) - where F: FnOnce(Result<(i32, i32), DbErr>) + Send + 'static { - let backend = self.backend.clone(); + pub fn save_note(&self, id: NoteId, note: note::ActiveModel, content: Option, callback: F) + where F: FnOnce(Result<(note::Model, i32), DbErr>) + Send + 'static { + let bhandle = self.clone(); let cmd = Box::pin(async move { let result = if note.note_id.is_set() { - note.update(&backend.db).await + note.update(&bhandle.backend.db).await } else { - note.insert(&backend.db).await + note.insert(&bhandle.backend.db).await }; match result { Ok(note) => { let note_id = note.note_id; + let result: Result<(note::Model, i32), DbErr>; if let Some(mut content) = content { let result2 = if content.id.is_set() { - content.update(&backend.db).await + content.update(&bhandle.backend.db).await } else { content.note_id = Set(note_id); - content.insert(&backend.db).await + content.insert(&bhandle.backend.db).await }; match result2 { Ok(ctn) => { - callback(Ok((note_id, ctn.id))); + result = Ok((note.clone(), ctn.id)); } Err(e) => { - callback(Err(e)); + result = Err(e); } } } else { - callback(Ok((note_id, 0))); + result = Ok((note.clone(), 0)); + } + + if result.is_ok() { + // broadcast new note + let update = NoteUpdate { + id: id.clone(), + model: note + }; + _ = bhandle.send_broadcast(BroadcastMessage::InsertNote(update)); } + callback(result); }, Err(e) => { callback(Err(e)); diff --git a/application/src/note.rs b/application/src/note.rs index 65cb9f5..98b72c3 100644 --- a/application/src/note.rs +++ b/application/src/note.rs @@ -164,13 +164,13 @@ impl Note { }; let proxy = doc.doc_proxy(); - self.backend.save_note(note, Some(notecontent), |result|{ + self.backend.save_note(self.id.clone(), note, Some(notecontent), |result|{ proxy.call_mainthread(move |_doc, note|{ match result { - Ok((note_id, content_id)) => { - note.id = NoteId::Id(note_id); + Ok((notemodel, content_id)) => { + note.id = NoteId::Id(notemodel.note_id); note.content_id = content_id; - println!("Note saved: note_id: {}, content_id: {}", note_id, content_id); + println!("Note saved: note_id: {}, content_id: {}", notemodel.note_id, content_id); }, Err(error) => { println!("Failed to save note: {}", error); diff --git a/application/src/notebook.rs b/application/src/notebook.rs index 9d808a1..e7b0bf5 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -4,7 +4,7 @@ use ui_rs::{action, ui_actions, UiModel}; use ui_rs::ui::*; use entity::note::{Model as Note}; -use crate::backend::{BackendHandle, BroadcastMessage, NoteId, NoteTitleUpdate}; +use crate::backend::{BackendHandle, BroadcastMessage, NoteId, NoteTitleUpdate, NoteUpdate}; use crate::note::new_note_id; use crate::window::NavigationItem; @@ -67,9 +67,9 @@ impl Notebook { } } - pub fn find_note(&self, id: NoteId) -> Option { + pub fn find_note(&self, id: &NoteId) -> Option { for (index, note) in self.notes.iter().enumerate() { - if note.id == id || note.orig_id == id { + if note.id == *id || note.orig_id == *id { return Some(index) } } @@ -91,7 +91,7 @@ impl Notebook { (index, note) }, NoteSelectFrom::NavigationItem(nav) => { - let index = self.find_note(nav.note_id?)?; + let index = self.find_note(&nav.note_id?)?; self.detach_current_note(); self.notes.select_with_event(index as i32, false); let note = self.notes.data_mut().get_mut(index)?; @@ -162,6 +162,7 @@ impl Notebook { } pub fn update_note_title(&mut self, update: &NoteTitleUpdate) { + // TODO: use find_note // Find the note in the list (or add it if it doesn't exist yet) // Start with the currently selected note let mut update_row: Option = None; @@ -189,6 +190,27 @@ impl Notebook { } } + pub fn insert_note(&mut self, update: NoteUpdate) { + if let Some(index) = self.find_note(&update.id) { + // update existing item + if let Some(elm) = self.notes.data_mut().get_mut(index) { + elm.data = update.model; + elm.id = NoteId::Id(elm.data.note_id); // id no longer temporary + self.notes.update_row(index); + } + } else { + let item = NoteItem::from_model(update.model); + // rember selection + let sel = self.notes.selected_index(); + // insert new item + self.notes.data_mut().push(item); + self.notes.update(); + if sel >= 0 { + self.notes.select_with_event(sel, false); + } + } + } + #[action] pub fn message(&mut self, _event: &ActionEvent) { while let Ok(msg) = self.broadcast_rx.try_recv() { @@ -198,6 +220,11 @@ impl Notebook { self.update_note_title(&update); } }, + BroadcastMessage::InsertNote(update) => { + if update.model.collection_id == self.collection_id { + self.insert_note(update); + } + }, _ => { },