diff --git a/src/core/kinds.rs b/src/core/kinds.rs index af1e383..59de7ba 100644 --- a/src/core/kinds.rs +++ b/src/core/kinds.rs @@ -40,6 +40,7 @@ impl TryFrom for ServiceItemKind { "mp4" | "mkv" | "webm" => { Ok(Self::Video(Video::from(path))) } + "pdf" => Ok(Self::Presentation(Presentation::from(path))), _ => Err(miette::miette!("Unknown item")), } } diff --git a/src/core/presentations.rs b/src/core/presentations.rs index f3d2277..4a74f79 100644 --- a/src/core/presentations.rs +++ b/src/core/presentations.rs @@ -456,7 +456,10 @@ pub async fn update_presentation_in_db( let Some(mut max) = ids.iter().map(|r| r.id).max() else { return Err(miette::miette!("cannot find max id")); }; - debug!(?e, "Presentation not found"); + debug!( + ?e, + "Presentation not found adding a new presentation" + ); max += 1; let result = query!( r#"INSERT into presentations VALUES($1, $2, $3, $4, $5, $6)"#, @@ -473,7 +476,7 @@ pub async fn update_presentation_in_db( return match result { Ok(_) => { - debug!("should have been updated"); + debug!("presentation should have been added"); Ok(()) } Err(e) => { diff --git a/src/main.rs b/src/main.rs index cbf87f6..ed2b6f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -961,7 +961,7 @@ impl cosmic::Application for App { } presentation_editor::Action::SplitAddPresentation((first, second)) => { if self.library.is_some() { - let second_task = self.update(Message::Library(library::Message::AddPresentations(Some(vec![second])))); + let second_task = self.update(Message::Library(library::Message::AddPresentationSplit(Some(second)))); self.update(Message::Library(library::Message::UpdatePresentation(first))).chain(second_task) } else { diff --git a/src/ui/library.rs b/src/ui/library.rs index debfb8d..65d5ddc 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -106,6 +106,7 @@ pub enum Message { AddImages(Option>), AddVideos(Option>), AddPresentations(Option>), + AddPresentationSplit(Option), } impl<'a> Library { @@ -254,8 +255,38 @@ impl<'a> Library { Task::batch(tasks).chain(after_task), ); } + Message::AddPresentationSplit(presentation) => { + debug!(?presentation, "adding to db"); + if let Some(presentation) = presentation { + if let Err(e) = self + .presentation_library + .add_item(presentation.clone()) + { + error!(?e); + } + return Action::Task( + Task::future(self.db.acquire()).and_then( + move |db| { + Task::perform( + add_presentation_to_db( + presentation.clone(), + db, + ), + move |res| { + debug!("added to db"); + if let Err(e) = res { + error!(?e); + } + Message::None + }, + ) + }, + ), + ); + } + } Message::AddPresentations(presentations) => { - debug!(?presentations); + debug!(?presentations, "adding to db"); let mut index = self.presentation_library.items.len(); // Check if empty let mut tasks = Vec::new(); @@ -597,7 +628,7 @@ impl<'a> Library { } Message::VideoChanged => debug!("vid shoulda changed"), Message::UpdatePresentation(presentation) => { - let Some((kind, index)) = self.editing_item else { + let Some((kind, _index)) = self.editing_item else { error!("Not editing an item"); return Action::None; }; @@ -606,6 +637,14 @@ impl<'a> Library { error!("Not editing a presentation item"); return Action::None; } + let index = self + .presentation_library + .items + .iter() + .position(|pres| pres.id == presentation.id) + .unwrap_or_default() + .try_into() + .unwrap_or_default(); match self .presentation_library diff --git a/src/ui/presentation_editor.rs b/src/ui/presentation_editor.rs index d5a4682..b599cfd 100644 --- a/src/ui/presentation_editor.rs +++ b/src/ui/presentation_editor.rs @@ -192,7 +192,6 @@ impl PresentationEditor { } } Message::AddSlides(slides) => { - debug!(?slides); self.slides = slides; } Message::None => (), @@ -314,6 +313,7 @@ impl PresentationEditor { } Message::SplitBefore => { if let Ok((first, second)) = self.split_before() { + debug!(?first, ?second); self.update_entire_presentation(&first); return Action::SplitAddPresentation(( first, second, @@ -322,6 +322,7 @@ impl PresentationEditor { } Message::SplitAfter => { if let Ok((first, second)) = self.split_after() { + debug!(?first, ?second); self.update_entire_presentation(&first); return Action::SplitAddPresentation(( first, second, @@ -512,21 +513,51 @@ impl PresentationEditor { .as_ref() .and_then(|doc| doc.page_count().ok()); warn!("changing presentation"); - self.current_slide = self.document.as_ref().and_then(|doc| { - let page = doc.load_page(0).ok()?; - let matrix = Matrix::IDENTITY; - let colorspace = Colorspace::device_rgb(); - let pixmap = page - .to_pixmap(&matrix, &colorspace, true, true) - .ok()?; + let pages = if let PresKind::Pdf { + starting_index, + ending_index, + } = presentation.kind + { + self.current_slide = + self.document.as_ref().and_then(|doc| { + let page = doc.load_page(starting_index).ok()?; + let matrix = Matrix::IDENTITY; + let colorspace = Colorspace::device_rgb(); + let pixmap = page + .to_pixmap(&matrix, &colorspace, true, true) + .ok()?; - Some(Handle::from_rgba( - pixmap.width(), - pixmap.height(), - pixmap.samples().to_vec(), - )) - }); - self.current_slide_index = Some(0); + Some(Handle::from_rgba( + pixmap.width(), + pixmap.height(), + pixmap.samples().to_vec(), + )) + }); + self.current_slide_index = Some(starting_index); + get_pages( + starting_index..=ending_index, + presentation.path.clone(), + ) + } else { + self.current_slide = + self.document.as_ref().and_then(|doc| { + let page = doc.load_page(0).ok()?; + let matrix = Matrix::IDENTITY; + let colorspace = Colorspace::device_rgb(); + let pixmap = page + .to_pixmap(&matrix, &colorspace, true, true) + .ok()?; + + Some(Handle::from_rgba( + pixmap.width(), + pixmap.height(), + pixmap.samples().to_vec(), + )) + }); + self.current_slide_index = Some(0); + get_pages(.., presentation.path.clone()) + }; + self.slides = pages; } fn split_before(&self) -> Result<(Presentation, Presentation)> { @@ -552,7 +583,10 @@ impl PresentationEditor { }; let second_presentation = Presentation { id: 0, - title: current_presentation.title.clone(), + title: format!( + "{} (2)", + current_presentation.title.clone() + ), path: current_presentation.path.clone(), kind: match current_presentation.kind { PresKind::Pdf { ending_index, .. } => { @@ -596,7 +630,10 @@ impl PresentationEditor { }; let second_presentation = Presentation { id: 0, - title: current_presentation.title.clone(), + title: format!( + "{} (2)", + current_presentation.title.clone() + ), path: current_presentation.path.clone(), kind: match current_presentation.kind { PresKind::Pdf { ending_index, .. } => {