#[derive(UiModel)]
pub struct Note {
+ pub doc: UiDocRef<Note>,
pub backend: Rc<BackendHandle>,
pub id: NoteId,
pub content_id: i32,
pub kind: NoteType,
- pub created: DateTimeWithTimeZone,
extract_title: bool,
title_start: i32,
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,
}
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(),
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);
+ }
}
- }
+ });
});
}
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;
}
}
+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());
/* ----------------------------------- 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);
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);