Skip to content

Commit

Permalink
Background scan
Browse files Browse the repository at this point in the history
  • Loading branch information
blissd committed Mar 30, 2024
1 parent bdc1ca4 commit 59bcb15
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 31 deletions.
2 changes: 1 addition & 1 deletion core/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl FsMetadata {
}

/// Scans a file system for pictures.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Scanner {
/// File system path to scan.
scan_base: PathBuf,
Expand Down
66 changes: 36 additions & 30 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use relm4::{
actions::{RelmAction, RelmActionGroup},
adw, gtk, main_application, Component, ComponentController, ComponentParts, ComponentSender,
Controller, SimpleComponent,
Controller, SimpleComponent, WorkerController,
};

use gtk::prelude::{
Expand All @@ -20,26 +20,25 @@ use relm4::adw::prelude::NavigationPageExt;
use std::cell::RefCell;
use std::rc::Rc;


mod components;

use self::{
components::{
about::AboutDialog,
all_photos::AllPhotos,
all_photos::AllPhotosInput,
all_photos::AllPhotosOutput,
month_photos::MonthPhotos,
month_photos::MonthPhotosInput,
month_photos::MonthPhotosOutput,
year_photos::YearPhotos,
year_photos::YearPhotosOutput,
one_photo::OnePhoto,
one_photo::OnePhotoInput,
}
use self::components::{
about::AboutDialog, all_photos::AllPhotos, all_photos::AllPhotosInput,
all_photos::AllPhotosOutput, month_photos::MonthPhotos, month_photos::MonthPhotosInput,
month_photos::MonthPhotosOutput, one_photo::OnePhoto, one_photo::OnePhotoInput,
year_photos::YearPhotos, year_photos::YearPhotosOutput,
};

mod background;

use self::background::{
scan_photos::ScanPhotos,
scan_photos::ScanPhotosInput,
scan_photos::ScanPhotosOutput,
};

pub(super) struct App {
scan_photos: WorkerController<ScanPhotos>,
about_dialog: Controller<AboutDialog>,
all_photos: Controller<AllPhotos>,
month_photos: Controller<MonthPhotos>,
Expand All @@ -66,6 +65,8 @@ pub(super) enum AppMsg {
// Scroll to first photo in year
GoToYear(i32),

// Photos have been scanned and repo can be updated
ScanAllCompleted(Vec<photos_core::scanner::Picture>),
}

relm4::new_action_group!(pub(super) WindowActionGroup, "win");
Expand Down Expand Up @@ -137,7 +138,7 @@ impl SimpleComponent for App {

#[name = "header_bar"]
add_top_bar = &adw::HeaderBar {
set_hexpand: true,
set_hexpand: true,

#[wrap(Some)]
set_title_widget = &adw::ViewSwitcher {
Expand Down Expand Up @@ -208,15 +209,7 @@ impl SimpleComponent for App {
photos_core::Previewer::build(&preview_base_path).unwrap()
};

let mut controller = photos_core::Controller::new(scan, repo, previewer);

// Time consuming!
match controller.scan() {
Err(e) => {
println!("Failed scanning: {:?}", e);
}
_ => {}
}
let controller = photos_core::Controller::new(scan.clone(), repo, previewer);

let controller = Rc::new(RefCell::new(controller));

Expand All @@ -225,18 +218,27 @@ impl SimpleComponent for App {
//println!("preview result: {:?}", result);
}

let scan_photos = ScanPhotos::builder()
.detach_worker(scan)
.forward(sender.input_sender(), |msg| match msg {
ScanPhotosOutput::ScanAllCompleted(pics) => AppMsg::ScanAllCompleted(pics),
});


let all_photos = AllPhotos::builder()
.launch(controller.clone())
.forward(sender.input_sender(), |msg| match msg {
AllPhotosOutput::PhotoSelected(id) => AppMsg::ViewPhoto(id),
});

let month_photos = MonthPhotos::builder().launch(controller.clone())
let month_photos = MonthPhotos::builder()
.launch(controller.clone())
.forward(sender.input_sender(), |msg| match msg {
MonthPhotosOutput::MonthSelected(ym) => AppMsg::GoToMonth(ym),
});

let year_photos = YearPhotos::builder().launch(controller.clone())
let year_photos = YearPhotos::builder()
.launch(controller.clone())
.forward(sender.input_sender(), |msg| match msg {
YearPhotosOutput::YearSelected(year) => AppMsg::GoToYear(year),
});
Expand All @@ -253,6 +255,7 @@ impl SimpleComponent for App {
let picture_navigation_view = adw::NavigationView::builder().build();

let model = Self {
scan_photos,
about_dialog,
all_photos,
month_photos,
Expand Down Expand Up @@ -286,14 +289,15 @@ impl SimpleComponent for App {

widgets.load_window_size();

model.scan_photos.sender().emit(ScanPhotosInput::ScanAll);

ComponentParts { model, widgets }
}

fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
match message {
AppMsg::Quit => main_application().quit(),
AppMsg::ViewPhoto(picture_id) => {
println!("Forward Showing photo for {}", picture_id);
// Send message to OnePhoto to show image
self.one_photo.emit(OnePhotoInput::ViewPhoto(picture_id));

Expand All @@ -310,7 +314,9 @@ impl SimpleComponent for App {
self.view_stack.set_visible_child_name("month");
self.month_photos.emit(MonthPhotosInput::GoToYear(year));
},

AppMsg::ScanAllCompleted(pics) => {
println!("Scan all completed msg received. {} pics.", pics.len());
},
}
}

Expand Down
1 change: 1 addition & 0 deletions src/app/background/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod scan_photos;
45 changes: 45 additions & 0 deletions src/app/background/scan_photos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use relm4::prelude::*;
use relm4::Worker;

#[derive(Debug)]
pub enum ScanPhotosInput {
ScanAll,
}

#[derive(Debug)]
pub enum ScanPhotosOutput {
ScanAllCompleted(Vec<photos_core::scanner::Picture>),
}

pub struct ScanPhotos {
scanner: photos_core::Scanner,
}

impl Worker for ScanPhotos {
type Init = photos_core::Scanner;
type Input = ScanPhotosInput;
type Output = ScanPhotosOutput;

fn init(scanner: Self::Init, _sender: ComponentSender<Self>) -> Self {
Self { scanner }
}

fn update(&mut self, msg: ScanPhotosInput, sender: ComponentSender<Self>) {
match msg {
ScanPhotosInput::ScanAll => {
println!("ScanAll");
let start_at = std::time::SystemTime::now();
let result = self.scanner.scan_all();
let end_at = std::time::SystemTime::now();

if let Ok(pics) = result {
let duration = end_at.duration_since(start_at).unwrap_or(std::time::Duration::new(0, 0));
println!("Scanned {} items in {} seconds", pics.len(), duration.as_secs());
let _ = sender.output(ScanPhotosOutput::ScanAllCompleted(pics));
} else {
println!("Failed scanning: {:?}", result);
}
}
};
}
}

0 comments on commit 59bcb15

Please sign in to comment.