From b4311a613e4616121da027b8d4d3b7a5121e0731 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Thu, 16 Oct 2025 20:38:42 -0500 Subject: [PATCH] fix songs adding not working correctly --- src/core/songs.rs | 13 +++--- src/ui/library.rs | 78 +++++++++++++++++++++-------------- src/ui/presentation_editor.rs | 6 +-- 3 files changed, 57 insertions(+), 40 deletions(-) diff --git a/src/core/songs.rs b/src/core/songs.rs index 31ec39c..6294c2f 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -421,13 +421,13 @@ pub async fn remove_from_db( } pub async fn add_song_to_db( - song: Song, db: PoolConnection, -) -> Result<()> { +) -> Result { let mut db = db.detach(); + let mut song = Song::default(); let verse_order = { - if let Some(vo) = song.verse_order { + if let Some(vo) = song.verse_order.clone() { vo.into_iter() .map(|mut s| { s.push(' '); @@ -441,13 +441,15 @@ pub async fn add_song_to_db( let audio = song .audio + .clone() .map(|a| a.to_str().unwrap_or_default().to_string()); let background = song .background + .clone() .map(|b| b.path.to_str().unwrap_or_default().to_string()); - query!( + let res = query!( r#"INSERT INTO songs (title, lyrics, author, ccli, verse_order, audio, font, font_size, background) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"#, song.title, song.lyrics, @@ -462,7 +464,8 @@ pub async fn add_song_to_db( .execute(&mut db) .await .into_diagnostic()?; - Ok(()) + song.id = res.last_insert_rowid() as i32; + Ok(song) } pub async fn update_song_in_db( diff --git a/src/ui/library.rs b/src/ui/library.rs index fcdceb0..f80b596 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -100,6 +100,7 @@ pub enum Message { OpenContext(i32), None, AddFiles(Vec), + AddSong(Song), AddImages(Option>), AddVideos(Option>), AddPresentations(Option>), @@ -141,19 +142,37 @@ impl<'a> Library { Message::DeleteItem => { return self.delete_items(); } + Message::AddSong(song) => { + if let Err(e) = self.song_library.add_item(song) { + error!(?e, "couldn't add song to model"); + } else { + let index = + (self.song_library.items.len() - 1) as i32; + return Action::Task(Task::done( + Message::OpenItem(Some(( + LibraryKind::Song, + index, + ))), + )); + } + } Message::AddItem => { let kind = self.library_open.unwrap_or(LibraryKind::Song); - let item = match kind { + match kind { LibraryKind::Song => { let song = Song::default(); - let index = self.song_library.items.len(); - self.song_library - .add_item(song) - .map(|_| { - (LibraryKind::Song, index as i32) + let task = Task::future(self.db.acquire()).and_then(move |db| { + Task::perform(add_song_to_db(db), move |res| { + match res { + Ok(song) => { + Message::AddSong(song) + }, + Err(e) => {error!(?e, "couldn't add song to db"); Message::None} + } }) - .ok() + }); + return Action::Task(task); } LibraryKind::Video => { return Action::Task(Task::perform( @@ -174,7 +193,6 @@ impl<'a> Library { )); } }; - return self.update(Message::OpenItem(item)); } Message::AddVideos(videos) => { debug!(?videos); @@ -653,30 +671,26 @@ impl<'a> Library { .add_item(song.clone()) .err() else { - let task = Task::future( - self.db.acquire(), - ) - .and_then(move |db| { - Task::perform( - add_song_to_db( - song.clone(), - db, - ), - { - let song = song.clone(); - move |res| { - debug!( - ?song, - "added to db" - ); - if let Err(e) = res { - error!(?e); - } - Message::None - } - }, - ) - }); + let task = + Task::future(self.db.acquire()) + .and_then(move |db| { + Task::perform( + add_song_to_db(db), + { + move |res| { + if let Err( + e, + ) = res + { + error!( + ?e + ); + } + Message::None + } + }, + ) + }); tasks.push(task); continue; }; diff --git a/src/ui/presentation_editor.rs b/src/ui/presentation_editor.rs index 0f426ea..c9e0eb7 100644 --- a/src/ui/presentation_editor.rs +++ b/src/ui/presentation_editor.rs @@ -51,7 +51,7 @@ impl PresentationEditor { Self { presentation: None, document: None, - title: "Death was Arrested".to_string(), + title: "".to_string(), editing: false, current_slide: None, current_slide_index: None, @@ -183,7 +183,7 @@ impl PresentationEditor { } pub fn view(&self) -> Element { - let container = if let Some(slide) = &self.current_slide { + let presentation = if let Some(slide) = &self.current_slide { container( widget::image(slide) .content_fit(ContentFit::ScaleDown), @@ -199,7 +199,7 @@ impl PresentationEditor { ]; let column = column![ self.toolbar(), - container.center(Length::Fill), + presentation.center(Length::Fill), control_buttons ] .spacing(theme::active().cosmic().space_l());