From 6142fd1c765d0ac0ead746addae9b9292595290e Mon Sep 17 00:00:00 2001 From: David Bliss Date: Tue, 26 Mar 2024 08:48:09 +0000 Subject: [PATCH] Basic year view implementation --- Cargo.lock | 2 +- Cargo.toml | 2 +- core/src/repo.rs | 8 ++++++++ src/app.rs | 3 +-- src/main.rs | 2 +- src/year_photos.rs | 35 ++++++++++++++++++++--------------- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69147a25..78f69ca7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1326,8 +1326,8 @@ checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" name = "photo-romantic" version = "0.1.0" dependencies = [ - "chrono", "gettext-rs", + "itertools", "photos_core", "relm4", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 4bdb3393..4c915891 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ gettext-rs = { version = "0.7", features = ["gettext-system"] } tracing = "0.1.37" tracing-subscriber = "0.3" relm4 = { version = "0.8.1", features = ["libadwaita", "gnome_45"] } -chrono = "0.4.35" +itertools = "0.12.1" [dependencies.photos_core] diff --git a/core/src/repo.rs b/core/src/repo.rs index 480ad2d5..27937400 100644 --- a/core/src/repo.rs +++ b/core/src/repo.rs @@ -48,6 +48,14 @@ pub struct Picture { pub order_by_ts: Option>, } +impl Picture { + pub fn year(&self) -> u32 { + self.order_by_ts + .map(|ts| ts.date_naive().year_ce().1) + .unwrap_or(0) + } +} + /// Repository of picture metadata. /// Repository is backed by a Sqlite database. #[derive(Debug)] diff --git a/src/app.rs b/src/app.rs index bcb64a03..a997a28e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -15,9 +15,9 @@ use gtk::{gio, glib}; use relm4::adw::prelude::AdwApplicationWindowExt; use crate::all_photos::AllPhotos; -use crate::year_photos::YearPhotos; use crate::config::{APP_ID, PROFILE}; use crate::modals::about::AboutDialog; +use crate::year_photos::YearPhotos; pub(super) struct App { about_dialog: Controller, @@ -140,7 +140,6 @@ impl SimpleComponent for App { let all_photos = AllPhotos::builder().launch(()).detach(); let year_photos = YearPhotos::builder().launch(()).detach(); - let model = Self { about_dialog, all_photos, diff --git a/src/main.rs b/src/main.rs index f1d33cc9..17069109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,9 @@ #[rustfmt::skip] mod config; mod all_photos; -mod year_photos; mod app; mod modals; +mod year_photos; use app::App; use config::{APP_ID, GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE}; diff --git a/src/year_photos.rs b/src/year_photos.rs index e95e2e27..2bdd0617 100644 --- a/src/year_photos.rs +++ b/src/year_photos.rs @@ -5,27 +5,26 @@ use gtk::glib; use gtk::prelude::{BoxExt, OrientableExt}; use photos_core; -use chrono::Datelike; +use itertools::Itertools; use relm4::gtk; +use relm4::gtk::prelude::FrameExt; use relm4::gtk::prelude::WidgetExt; use relm4::typed_view::grid::{RelmGridItem, TypedGridView}; use relm4::*; use std::cell::RefCell; use std::path; use std::rc::Rc; -use relm4::gtk::prelude::FrameExt; #[derive(Debug)] pub struct PicturePreview { controller: Rc>, picture: photos_core::repo::Picture, - year: u32, } pub struct Widgets { picture: gtk::Picture, - frame: gtk::Frame, + label: gtk::Label, } impl RelmGridItem for PicturePreview { @@ -38,9 +37,15 @@ impl RelmGridItem for PicturePreview { set_orientation: gtk::Orientation::Vertical, set_margin_all: 1, - #[name = "frame"] + #[name(label)] + gtk::Label {}, + + adw::Clamp { + set_maximum_size: 200, + gtk::Frame { - #[name = "picture"] + + #[name(picture)] gtk::Picture { set_can_shrink: true, set_valign: gtk::Align::Center, @@ -48,16 +53,19 @@ impl RelmGridItem for PicturePreview { set_height_request: 200, } } + } } } - let widgets = Widgets { picture, frame }; + let widgets = Widgets { picture, label }; (my_box, widgets) } fn bind(&mut self, widgets: &mut Self::Widgets, _root: &mut Self::Root) { - widgets.frame.set_label(Some(format!("{}", self.year).as_str())); + widgets + .label + .set_label(format!("{}", self.picture.year()).as_str()); // compute preview image if it is absent if self.picture.square_preview_path.is_none() { @@ -160,13 +168,10 @@ impl SimpleComponent for YearPhotos { .all_with_previews() .unwrap() .into_iter() - .map(|picture| { - let year = picture.order_by_ts.map(|ts| ts.date_naive().year_ce().1).unwrap(); - PicturePreview { - picture, - year, - controller: controller.clone(), - } + .dedup_by(|x, y| x.year() == y.year()) + .map(|picture| PicturePreview { + picture, + controller: controller.clone(), }); let mut grid_view_wrapper: TypedGridView =