From 73594d1e3cb2764748139b4d0463769d9f199bc4 Mon Sep 17 00:00:00 2001 From: Tormak <63308171+Tormak9970@users.noreply.github.com> Date: Fri, 31 Mar 2023 10:28:31 -0500 Subject: [PATCH] fix: save and discard now function properly --- src-tauri/src/main.rs | 34 ++++++++++++++++++---------- src/lib/controllers/AppController.ts | 9 +++++--- src/types/global.d.ts | 1 + 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e9867706..e4819d18 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -25,6 +25,7 @@ type GridImageCache = HashMap>; struct ChangedPath { appId: String, gridType: String, + oldPath: String, targetPath: String, sourcePath: String } @@ -41,7 +42,7 @@ fn get_grid_filename(appid: &str, grid_type: &str, image_type: &str) -> String { } fn adjust_path(appid: &str, path: &str, grid_type: &str) -> String { - let format_start_index = path.rfind(".").expect("Path should have had a file extension.") + 1; + let format_start_index = path.rfind(".").expect("Path should have had a file extension."); let image_type = &path[format_start_index..]; return get_grid_filename(appid, grid_type, image_type); } @@ -56,19 +57,15 @@ fn filter_paths(app_handle: &AppHandle, current_paths: &GridImageCache, original let grid_path_owned = grid_path.to_owned(); let source_path_owned = source_path.to_owned(); - if appid.as_str().to_owned() == "752590" { - println!("Source: {} Target: {}.", source_path_owned, grid_path_owned); - } - if source_path_owned != grid_path_owned { - // println!("Source: {} Target: {}.", source_path_owned, grid_path_owned); - let adjusted_path = adjust_path(appid.as_str(), source_path_owned.as_str(), grid_type.as_str()); - let target_path = grids_dir.join(adjusted_path); + let adjusted_path = adjust_path(appid.as_str(), source_path_owned.as_str(), grid_type.as_str()).replace("\\", "/"); + let target_path = String::from(grids_dir.join(adjusted_path).to_str().unwrap()).replace("\\", "/"); let changed_path = ChangedPath { appId: appid.to_owned(), gridType: grid_type.to_owned(), - targetPath: String::from(target_path.to_str().unwrap()), - sourcePath: source_path_owned + oldPath: grid_path_owned.replace("\\", "/"), + targetPath: target_path.to_owned(), + sourcePath: source_path_owned.replace("\\", "/") }; res.push(changed_path); @@ -158,12 +155,25 @@ async fn save_changes(app_handle: AppHandle, current_art: String, original_art: for changed_path in (&paths_to_set).into_iter() { let source = changed_path.sourcePath.to_owned(); let target = changed_path.targetPath.to_owned(); - let copy_res = fs::copy(target.clone(), source.clone()); + + if changed_path.oldPath.contains("grid") { + let remove_res = fs::remove_file(changed_path.oldPath.to_owned()); + if remove_res.is_err() { + let err = remove_res.err().unwrap(); + return format!("{{ \"error\": \"{}\"}}", err.to_string()); + } + } + + fs::File::create(target.clone()).unwrap(); + + let copy_res = fs::copy(source.clone(), target.clone()); + if copy_res.is_ok() { logger::log_to_file(app_handle.to_owned(), format!("Copied {} to {}.", source, target).as_str(), 0); } else { logger::log_to_file(app_handle.to_owned(), format!("Failed to copy {} to {}.", source, target).as_str(), 2); - return format!("{{ \"error\": \"Failed to copy {} to {}\"}}", source, target); + let err = copy_res.err().unwrap(); + return format!("{{ \"error\": \"{}\"}}", err.to_string()); } } diff --git a/src/lib/controllers/AppController.ts b/src/lib/controllers/AppController.ts index 0bc178a4..3196c9f7 100644 --- a/src/lib/controllers/AppController.ts +++ b/src/lib/controllers/AppController.ts @@ -156,7 +156,7 @@ export class AppController { const filteredCache = await AppController.getCacheData(); - originalAppLibraryCache.set(filteredCache); + originalAppLibraryCache.set(JSON.parse(JSON.stringify(filteredCache))); appLibraryCache.set(filteredCache); const filteredKeys = Object.keys(filteredCache); @@ -189,7 +189,6 @@ export class AppController { ToastController.showSuccessToast("Changes failed."); LogController.log("Changes failed."); } else { - console.log(changedPaths); for (const changedPath of (changedPaths as ChangedPath[])) { libraryCache[changedPath.appId][changedPath.gridType] = changedPath.targetPath; } @@ -198,6 +197,8 @@ export class AppController { ToastController.showSuccessToast("Changes saved!"); LogController.log("Saved changes."); } + + canSave.set(false); } /** @@ -206,10 +207,12 @@ export class AppController { */ static async discardChanges(): Promise { const originalImgs = get(originalAppLibraryCache); - appLibraryCache.set(originalImgs); + appLibraryCache.set({...originalImgs}); ToastController.showSuccessToast("Changes discarded!"); LogController.log("Discarded changes."); + + canSave.set(false); } diff --git a/src/types/global.d.ts b/src/types/global.d.ts index bf8f1b88..1183819f 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -25,6 +25,7 @@ type LibraryCacheEntry = { type ChangedPath = { appId: string, gridType: string, + oldPath: string, targetPath: string, sourcePath: string } \ No newline at end of file