pub enum BroadcastMessage {
NotebookStructureUpdate(Vec<Node>),
NoteTitleUpdate(NoteTitleUpdate),
+ InsertNote(NoteUpdate)
}
#[derive(Clone, PartialEq, Eq)]
pub title: String,
}
+#[derive(Clone)]
+pub struct NoteUpdate {
+ pub id: NoteId,
+ pub model: note::Model
+}
+
impl Backend {
pub fn new(profile: Option<i32>, host: &str, user: &str) -> Result<Self, DbErr> {
let _ = self.tx.send(cmd);
}
- pub fn save_note<F>(&self, note: note::ActiveModel, content: Option<notecontent::ActiveModel>, callback: F)
- where F: FnOnce(Result<(i32, i32), DbErr>) + Send + 'static {
- let backend = self.backend.clone();
+ pub fn save_note<F>(&self, id: NoteId, note: note::ActiveModel, content: Option<notecontent::ActiveModel>, callback: F)
+ where F: FnOnce(Result<(note::Model, i32), DbErr>) + Send + 'static {
+ let bhandle = self.clone();
let cmd = Box::pin(async move {
let result = if note.note_id.is_set() {
- note.update(&backend.db).await
+ note.update(&bhandle.backend.db).await
} else {
- note.insert(&backend.db).await
+ note.insert(&bhandle.backend.db).await
};
match result {
Ok(note) => {
let note_id = note.note_id;
+ let result: Result<(note::Model, i32), DbErr>;
if let Some(mut content) = content {
let result2 = if content.id.is_set() {
- content.update(&backend.db).await
+ content.update(&bhandle.backend.db).await
} else {
content.note_id = Set(note_id);
- content.insert(&backend.db).await
+ content.insert(&bhandle.backend.db).await
};
match result2 {
Ok(ctn) => {
- callback(Ok((note_id, ctn.id)));
+ result = Ok((note.clone(), ctn.id));
}
Err(e) => {
- callback(Err(e));
+ result = Err(e);
}
}
} else {
- callback(Ok((note_id, 0)));
+ result = Ok((note.clone(), 0));
+ }
+
+ if result.is_ok() {
+ // broadcast new note
+ let update = NoteUpdate {
+ id: id.clone(),
+ model: note
+ };
+ _ = bhandle.send_broadcast(BroadcastMessage::InsertNote(update));
}
+ callback(result);
},
Err(e) => {
callback(Err(e));
};
let proxy = doc.doc_proxy();
- self.backend.save_note(note, Some(notecontent), |result|{
+ self.backend.save_note(self.id.clone(), note, Some(notecontent), |result|{
proxy.call_mainthread(move |_doc, note|{
match result {
- Ok((note_id, content_id)) => {
- note.id = NoteId::Id(note_id);
+ Ok((notemodel, content_id)) => {
+ note.id = NoteId::Id(notemodel.note_id);
note.content_id = content_id;
- println!("Note saved: note_id: {}, content_id: {}", note_id, content_id);
+ println!("Note saved: note_id: {}, content_id: {}", notemodel.note_id, content_id);
},
Err(error) => {
println!("Failed to save note: {}", error);
use ui_rs::ui::*;
use entity::note::{Model as Note};
-use crate::backend::{BackendHandle, BroadcastMessage, NoteId, NoteTitleUpdate};
+use crate::backend::{BackendHandle, BroadcastMessage, NoteId, NoteTitleUpdate, NoteUpdate};
use crate::note::new_note_id;
use crate::window::NavigationItem;
}
}
- pub fn find_note(&self, id: NoteId) -> Option<usize> {
+ pub fn find_note(&self, id: &NoteId) -> Option<usize> {
for (index, note) in self.notes.iter().enumerate() {
- if note.id == id || note.orig_id == id {
+ if note.id == *id || note.orig_id == *id {
return Some(index)
}
}
(index, note)
},
NoteSelectFrom::NavigationItem(nav) => {
- let index = self.find_note(nav.note_id?)?;
+ let index = self.find_note(&nav.note_id?)?;
self.detach_current_note();
self.notes.select_with_event(index as i32, false);
let note = self.notes.data_mut().get_mut(index)?;
}
pub fn update_note_title(&mut self, update: &NoteTitleUpdate) {
+ // TODO: use find_note
// Find the note in the list (or add it if it doesn't exist yet)
// Start with the currently selected note
let mut update_row: Option<usize> = None;
}
}
+ pub fn insert_note(&mut self, update: NoteUpdate) {
+ if let Some(index) = self.find_note(&update.id) {
+ // update existing item
+ if let Some(elm) = self.notes.data_mut().get_mut(index) {
+ elm.data = update.model;
+ elm.id = NoteId::Id(elm.data.note_id); // id no longer temporary
+ self.notes.update_row(index);
+ }
+ } else {
+ let item = NoteItem::from_model(update.model);
+ // rember selection
+ let sel = self.notes.selected_index();
+ // insert new item
+ self.notes.data_mut().push(item);
+ self.notes.update();
+ if sel >= 0 {
+ self.notes.select_with_event(sel, false);
+ }
+ }
+ }
+
#[action]
pub fn message(&mut self, _event: &ActionEvent) {
while let Ok(msg) = self.broadcast_rx.try_recv() {
self.update_note_title(&update);
}
},
+ BroadcastMessage::InsertNote(update) => {
+ if update.model.collection_id == self.collection_id {
+ self.insert_note(update);
+ }
+ },
_ => {
},