]> uap-core.de Git - note.git/commitdiff
update notes list when a note title is changed
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 29 May 2026 16:27:45 +0000 (18:27 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 29 May 2026 16:27:45 +0000 (18:27 +0200)
application/src/backend.rs
application/src/note.rs
application/src/notebook.rs
application/src/window.rs
ui-rs/src/ui/toolkit.rs

index 223ef1d7978ca082dde220ef52048aa58aac8890..809228b93f769d9126985a3c7e733dc215ea4f4a 100644 (file)
@@ -5,6 +5,7 @@ use tokio::runtime::Runtime;
 use std::sync::{Arc};
 use std::thread::JoinHandle;
 use tokio::sync::{broadcast, mpsc};
+use tokio::sync::broadcast::error::SendError;
 use migration::{Migrator, MigratorTrait};
 use ui_rs::ui;
 
@@ -44,7 +45,42 @@ impl Clone for BackendHandle {
 
 #[derive(Clone)]
 pub enum BroadcastMessage {
-    NotebookStructureUpdate(Vec<Node>)
+    NotebookStructureUpdate(Vec<Node>),
+    NotesUpdate(NotesUpdate),
+}
+
+#[derive(Clone)]
+pub enum NoteId {
+    Id(i32),
+    TmpId(String),
+}
+
+impl Default for NoteId {
+    fn default() -> Self {
+        NoteId::Id(0)
+    }
+}
+
+#[derive(Clone, Default)]
+pub struct NoteUpdate {
+    pub note_id: NoteId,
+    pub title: Option<String>,
+    pub content: bool,
+}
+
+#[derive(Clone, Default)]
+pub struct NotesUpdate {
+    pub collection_id: i32,
+    pub note_updates: Vec<NoteUpdate>,
+}
+
+impl NotesUpdate {
+    pub fn new(collection_id: i32, note_updates: Vec<NoteUpdate>) -> Self {
+        NotesUpdate {
+            collection_id: collection_id,
+            note_updates: note_updates,
+        }
+    }
 }
 
 
@@ -232,6 +268,14 @@ impl Backend {
 }
 
 impl BackendHandle {
+    pub fn send_broadcast(&self, msg: BroadcastMessage)  -> Result<usize, SendError<BroadcastMessage>> {
+        let result = self.btx.send(msg);
+        if let Some(notify) = &self.backend.broadcast_notify.as_ref() {
+            notify();
+        }
+        result
+    }
+    
     /// Reloads the notebook structure/collections. On success, it sends a NotebookStructureUpdate
     /// to the broadcast channel.
     pub fn reload_collections(&self) {
index 2307764f931a9f9ef0d03524a0795670803a72fc..c46c776a389a2c122b903f1894806bf0b789362c 100644 (file)
@@ -5,7 +5,7 @@ use sea_orm::{NotSet, Set};
 use entity::note::NoteType;
 use ui_rs::{action, ui_actions, UiModel};
 use ui_rs::ui::*;
-use crate::backend::BackendHandle;
+use crate::backend::{BackendHandle, BroadcastMessage, NoteId, NoteUpdate, NotesUpdate};
 use crate::window::NoteTypeTabView;
 
 #[derive(UiModel)]
@@ -61,17 +61,25 @@ impl Note {
     pub fn init_content(&mut self, content: &entity::notecontent::Model) {
         self.content_id = content.id;
         self.text.set(content.content.as_str());
-        self.update_title(content.content.as_str());
+        self.update_title(content.content.as_str(), false);
     }
 
-    pub fn update_title(&mut self, s: &str) {
+    pub fn update_title(&mut self, s: &str, notify: bool) {
         match generate_title(s) {
             Some(result) => {
                 let title = result.0;
                 self.title_start = result.1 as i32;
                 self.title_end = result.1 as i32 + title.len() as i32;
 
-                // TODO: notify notebook that the title has changed
+                if notify {
+                    let update = NoteUpdate {
+                        note_id: NoteId::Id(self.note_id),
+                        title: Some(title.to_string()),
+                        ..Default::default()
+                    };
+                    let msg = NotesUpdate::new(self.collection_id, vec![update]);
+                    _ = self.backend.as_ref().send_broadcast(BroadcastMessage::NotesUpdate(msg));
+                }
             },
             None => {
                 self.title_start = -1;
@@ -135,7 +143,7 @@ impl Note {
         }
 
         // TODO: we don't need the full text
-        self.update_title(self.text.get().as_str());
+        self.update_title(self.text.get().as_str(), true);
     }
 }
 
index a9be563016d1d2ffb0712c193b25cd349e3e790c..ad839003bd800b16f52c3c501b37055296223105 100644 (file)
@@ -3,7 +3,7 @@ use ui_rs::{action, ui_actions, UiModel};
 use ui_rs::ui::*;
 
 use entity::note::{Model as Note};
-use crate::backend::{BackendHandle, BroadcastMessage};
+use crate::backend::{BackendHandle, BroadcastMessage, NoteId};
 
 #[derive(UiModel)]
 pub struct Notebook {
@@ -113,8 +113,29 @@ impl Notebook {
     pub fn message(&mut self, _event: &ActionEvent) {
         while let Ok(msg) = self.broadcast_rx.try_recv() {
             match msg {
+                BroadcastMessage::NotesUpdate(update) => {
+                    let mut update_rows: Vec<usize> = Vec::new();
+                    for u in &update.note_updates {
+                        if let NoteId::Id(id) = u.note_id {
+                            let note_data = self.notes.data_mut();
+                            for (i, note) in note_data.iter_mut().enumerate() {
+                                if note.note_id == id {
+                                    // update note
+                                    if let Some(title) = &u.title {
+                                        note.title = title.clone();
+                                    }
+                                }
+                                update_rows.push(i);
+                            }
+                        }
+                    }
+
+                    for i in update_rows {
+                        self.notes.update_row(i);
+                    }
+                },
                 _ => {
-                    
+
                 },
             }
         }
index 450dc99f27b1d255954db9ff19ad7bceb290f23f..1927fbfd5ea0a520ca383af16c5f84247f530bd4 100644 (file)
@@ -90,6 +90,7 @@ impl MainWindow {
         while let Ok(msg) = self.broadcast_rx.try_recv() {
             match msg {
                 BroadcastMessage::NotebookStructureUpdate(nodes) => self.update_notebook_structure(&nodes),
+                _ => {}
             }
         }
     }
index 708c22e258e693cfcc5168482f38540062860c40..bb9d111dfedd104886cf9afc0db4cb40f99aa697 100644 (file)
@@ -774,15 +774,15 @@ impl<T> UiList<T> {
         }
     }
 
-    pub fn update(&mut self) {
+    pub fn update(&self) {
         unsafe {
             ui_list_update(self.ptr);
         }
     }
 
-    pub fn update_row(&mut self, row: i32) {
+    pub fn update_row(&self, row: usize) {
         unsafe {
-            ui_list_update_row(self.ptr, row);
+            ui_list_update_row(self.ptr, row as i32);
         }
     }