Add reload button functionality
This commit is contained in:
parent
3f5e621b6b
commit
7b931fb033
102
src/main.rs
102
src/main.rs
@ -1,20 +1,16 @@
|
|||||||
mod database;
|
mod database;
|
||||||
mod sheet;
|
mod sheet;
|
||||||
mod sheet_dao;
|
mod sheet_dao;
|
||||||
|
mod sheet_validation;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
use std::{
|
use std::{path::PathBuf, process};
|
||||||
path::{Path, PathBuf},
|
|
||||||
process,
|
|
||||||
};
|
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use database::Database;
|
use database::Database;
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use log::{debug, error};
|
use log::error;
|
||||||
use relm4::RelmApp;
|
use relm4::RelmApp;
|
||||||
use sheet::{Pdf, Sheet};
|
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
use crate::ui::app::{AppInitData, AppModel};
|
use crate::ui::app::{AppInitData, AppModel};
|
||||||
|
|
||||||
@ -36,96 +32,16 @@ async fn main() {
|
|||||||
let database = Database::setup(cli.directory.join("database.sqlite"))
|
let database = Database::setup(cli.directory.join("database.sqlite"))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let sheets = sheet_dao::fetch_all_sheets(&database).await.unwrap();
|
|
||||||
|
|
||||||
debug!("Validating sheets from database...");
|
let sheets = sheet_validation::load_and_validate_sheets(&database, &cli.directory).await;
|
||||||
let mut validation_result = validate_sheet_files(sheets, &cli.directory);
|
|
||||||
debug!("{}", validation_result.get_stats()); // TODO: handle invalidated files
|
|
||||||
for updated in validation_result.updated_sheets.iter() {
|
|
||||||
sheet_dao::update_sheet_path(&database, updated)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut sheets = validation_result.validated_sheets;
|
|
||||||
sheets.append(&mut validation_result.updated_sheets);
|
|
||||||
|
|
||||||
debug!("Inserting unassigned files into orphan table...");
|
|
||||||
for unassigned in validation_result.unassigned_files {
|
|
||||||
let orphan = sheet_dao::insert_file_as_orphan(&database, unassigned)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
sheets.push(orphan);
|
|
||||||
}
|
|
||||||
|
|
||||||
let app_init_data = AppInitData { sheets, database };
|
|
||||||
|
|
||||||
|
let app_init_data = AppInitData {
|
||||||
|
sheets,
|
||||||
|
database,
|
||||||
|
directory: cli.directory,
|
||||||
|
};
|
||||||
let app = RelmApp::new("de.frajul.sheet-organizer");
|
let app = RelmApp::new("de.frajul.sheet-organizer");
|
||||||
// Pass empty command line args to allow my own parsing
|
// Pass empty command line args to allow my own parsing
|
||||||
app.with_args(Vec::new())
|
app.with_args(Vec::new())
|
||||||
.run_async::<AppModel>(app_init_data);
|
.run_async::<AppModel>(app_init_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FileValidationResult {
|
|
||||||
validated_sheets: Vec<Sheet>,
|
|
||||||
invalidated_sheets: Vec<Sheet>,
|
|
||||||
updated_sheets: Vec<Sheet>,
|
|
||||||
|
|
||||||
unassigned_files: Vec<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileValidationResult {
|
|
||||||
fn get_stats(&self) -> String {
|
|
||||||
format!("Validated sheets: {}\nInvalidated sheets: {}\nUpdated sheets: {}\nUnassigned files: {}",
|
|
||||||
self.validated_sheets.len(), self.invalidated_sheets.len(), self.updated_sheets.len(),
|
|
||||||
self.unassigned_files.len())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValidationResult {
|
|
||||||
let (validated_sheets, mut invalidated_sheets): (Vec<_>, Vec<_>) = sheets
|
|
||||||
.into_iter()
|
|
||||||
.partition(|sheet| sheet.pdf.validate_own_path().unwrap_or(false));
|
|
||||||
|
|
||||||
let mut updated_sheets = Vec::new();
|
|
||||||
let mut unassigned_files = Vec::new();
|
|
||||||
|
|
||||||
// TODO: improve performance?
|
|
||||||
for pdf_file in find_all_pdfs_in_directory_recursive(dir) {
|
|
||||||
if let Some((i, _)) = invalidated_sheets
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.find(|(_, sheet)| sheet.pdf.validate_path(&pdf_file).unwrap_or(false))
|
|
||||||
{
|
|
||||||
let mut sheet = invalidated_sheets.remove(i);
|
|
||||||
let new_pdf = Pdf::try_from(pdf_file).unwrap();
|
|
||||||
sheet.pdf = new_pdf;
|
|
||||||
updated_sheets.push(sheet);
|
|
||||||
} else if !validated_sheets
|
|
||||||
.iter()
|
|
||||||
.any(|sheet| sheet.pdf.path == pdf_file)
|
|
||||||
{
|
|
||||||
unassigned_files.push(pdf_file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FileValidationResult {
|
|
||||||
validated_sheets,
|
|
||||||
invalidated_sheets,
|
|
||||||
updated_sheets,
|
|
||||||
unassigned_files,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_all_pdfs_in_directory_recursive(dir: impl AsRef<Path>) -> impl Iterator<Item = PathBuf> {
|
|
||||||
WalkDir::new(dir)
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|e| e.ok())
|
|
||||||
.filter(|file| file.file_type().is_file())
|
|
||||||
.map(|file| file.into_path())
|
|
||||||
.filter(|path| {
|
|
||||||
path.extension()
|
|
||||||
.map(|s| s.to_string_lossy().to_ascii_lowercase() == "pdf")
|
|
||||||
.unwrap_or(false)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
103
src/sheet_validation.rs
Normal file
103
src/sheet_validation.rs
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use log::debug;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::Database,
|
||||||
|
sheet::{Pdf, Sheet},
|
||||||
|
sheet_dao,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn load_and_validate_sheets(
|
||||||
|
database: &Database,
|
||||||
|
directory: impl AsRef<Path>,
|
||||||
|
) -> Vec<Sheet> {
|
||||||
|
let sheets = sheet_dao::fetch_all_sheets(&database).await.unwrap();
|
||||||
|
|
||||||
|
debug!("Validating sheets from database...");
|
||||||
|
let mut validation_result = validate_sheet_files(sheets, directory);
|
||||||
|
debug!("{}", validation_result.get_stats()); // TODO: handle invalidated files
|
||||||
|
for updated in validation_result.updated_sheets.iter() {
|
||||||
|
sheet_dao::update_sheet_path(&database, updated)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sheets = validation_result.validated_sheets;
|
||||||
|
sheets.append(&mut validation_result.updated_sheets);
|
||||||
|
|
||||||
|
debug!("Inserting unassigned files into orphan table...");
|
||||||
|
for unassigned in validation_result.unassigned_files {
|
||||||
|
let orphan = sheet_dao::insert_file_as_orphan(&database, unassigned)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
sheets.push(orphan);
|
||||||
|
}
|
||||||
|
|
||||||
|
sheets
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FileValidationResult {
|
||||||
|
validated_sheets: Vec<Sheet>,
|
||||||
|
invalidated_sheets: Vec<Sheet>,
|
||||||
|
updated_sheets: Vec<Sheet>,
|
||||||
|
|
||||||
|
unassigned_files: Vec<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileValidationResult {
|
||||||
|
fn get_stats(&self) -> String {
|
||||||
|
format!("Validated sheets: {}\nInvalidated sheets: {}\nUpdated sheets: {}\nUnassigned files: {}",
|
||||||
|
self.validated_sheets.len(), self.invalidated_sheets.len(), self.updated_sheets.len(),
|
||||||
|
self.unassigned_files.len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValidationResult {
|
||||||
|
let (validated_sheets, mut invalidated_sheets): (Vec<_>, Vec<_>) = sheets
|
||||||
|
.into_iter()
|
||||||
|
.partition(|sheet| sheet.pdf.validate_own_path().unwrap_or(false));
|
||||||
|
|
||||||
|
let mut updated_sheets = Vec::new();
|
||||||
|
let mut unassigned_files = Vec::new();
|
||||||
|
|
||||||
|
// TODO: improve performance?
|
||||||
|
for pdf_file in find_all_pdfs_in_directory_recursive(dir) {
|
||||||
|
if let Some((i, _)) = invalidated_sheets
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, sheet)| sheet.pdf.validate_path(&pdf_file).unwrap_or(false))
|
||||||
|
{
|
||||||
|
let mut sheet = invalidated_sheets.remove(i);
|
||||||
|
let new_pdf = Pdf::try_from(pdf_file).unwrap();
|
||||||
|
sheet.pdf = new_pdf;
|
||||||
|
updated_sheets.push(sheet);
|
||||||
|
} else if !validated_sheets
|
||||||
|
.iter()
|
||||||
|
.any(|sheet| sheet.pdf.path == pdf_file)
|
||||||
|
{
|
||||||
|
unassigned_files.push(pdf_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FileValidationResult {
|
||||||
|
validated_sheets,
|
||||||
|
invalidated_sheets,
|
||||||
|
updated_sheets,
|
||||||
|
unassigned_files,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_all_pdfs_in_directory_recursive(dir: impl AsRef<Path>) -> impl Iterator<Item = PathBuf> {
|
||||||
|
WalkDir::new(dir)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|e| e.ok())
|
||||||
|
.filter(|file| file.file_type().is_file())
|
||||||
|
.map(|file| file.into_path())
|
||||||
|
.filter(|path| {
|
||||||
|
path.extension()
|
||||||
|
.map(|s| s.to_string_lossy().to_ascii_lowercase() == "pdf")
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
|
}
|
@ -1,3 +1,8 @@
|
|||||||
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
@ -8,9 +13,9 @@ use relm4::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
database::Database,
|
database::{self, Database},
|
||||||
sheet::{I64DateTime, Sheet},
|
sheet::{I64DateTime, Sheet},
|
||||||
sheet_dao,
|
sheet_dao, sheet_validation,
|
||||||
ui::mcdu::McduOutput,
|
ui::mcdu::McduOutput,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -20,7 +25,8 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct AppModel {
|
pub struct AppModel {
|
||||||
database: Database,
|
database: Arc<Database>,
|
||||||
|
directory: Arc<PathBuf>,
|
||||||
mcdu: Controller<McduModel>,
|
mcdu: Controller<McduModel>,
|
||||||
sheets_listing: Controller<SheetListingModel>,
|
sheets_listing: Controller<SheetListingModel>,
|
||||||
edit_mode: bool,
|
edit_mode: bool,
|
||||||
@ -39,6 +45,7 @@ pub enum AppInput {
|
|||||||
pub struct AppInitData {
|
pub struct AppInitData {
|
||||||
pub sheets: Vec<Sheet>,
|
pub sheets: Vec<Sheet>,
|
||||||
pub database: Database,
|
pub database: Database,
|
||||||
|
pub directory: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[relm4::component(pub, async)]
|
#[relm4::component(pub, async)]
|
||||||
@ -46,7 +53,7 @@ impl AsyncComponent for AppModel {
|
|||||||
type Input = AppInput;
|
type Input = AppInput;
|
||||||
type Output = ();
|
type Output = ();
|
||||||
type Init = AppInitData;
|
type Init = AppInitData;
|
||||||
type CommandOutput = ();
|
type CommandOutput = Vec<Sheet>;
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
#[root]
|
#[root]
|
||||||
@ -87,9 +94,9 @@ impl AsyncComponent for AppModel {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
gtk::ScrolledWindow {
|
gtk::ScrolledWindow {
|
||||||
model.sheets_listing.widget(),
|
model.sheets_listing.widget(),
|
||||||
set_vexpand: true,
|
set_vexpand: true,
|
||||||
set_hexpand: true,
|
set_hexpand: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
model.mcdu.widget() {
|
model.mcdu.widget() {
|
||||||
@ -122,7 +129,8 @@ impl AsyncComponent for AppModel {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let model = AppModel {
|
let model = AppModel {
|
||||||
database: init_data.database,
|
database: Arc::new(init_data.database),
|
||||||
|
directory: Arc::new(init_data.directory),
|
||||||
mcdu,
|
mcdu,
|
||||||
sheets_listing,
|
sheets_listing,
|
||||||
edit_mode: false,
|
edit_mode: false,
|
||||||
@ -136,7 +144,7 @@ impl AsyncComponent for AppModel {
|
|||||||
async fn update(
|
async fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
message: Self::Input,
|
message: Self::Input,
|
||||||
_sender: AsyncComponentSender<Self>,
|
sender: AsyncComponentSender<Self>,
|
||||||
_root: &Self::Root,
|
_root: &Self::Root,
|
||||||
) {
|
) {
|
||||||
match message {
|
match message {
|
||||||
@ -156,10 +164,31 @@ impl AsyncComponent for AppModel {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AppInput::Refresh => todo!(),
|
AppInput::Refresh => {
|
||||||
|
let db = Arc::clone(&self.database);
|
||||||
|
let dir = Arc::clone(&self.directory);
|
||||||
|
sender.oneshot_command(async move {
|
||||||
|
let sheets =
|
||||||
|
sheet_validation::load_and_validate_sheets(&db, dir.as_ref()).await;
|
||||||
|
return sheets;
|
||||||
|
})
|
||||||
|
}
|
||||||
AppInput::Sort => self.sheets_listing.emit(SheetListingInput::Sort),
|
AppInput::Sort => self.sheets_listing.emit(SheetListingInput::Sort),
|
||||||
AppInput::Shuffle => self.sheets_listing.emit(SheetListingInput::Shuffle),
|
AppInput::Shuffle => self.sheets_listing.emit(SheetListingInput::Shuffle),
|
||||||
AppInput::SetEditMode(edit_mode) => self.edit_mode = edit_mode,
|
AppInput::SetEditMode(edit_mode) => self.edit_mode = edit_mode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update_cmd(
|
||||||
|
&mut self,
|
||||||
|
message: Self::CommandOutput,
|
||||||
|
_sender: AsyncComponentSender<Self>,
|
||||||
|
_: &Self::Root,
|
||||||
|
) {
|
||||||
|
let mut sheets = message;
|
||||||
|
sheets.sort_by(|a, b| a.cmp(b).reverse());
|
||||||
|
|
||||||
|
self.sheets_listing
|
||||||
|
.emit(SheetListingInput::ReloadSheets(sheets));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
|
|
||||||
use relm4::factory::FactoryVecDeque;
|
use relm4::factory::FactoryVecDeque;
|
||||||
@ -22,6 +20,7 @@ pub enum SheetListingInput {
|
|||||||
ListBoxRowClicked(i32),
|
ListBoxRowClicked(i32),
|
||||||
Sort,
|
Sort,
|
||||||
Shuffle,
|
Shuffle,
|
||||||
|
ReloadSheets(Vec<Sheet>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -82,6 +81,12 @@ impl SimpleComponent for SheetListingModel {
|
|||||||
}
|
}
|
||||||
SheetListingInput::Sort => sort_sheets(&mut self.sheets),
|
SheetListingInput::Sort => sort_sheets(&mut self.sheets),
|
||||||
SheetListingInput::Shuffle => shuffle_sheets(&mut self.sheets),
|
SheetListingInput::Shuffle => shuffle_sheets(&mut self.sheets),
|
||||||
|
SheetListingInput::ReloadSheets(sheets) => {
|
||||||
|
self.sheets.guard().clear();
|
||||||
|
for sheet_model_type in sheets {
|
||||||
|
self.sheets.guard().push_back(sheet_model_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user