]> uap-core.de Git - note.git/commitdiff
store note_id, content_id after a note is saved
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 31 May 2026 12:19:20 +0000 (14:19 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 31 May 2026 12:19:20 +0000 (14:19 +0200)
application/src/note.rs
application/src/notebook.rs
ui-rs/src/ui/toolkit.rs

index 87e4430922257b4c680fe35d3354d2746ed0a415..d7111ff8d03fc8e00fb5f434703a1f1c00e13742 100644 (file)
@@ -13,6 +13,7 @@ static TMP_ID: AtomicU64 = AtomicU64::new(1);
 
 #[derive(UiModel)]
 pub struct Note {
+    pub doc: UiDocRef<Note>,
     pub backend: Rc<BackendHandle>,
 
     pub id: NoteId,
@@ -21,7 +22,6 @@ pub struct Note {
     pub content_id: i32,
 
     pub kind: NoteType,
-    pub created: DateTimeWithTimeZone,
 
     extract_title: bool,
     title_start: i32,
@@ -38,13 +38,13 @@ pub struct Note {
 impl Note {
     pub fn new(id: NoteId, collection_id: i32, backend: Rc<BackendHandle>) -> Self {
         Note {
+            doc: Default::default(),
             backend: backend,
             collection_id: collection_id,
 
             id: id,
             content_id: 0,
             kind: NoteType::PlainTextNote,
-            created: Default::default(),
             extract_title: false,
             title_start: -1,
             title_end: -1,
@@ -123,6 +123,10 @@ impl Note {
     }
 
     pub fn save(&self, collection_id: i32, backend: &BackendHandle) {
+        let Some(doc) = self.doc.get_doc() else {
+            return;
+        };
+
         let content = self.text.get();
         let title = match generate_title(content.as_str()) {
             Some(title) => title.0.to_string(),
@@ -149,16 +153,20 @@ impl Note {
             content: Set(content),
         };
 
+        let proxy = doc.doc_proxy();
         backend.save_note(note, Some(notecontent), |result|{
-            match result {
-                Ok((note_id, content_id)) => {
-                    // TODO: how to save the note_id, content_id in self?
-                    println!("Note saved");
-                },
-                Err(error) => {
-                    println!("Failed to save note: {}", error);
+            proxy.call_mainthread(move |_doc, note|{
+                match result {
+                    Ok((note_id, content_id)) => {
+                        note.id = NoteId::Id(note_id);
+                        note.content_id = content_id;
+                        println!("Note saved: note_id: {}, content_id: {}", note_id, content_id);
+                    },
+                    Err(error) => {
+                        println!("Failed to save note: {}", error);
+                    }
                 }
-            }
+            });
         });
     }
 
index d8e0320ac12518931bcc6e222dac53e54c64ae9f..786bc5ebae69ea380908d778af51f658cd88a3be 100644 (file)
@@ -82,6 +82,7 @@ impl Notebook {
                     let note_data = crate::note::Note::new(note.id.clone(), self.collection_id, self.backend.clone());
                     // Create the note document object
                     UiDoc::new2(note_data, |n,d| {
+                        n.doc = d.doc_ref();
                         if note.is_new() {
                             n.init_new();
                         } else {
index e939b5a0c2c298c69598e6e806cf485f5f296711..b36bf72c99385f018d15e47accba6e06179332ea 100644 (file)
@@ -175,6 +175,8 @@ impl<T: UiModel + UiActions> UiDoc<T> {
         unsafe {
             let doc = ui_document_new(mem::size_of::<*mut T>());
             let ctx = UiContext::from_ptr(ui_document_context(doc));
+            ui_app_ref();
+            ui_reg_destructor(ctx.ptr, std::ptr::null_mut(), doc_app_unref);
             let data_ptr = ctx.reg_box(Box::new(data)); // returns *mut T
             let doc_storage: *mut *mut T = doc.cast();
             *doc_storage = data_ptr;
@@ -211,6 +213,10 @@ impl<T: UiModel + UiActions> UiDoc<T> {
     }
 }
 
+extern "C" fn doc_app_unref(_: *mut c_void) {
+    app_unref();
+}
+
 impl<T> UiObject<T> {
     pub fn from_ptr(ptr: *mut ffi::UiObject) -> UiObject<T> {
         assert!(!ptr.is_null());
@@ -1076,6 +1082,18 @@ pub unsafe fn ui_list_get<'a, T>(list: *const ffi::UiList, index: usize) -> Opti
 
 /* ----------------------------------- Event Loop ---------------------------------- */
 
+pub fn app_ref() {
+    unsafe {
+        ui_app_ref();
+    }
+}
+
+pub fn app_unref() {
+    unsafe {
+        ui_app_unref();
+    }
+}
+
 pub fn call_mainthread<F>(f: F)
 where F: FnOnce() + Send {
     let b = Box::new(f);
@@ -1183,6 +1201,9 @@ type UiListGetFunc   = extern "C" fn(*const ffi::UiList, c_int) -> *mut c_void;
 type UiListCountFunc = extern "C" fn(*const ffi::UiList) -> c_int;
 
 extern "C" {
+    pub fn ui_app_ref();
+    pub fn ui_app_unref();
+
     pub fn ui_object_ref(obj: *mut ffi::UiObject);
     pub fn ui_object_unref(obj: *mut ffi::UiObject);