From: Olaf Wintermann Date: Mon, 25 May 2026 16:53:35 +0000 (+0200) Subject: prepare note save X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=note.git prepare note save --- diff --git a/application/src/backend.rs b/application/src/backend.rs index 1c537af..8469c36 100644 --- a/application/src/backend.rs +++ b/application/src/backend.rs @@ -295,4 +295,14 @@ impl BackendHandle { }); let _ = self.tx.send(cmd); } + + pub fn save_note(&self, note: entity::note::ActiveModel, callback: F) + where F: FnOnce(Result) + Send + 'static { + let backend = self.backend.clone(); + let cmd = Box::pin(async move { + let result = note.save(&backend.db).await; + callback(result); + }); + let _ = self.tx.send(cmd); + } } diff --git a/application/src/note.rs b/application/src/note.rs index afac9db..14b7c64 100644 --- a/application/src/note.rs +++ b/application/src/note.rs @@ -1,12 +1,19 @@ +use sea_orm::prelude::DateTimeWithTimeZone; +use sea_orm::sea_query::prelude::Utc; +use sea_orm::{NotSet, Set}; use entity::note::NoteType; use ui_rs::{ui_actions, UiModel}; use ui_rs::ui::*; +use crate::backend::BackendHandle; use crate::window::NoteTypeTabView; #[derive(UiModel, Default)] pub struct Note { pub note_id: i32, + pub kind: NoteType, + pub created: DateTimeWithTimeZone, + #[bind("note_type")] pub note_type: UiInteger, @@ -25,4 +32,42 @@ impl Note { }; self.note_type.set(tab as i64); } + + pub fn save(&self, collection_id: i32, backend: &BackendHandle) { + let content = self.text.get(); + let title = match generate_title(content.as_str()) { + Some(title) => title.to_string(), + None => "Note".to_string() + }; + + let note = entity::note::ActiveModel { + note_id: if self.note_id == 0 { NotSet } else { Set(self.note_id) }, + collection_id: Set(collection_id), + kind: Set(self.kind.clone()), + title: Set(title), + content: Set(content), + lastmodified: Set(Utc::now().into()), + created: if self.note_id == 0 { Set(Utc::now().into()) } else { NotSet } + }; + + backend.save_note(note, |result|{ + match result { + Ok(model) => { + println!("Note saved"); + }, + Err(error) => { + println!("Failed to save note: {}", error); + } + } + }); + } +} + +fn generate_title(s: &str) -> Option<&str> { + for line in s.lines() { + if !line.trim().is_empty() { + return Some(line); + } + } + None } \ No newline at end of file diff --git a/application/src/notebook.rs b/application/src/notebook.rs index 5ceeacf..2239b5a 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -2,10 +2,12 @@ use ui_rs::{action, ui_actions, UiModel}; use ui_rs::ui::*; use entity::note::{Model as Note}; +use crate::backend::BackendHandle; -#[derive(UiModel, Default)] +#[derive(UiModel)] pub struct Notebook { pub doc_ref: UiDocRef, + pub backend: BackendHandle, pub collection_id: i32, pub selected_note: Option>, @@ -16,9 +18,13 @@ pub struct Notebook { #[ui_actions] impl Notebook { - pub fn new(id: i32) -> Self { + pub fn new(id: i32, backend: &BackendHandle) -> Self { Notebook { - collection_id: id, ..Default::default() + doc_ref: Default::default(), + backend: backend.clone(), + collection_id: id, + selected_note: None, + notes: Default::default() } } @@ -37,8 +43,20 @@ impl Notebook { if let Some(note) = s.selected_element(self.notes.data()) { // detach the current note if let Some(current) = &self.selected_note { - self.doc_ref.get_doc().unwrap().ctx.detach(current); - self.selected_note = None; + // doc_ref.get_doc() should never fail here + if let Some(mut doc) = self.doc_ref.get_doc() { + // save the note + let note_proxy = current.doc_proxy(); + let backend = self.backend.clone(); + let collection_id = self.collection_id; + note_proxy.call_mainthread(move |doc, note|{ + note.save(collection_id, &backend); + }); + + // detach the note + doc.ctx.detach(current); + self.selected_note = None; + } } // Create the new note diff --git a/application/src/window.rs b/application/src/window.rs index 8534825..ac5e96b 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -132,7 +132,7 @@ pub fn create_window(app: &App, ctx: &AppContext) -> UiObject