From: Olaf Wintermann Date: Sun, 31 May 2026 12:19:20 +0000 (+0200) Subject: store note_id, content_id after a note is saved X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=3aeb94fec7a577f406b51c0d03216ba2905e2a73;p=note.git store note_id, content_id after a note is saved --- diff --git a/application/src/note.rs b/application/src/note.rs index 87e4430..d7111ff 100644 --- a/application/src/note.rs +++ b/application/src/note.rs @@ -13,6 +13,7 @@ static TMP_ID: AtomicU64 = AtomicU64::new(1); #[derive(UiModel)] pub struct Note { + pub doc: UiDocRef, pub backend: Rc, 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) -> 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); + } } - } + }); }); } diff --git a/application/src/notebook.rs b/application/src/notebook.rs index d8e0320..786bc5e 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -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 { diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index e939b5a..b36bf72 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -175,6 +175,8 @@ impl UiDoc { 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 UiDoc { } } +extern "C" fn doc_app_unref(_: *mut c_void) { + app_unref(); +} + impl UiObject { pub fn from_ptr(ptr: *mut ffi::UiObject) -> UiObject { 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) 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);