Add reload button functionality

This commit is contained in:
2024-03-02 18:56:02 +01:00
parent 3f5e621b6b
commit 7b931fb033
4 changed files with 158 additions and 105 deletions

View File

@@ -1,20 +1,16 @@
mod database;
mod sheet;
mod sheet_dao;
mod sheet_validation;
mod ui;
use std::{
path::{Path, PathBuf},
process,
};
use std::{path::PathBuf, process};
use clap::Parser;
use database::Database;
use env_logger::Env;
use log::{debug, error};
use log::error;
use relm4::RelmApp;
use sheet::{Pdf, Sheet};
use walkdir::WalkDir;
use crate::ui::app::{AppInitData, AppModel};
@@ -36,96 +32,16 @@ async fn main() {
let database = Database::setup(cli.directory.join("database.sqlite"))
.await
.unwrap();
let sheets = sheet_dao::fetch_all_sheets(&database).await.unwrap();
debug!("Validating sheets from database...");
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 sheets = sheet_validation::load_and_validate_sheets(&database, &cli.directory).await;
let app_init_data = AppInitData {
sheets,
database,
directory: cli.directory,
};
let app = RelmApp::new("de.frajul.sheet-organizer");
// Pass empty command line args to allow my own parsing
app.with_args(Vec::new())
.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)
})
}