]> uap-core.de Git - note.git/commitdiff
broadcast new notes to all windows main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 9 Jun 2026 20:30:26 +0000 (22:30 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 9 Jun 2026 20:30:26 +0000 (22:30 +0200)
application/src/backend.rs
application/src/note.rs
application/src/notebook.rs

index 2bf57107f8567621db3a44ab7269a0bebbdfe1ae..84ddcfc36ad759cc8aceec1d6ada92166ee66b42 100644 (file)
@@ -47,6 +47,7 @@ impl Clone for BackendHandle {
 pub enum BroadcastMessage {
     NotebookStructureUpdate(Vec<Node>),
     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<i32>, host: &str, user: &str) -> Result<Self, DbErr> {
@@ -334,38 +341,49 @@ impl BackendHandle {
         let _ = self.tx.send(cmd);
     }
     
-    pub fn save_note<F>(&self, note: note::ActiveModel, content: Option<notecontent::ActiveModel>, callback: F)
-    where F: FnOnce(Result<(i32, i32), DbErr>) + Send + 'static {
-        let backend = self.backend.clone();
+    pub fn save_note<F>(&self, id: NoteId, note: note::ActiveModel, content: Option<notecontent::ActiveModel>, 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));
index 65cb9f573076df91f17b205e9118c79c11d02bc3..98b72c36b6804c1e0cd6188564ef62ec00930d7a 100644 (file)
@@ -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);
index 9d808a1d6ef5806499da501d3df0573021981ae6..e7b0bf54bbaa0d94df9d69e07b6dd3d3f0edea05 100644 (file)
@@ -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<usize> {
+    pub fn find_note(&self, id: &NoteId) -> Option<usize> {
         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<usize> = 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);
+                    }
+                },
                 _ => {
 
                 },