From 757e20c1c805f4e238b391adab8cfc00645ab80b Mon Sep 17 00:00:00 2001 From: David Bliss Date: Wed, 3 Apr 2024 17:57:18 +0100 Subject: [PATCH] Add refresh for year_photos --- src/app.rs | 3 ++- src/app/components/year_photos.rs | 41 +++++++++++++++++++------------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 5af060d7..180e3ada 100644 --- a/src/app.rs +++ b/src/app.rs @@ -30,7 +30,7 @@ use self::components::{ all_photos::{AllPhotos, AllPhotosInput, AllPhotosOutput}, month_photos::{MonthPhotos, MonthPhotosOutput, MonthPhotosInput}, one_photo::{OnePhoto, OnePhotoInput}, - year_photos::{YearPhotos, YearPhotosOutput}, + year_photos::{YearPhotos, YearPhotosInput, YearPhotosOutput}, selfie_photos::{SelfiePhotos, SelfiePhotosInput, SelfiePhotosOutput,}, }; @@ -465,6 +465,7 @@ impl SimpleComponent for App { self.all_photos.emit(AllPhotosInput::Refresh); self.selfie_photos.emit(SelfiePhotosInput::Refresh); self.month_photos.emit(MonthPhotosInput::Refresh); + self.year_photos.emit(YearPhotosInput::Refresh); self.generate_previews.emit(GeneratePreviewsInput::Generate); }, diff --git a/src/app/components/year_photos.rs b/src/app/components/year_photos.rs index 391d65e2..032aa53d 100644 --- a/src/app/components/year_photos.rs +++ b/src/app/components/year_photos.rs @@ -22,6 +22,9 @@ struct PhotoGridItem { pub enum YearPhotosInput { /// User has selected year in grid view YearSelected(u32), // WARN this is an index into an Vec, not a year. + + // Reload photos from database + Refresh, } #[derive(Debug)] @@ -93,6 +96,7 @@ impl RelmGridItem for PhotoGridItem { } pub struct YearPhotos { + repo: Arc>, pictures_grid_view: TypedGridView, } @@ -132,29 +136,15 @@ impl SimpleComponent for YearPhotos { root: Self::Root, sender: ComponentSender, ) -> ComponentParts { - let all_pictures = repo - .lock().unwrap() - .all() - .unwrap() - .into_iter() - .dedup_by(|x, y| x.year() == y.year()) - .map(|picture| PhotoGridItem { - picture, - }); - - let mut grid_view_wrapper: TypedGridView = - TypedGridView::new(); - grid_view_wrapper.extend_from_iter(all_pictures.into_iter()); + let mut grid_view_wrapper = TypedGridView::new(); let model = YearPhotos { + repo, pictures_grid_view: grid_view_wrapper, }; let pictures_box = &model.pictures_grid_view.view; - if !model.pictures_grid_view.is_empty(){ - pictures_box.scroll_to(model.pictures_grid_view.len() - 1, gtk::ListScrollFlags::SELECT, None); - } let widgets = view_output!(); ComponentParts { model, widgets } @@ -162,6 +152,25 @@ impl SimpleComponent for YearPhotos { fn update(&mut self, msg: Self::Input, sender: ComponentSender) { match msg { + YearPhotosInput::Refresh => { + let all_pictures = self.repo + .lock().unwrap() + .all() + .unwrap() + .into_iter() + .dedup_by(|x, y| x.year() == y.year()) + .map(|picture| PhotoGridItem { + picture, + }); + + self.pictures_grid_view.clear(); + self.pictures_grid_view.extend_from_iter(all_pictures.into_iter()); + + if !self.pictures_grid_view.is_empty(){ + self.pictures_grid_view.view + .scroll_to(self.pictures_grid_view.len() - 1, gtk::ListScrollFlags::SELECT, None); + } + }, YearPhotosInput::YearSelected(index) => { if let Some(item) = self.pictures_grid_view.get(index) { let date = item.borrow().picture.year_month();