Open annotated versions if they exist

This commit is contained in:
Julian Mutter 2024-06-27 22:00:28 +02:00
parent 182675d14e
commit d7379a2a9f
3 changed files with 25 additions and 4 deletions

View File

@ -1,10 +1,12 @@
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
ffi::OsStr,
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use log::debug;
use strum_macros::{EnumDiscriminants, EnumIter}; use strum_macros::{EnumDiscriminants, EnumIter};
pub trait PdfSheet { pub trait PdfSheet {
@ -49,10 +51,19 @@ impl Ord for Sheet {
} }
impl Sheet { impl Sheet {
pub fn open_file(&self) { pub fn open_file_or_annotated_version_if_exists(&self) {
let path = &self.pdf.path; let path = &self.pdf.path;
// TODO: open on first_page
opener::open(path).unwrap(); let mut annotated_path = path.with_extension("").into_os_string();
annotated_path.push("_annotated.pdf");
let annotated_version = Path::new(&annotated_path);
if annotated_version.exists() {
// TODO: open on first_page
opener::open(annotated_version).unwrap();
} else {
// TODO: open on first_page
opener::open(path).unwrap();
}
} }
pub fn is_part_of_book(&self) -> bool { pub fn is_part_of_book(&self) -> bool {

View File

@ -64,6 +64,16 @@ fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValida
// TODO: improve performance? // TODO: improve performance?
for pdf_file in find_all_pdfs_in_directory_recursive(dir) { for pdf_file in find_all_pdfs_in_directory_recursive(dir) {
// Make sure annotated files are not handled (they are then only opened if existent)
if pdf_file
.file_name()
.unwrap()
.to_string_lossy()
.ends_with("_annotated.pdf")
{
continue;
}
if let Some((i, _)) = invalidated_sheets if let Some((i, _)) = invalidated_sheets
.iter() .iter()
.enumerate() .enumerate()

View File

@ -175,7 +175,7 @@ impl AsyncComponent for AppModel {
.forward(sender.input_sender(), |_| todo!()), .forward(sender.input_sender(), |_| todo!()),
); );
} else { } else {
sheet.open_file(); sheet.open_file_or_annotated_version_if_exists();
let mut sheet = sheet; let mut sheet = sheet;
sheet.last_opened = I64DateTime(Utc::now()); sheet.last_opened = I64DateTime(Utc::now());
sheet_dao::update_sheet_last_opened(&self.database, &sheet) sheet_dao::update_sheet_last_opened(&self.database, &sheet)