From 89294061b7991a8cbdcee259a29297ecc79d0399 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 14 Jan 2025 15:03:59 -0600 Subject: [PATCH] a fledgling visible library system Why do I write these weird commit messages.... --- src/core/images.rs | 19 +++++-- src/core/model.rs | 14 ++++-- src/core/presentations.rs | 21 ++++++-- src/core/songs.rs | 16 ++++-- src/core/videos.rs | 18 +++++-- src/main.rs | 53 ++++++++++++++++---- src/ui/library.rs | 102 +++++++++++++++++++++++++++----------- 7 files changed, 188 insertions(+), 55 deletions(-) diff --git a/src/core/images.rs b/src/core/images.rs index d09f98b..0fa339c 100644 --- a/src/core/images.rs +++ b/src/core/images.rs @@ -1,6 +1,9 @@ use crate::{Background, Slide, SlideBuilder, TextAlignment}; -use super::{model::Model, service_items::ServiceTrait}; +use super::{ + model::{get_db, LibraryKind, Model}, + service_items::ServiceTrait, +}; use crisp::types::{Keyword, Value}; use miette::{IntoDiagnostic, Result}; use serde::{Deserialize, Serialize}; @@ -90,12 +93,22 @@ impl ServiceTrait for Image { } impl Model { - pub async fn load_from_db(&mut self) { + pub async fn new_image_model(db: &mut SqliteConnection) -> Self { + let mut model = Self { + items: vec![], + kind: LibraryKind::Image, + }; + + model.load_from_db(db).await; + model + } + + pub async fn load_from_db(&mut self, db: &mut SqliteConnection) { let result = query_as!( Image, r#"SELECT title as "title!", file_path as "path!", id as "id: i32" from images"# ) - .fetch_all(&mut self.db) + .fetch_all(db) .await; match result { Ok(v) => { diff --git a/src/core/model.rs b/src/core/model.rs index 2f38cf7..9ff6dcd 100644 --- a/src/core/model.rs +++ b/src/core/model.rs @@ -6,12 +6,20 @@ use sqlx::{Connection, SqliteConnection}; use super::kinds::ServiceItemKind; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Model { pub items: Vec, - pub db: SqliteConnection, - pub kind: ServiceItemKind, + pub kind: LibraryKind, } + +#[derive(Debug, Clone)] +pub enum LibraryKind { + Song, + Video, + Image, + Presentation, +} + impl Model { pub fn add_item(&mut self, item: T) -> Result<()> { self.items.push(item); diff --git a/src/core/presentations.rs b/src/core/presentations.rs index 1e390d1..a9ce0bc 100644 --- a/src/core/presentations.rs +++ b/src/core/presentations.rs @@ -9,7 +9,10 @@ use tracing::error; use crate::{Background, Slide, SlideBuilder, TextAlignment}; -use super::{model::Model, service_items::ServiceTrait}; +use super::{ + model::{get_db, LibraryKind, Model}, + service_items::ServiceTrait, +}; #[derive( Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, @@ -131,11 +134,23 @@ impl FromRow<'_, SqliteRow> for Presentation { } impl Model { - pub async fn load_from_db(&mut self) { + pub async fn new_presentation_model( + db: &mut SqliteConnection, + ) -> Self { + let mut model = Self { + items: vec![], + kind: LibraryKind::Presentation, + }; + + model.load_from_db(db).await; + model + } + + pub async fn load_from_db(&mut self, db: &mut SqliteConnection) { let result = query!( r#"SELECT id as "id: i32", title, file_path as "path", html from presentations"# ) - .fetch_all(&mut self.db) + .fetch_all(db) .await; match result { Ok(v) => { diff --git a/src/core/songs.rs b/src/core/songs.rs index 7cfddd8..98c7ce2 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -12,7 +12,7 @@ use tracing::{debug, error}; use crate::{core::slide, Slide, SlideBuilder}; use super::{ - model::Model, + model::{get_db, LibraryKind, Model}, service_items::ServiceTrait, slide::{Background, TextAlignment}, }; @@ -348,9 +348,19 @@ pub async fn get_song_from_db( } impl Model { - pub async fn load_from_db(&mut self) { + pub async fn new_song_model(db: &mut SqliteConnection) -> Self { + let mut model = Self { + items: vec![], + kind: LibraryKind::Song, + }; + + model.load_from_db(db).await; + model + } + + pub async fn load_from_db(&mut self, db: &mut SqliteConnection) { // static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3"; - let result = query(r#"SELECT verse_order as "verse_order!", font_size as "font_size!: i32", background_type as "background_type!", horizontal_text_alignment as "horizontal_text_alignment!", vertical_text_alignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs"#).fetch_all(&mut self.db).await; + let result = query(r#"SELECT verse_order as "verse_order!", font_size as "font_size!: i32", background_type as "background_type!", horizontal_text_alignment as "horizontal_text_alignment!", vertical_text_alignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs"#).fetch_all(db).await; match result { Ok(s) => { for song in s.into_iter() { diff --git a/src/core/videos.rs b/src/core/videos.rs index ed5ed03..c75b99d 100644 --- a/src/core/videos.rs +++ b/src/core/videos.rs @@ -1,7 +1,9 @@ use crate::{Background, SlideBuilder, TextAlignment}; use super::{ - model::Model, service_items::ServiceTrait, slide::Slide, + model::{get_db, LibraryKind, Model}, + service_items::ServiceTrait, + slide::Slide, }; use cosmic::iced::Executor; use crisp::types::{Keyword, Value}; @@ -136,8 +138,18 @@ impl ServiceTrait for Video { } impl Model