From 4479a11dff64dbf7030859b2f46804aed21e01da Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Fri, 29 May 2026 14:33:20 -0500 Subject: [PATCH] [feat] filtering and work for sorting --- src/core/images.rs | 34 ++++-- src/core/model.rs | 14 ++- src/core/presentations.rs | 32 ++++-- src/core/songs.rs | 34 ++++-- src/core/videos.rs | 34 ++++-- src/ui/library.rs | 215 ++++++++++++++++++++++++++++---------- 6 files changed, 275 insertions(+), 88 deletions(-) diff --git a/src/core/images.rs b/src/core/images.rs index 5025498..4e3f9f9 100644 --- a/src/core/images.rs +++ b/src/core/images.rs @@ -1,4 +1,4 @@ -use crate::core::model::Sort; +use crate::core::model::{Sort, SortDirection}; use crate::{Background, Slide, SlideBuilder, TextAlignment}; use super::content::Content; @@ -158,7 +158,7 @@ impl Model { let mut model = Self { items: vec![], kind: LibraryKind::Image, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), }; let mut db = db.acquire().await.expect("probs"); @@ -188,12 +188,30 @@ impl Model { pub fn sort(&mut self) { match self.sorting_method { - Sort::AccessTime => { + Sort::AccessTime(SortDirection::Descending) => { self.items.sort_by(|a, b| b.accessed_at.cmp(&a.accessed_at)) } - Sort::CreatedTime => todo!(), - Sort::Title => todo!(), - Sort::Secondary => todo!(), + Sort::AccessTime(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.accessed_at.cmp(&b.accessed_at)) + } + Sort::Title(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.title.cmp(&a.title)) + } + Sort::Title(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.title.cmp(&b.title)) + } + Sort::CreatedTime(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.created_at.cmp(&a.created_at)) + } + Sort::CreatedTime(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.created_at.cmp(&b.created_at)) + } + Sort::Secondary(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.path.cmp(&a.path)) + } + Sort::Secondary(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.path.cmp(&b.path)) + } } } @@ -327,7 +345,7 @@ mod test { let mut image_model: Model = Model { items: vec![], kind: LibraryKind::Image, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), }; let mut db = add_db() .await @@ -350,7 +368,7 @@ mod test { let mut image_model: Model = Model { items: vec![], kind: LibraryKind::Image, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), }; let result = image_model.add_item(image.clone()); let new_image = test_image("A newer image".into()); diff --git a/src/core/model.rs b/src/core/model.rs index 5417557..37cc070 100644 --- a/src/core/model.rs +++ b/src/core/model.rs @@ -26,10 +26,16 @@ pub enum LibraryKind { #[derive(Debug, Clone, Eq, PartialEq, Copy, Serialize, Deserialize)] pub enum Sort { - AccessTime, - CreatedTime, - Title, - Secondary, // This can be author or file name + AccessTime(SortDirection), + CreatedTime(SortDirection), + Title(SortDirection), + Secondary(SortDirection), // This can be author or file name +} + +#[derive(Debug, Clone, Eq, PartialEq, Copy, Serialize, Deserialize)] +pub enum SortDirection { + Ascending, + Descending, } #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] diff --git a/src/core/presentations.rs b/src/core/presentations.rs index 28f9efa..5e69c0a 100644 --- a/src/core/presentations.rs +++ b/src/core/presentations.rs @@ -13,7 +13,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use tracing::{debug, error}; -use crate::core::model::Sort; +use crate::core::model::{Sort, SortDirection}; use crate::{Background, Slide, SlideBuilder, TextAlignment}; use super::content::Content; @@ -299,7 +299,7 @@ impl Model { let mut model = Self { items: vec![], kind: LibraryKind::Presentation, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), }; model.load_from_db(db).await; model @@ -363,12 +363,30 @@ impl Model { pub fn sort(&mut self) { match self.sorting_method { - Sort::AccessTime => { + Sort::AccessTime(SortDirection::Descending) => { self.items.sort_by(|a, b| b.accessed_at.cmp(&a.accessed_at)) } - Sort::CreatedTime => todo!(), - Sort::Title => todo!(), - Sort::Secondary => todo!(), + Sort::AccessTime(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.accessed_at.cmp(&b.accessed_at)) + } + Sort::Title(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.title.cmp(&a.title)) + } + Sort::Title(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.title.cmp(&b.title)) + } + Sort::CreatedTime(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.created_at.cmp(&a.created_at)) + } + Sort::CreatedTime(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.created_at.cmp(&b.created_at)) + } + Sort::Secondary(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.path.cmp(&a.path)) + } + Sort::Secondary(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.path.cmp(&b.path)) + } } } @@ -549,7 +567,7 @@ mod test { let mut presentation_model: Model = Model { items: vec![], kind: LibraryKind::Presentation, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), }; let db = Arc::new(add_db().await.expect("Getting db error")); presentation_model.load_from_db(db).await; diff --git a/src/core/songs.rs b/src/core/songs.rs index aadc59a..9c949ff 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -19,7 +19,7 @@ use tracing::{debug, error}; use crate::core::content::Content; use crate::core::kinds::ServiceItemKind; -use crate::core::model::{LibraryKind, Model, Sort}; +use crate::core::model::{LibraryKind, Model, Sort, SortDirection}; use crate::core::service_items::ServiceTrait; use crate::core::slide::{self, Background, TextAlignment}; use crate::ui::text_svg::{Color, Font, Stroke, shadow, stroke}; @@ -667,7 +667,7 @@ impl Model { let mut model = Self { items: vec![], kind: LibraryKind::Song, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), }; model.load_from_db(db).await; @@ -702,12 +702,32 @@ impl Model { pub fn sort(&mut self) { match self.sorting_method { - Sort::AccessTime => { + Sort::AccessTime(SortDirection::Descending) => { self.items.sort_by(|a, b| b.accessed_at.cmp(&a.accessed_at)) } - Sort::CreatedTime => todo!(), - Sort::Title => todo!(), - Sort::Secondary => todo!(), + Sort::AccessTime(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.accessed_at.cmp(&b.accessed_at)) + } + Sort::Title(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.title.cmp(&a.title)) + } + Sort::Title(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.title.cmp(&b.title)) + } + Sort::CreatedTime(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.created_at.cmp(&a.created_at)) + } + Sort::CreatedTime(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.created_at.cmp(&b.created_at)) + } + Sort::Secondary(SortDirection::Descending) => { + self.items.sort_by(|a, b| b.author.cmp(&a.author)) + } + Sort::Secondary(SortDirection::Ascending) => { + self.items.sort_by(|a, b| a.author.cmp(&b.author)) + } // Sort::CreatedTime => todo!(), + // Sort::Title => todo!(), + // Sort::Secondary => todo!(), } } @@ -1315,7 +1335,7 @@ You saved my soul" let song_model: Model = Model { items: vec![], kind: LibraryKind::Song, - sorting_method: Sort::AccessTime, + sorting_method: Sort::AccessTime(SortDirection::Descending), // db: crate::core::model::get_db().await, }; song_model diff --git a/src/core/videos.rs b/src/core/videos.rs index 4f9ef24..5bc00a8 100644 --- a/src/core/videos.rs +++ b/src/core/videos.rs @@ -1,4 +1,4 @@ -use crate::core::model::Sort; +use crate::core::model::{Sort, SortDirection}; use crate::{Background, SlideBuilder, TextAlignment}; use super::content::Content; @@ -184,7 +184,7 @@ impl Model