Skip to content

Commit

Permalink
Improves saving multiple images / data jsons (#291)
Browse files Browse the repository at this point in the history
* Improves saving multiple images / data jsons

* copy back the old cargo lock
  • Loading branch information
neil-morrison44 authored May 28, 2024
1 parent bc86f73 commit 623b16b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pocket-sync",
"private": true,
"version": "4.9.0",
"version": "4.9.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pocket-sync"
version = "4.9.0"
version = "4.9.1"
description = "A GUI tool for doing stuff with the Analogue Pocket"
authors = ["neil-morrison44"]
license = "AGPL-3.0"
Expand Down
27 changes: 26 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,30 @@ async fn check_root_files(
.map_err(|err| err.to_string())
}

#[tauri::command(async)]
async fn save_multiple_files(
state: tauri::State<'_, PocketSyncState>,
paths: Vec<&str>,
data: Vec<Vec<u8>>,
) -> Result<(), String> {
debug!("Command: save_multiple_files");
let pocket_path = state.0.pocket_path.read().await;

let all_paths: Vec<PathBuf> = paths.iter().map(|p| pocket_path.join(p)).collect();
let common_dir = find_common_path(&all_paths).unwrap();

let arc_lock = state.0.file_locker.find_lock_for(&common_dir).await;
let _write_lock = arc_lock.write().await;

for (file_path, file_data) in all_paths.iter().zip(data) {
tokio::fs::write(file_path, file_data)
.await
.map_err(|err| err.to_string())?;
}

Ok(())
}

fn main() {
tauri::Builder::default()
.plugin(
Expand Down Expand Up @@ -758,7 +782,8 @@ fn main() {
download_firmware,
clear_file_cache,
check_root_files,
find_required_files
find_required_files,
save_multiple_files
])
.setup(|app| {
log_panics::init();
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "Pocket Sync",
"version": "4.9.0"
"version": "4.9.1"
},
"tauri": {
"allowlist": {
Expand Down
16 changes: 9 additions & 7 deletions src/components/platforms/dataPacks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import { PlatformId, ImagePack, PlatformInfoJSON } from "../../../types"
import { Modal } from "../../modal"

import "./index.css"
import { invokeSaveFile } from "../../../utils/invokes"
import { pocketPathAtom } from "../../../recoil/atoms"
import { useTranslation } from "react-i18next"
import { invokeSaveMultipleFiles } from "../../../utils/invokes"

type DataPacksProps = {
platformId?: PlatformId
Expand Down Expand Up @@ -43,20 +42,23 @@ export const DataPacks = ({ onClose, platformId }: DataPacksProps) => {
const apply = useRecoilCallback(
({ snapshot }) =>
async () => {
const pocketPath = await snapshot.getPromise(pocketPathAtom)
const selectionEntries = Object.entries(selections)
const encoder = new TextEncoder()

const paths = []
const jsons = []

for (let index = 0; index < selectionEntries.length; index++) {
const [platformId, pack] = selectionEntries[index]
if (!pack) continue
const packJson = await snapshot.getPromise(
DataPackJsonSelectorFamily({ ...pack, platformId })
)
await invokeSaveFile(
`${pocketPath}/Platforms/${platformId}.json`,
encoder.encode(JSON.stringify(packJson, null, 2))
)
paths.push(`Platforms/${platformId}.json`)
jsons.push(encoder.encode(JSON.stringify(packJson, null, 2)))
}

await invokeSaveMultipleFiles(paths, jsons)
onClose()
},
[selections]
Expand Down
18 changes: 10 additions & 8 deletions src/components/platforms/imagePacks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import {
useRecoilValue,
useRecoilValueLoadable,
} from "recoil"
import { pocketPathAtom } from "../../../recoil/atoms"
import {
ImagePackImageSelectorFamily,
imagePackListSelector,
platformsListSelector,
} from "../../../recoil/platforms/selectors"
import { ImagePack, PlatformId } from "../../../types"
import { invokeSaveFile } from "../../../utils/invokes"
import { PlatformImage } from "../../cores/platformImage"
import { Link } from "../../link"
import { Loader } from "../../loader"
Expand All @@ -21,6 +19,7 @@ import { useTranslation } from "react-i18next"
import "./index.css"
import { PlatformName } from "./platformName"
import { OnlyLoadsWhenShown } from "../../../utils/onlyLoadsWhenShown"
import { invokeSaveMultipleFiles } from "../../../utils/invokes"

type ImagePacksProps = {
onClose: () => void
Expand Down Expand Up @@ -51,21 +50,24 @@ export const ImagePacks = ({ onClose, singlePlatformId }: ImagePacksProps) => {
const applyChanges = useRecoilCallback(
({ snapshot }) =>
async () => {
const pocketPath = await snapshot.getPromise(pocketPathAtom)
if (!pocketPath) return
const paths = []
const images = []

for (const platformId in selections) {
const pack = selections[platformId]
if (!pack) continue
const image = await snapshot.getPromise(
ImagePackImageSelectorFamily({ ...pack, platformId })
)
if (!image) continue
await invokeSaveFile(
`${pocketPath}/Platforms/_images/${platformId}.bin`,
new Uint8Array(await image.file.arrayBuffer())
)

const imageData = new Uint8Array(await image.file.arrayBuffer())

paths.push(`Platforms/_images/${platformId}.bin`)
images.push(imageData)
}

await invokeSaveMultipleFiles(paths, images)
onClose()
},
[selections]
Expand Down
1 change: 0 additions & 1 deletion src/recoil/platforms/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export const DataPackJsonSelectorFamily = selectorFamily<
let parsed: PlatformInfoJSON | null = null

try {
console.log("Parse O")
parsed = JSON.parse(text)
} catch (err) {
console.warn(
Expand Down
9 changes: 9 additions & 0 deletions src/utils/invokes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ export const invokeInstallArchiveFiles = async (
archiveUrl,
turbo,
})

export const invokeSaveMultipleFiles = async (
paths: string[],
data: Uint8Array[]
) =>
await invoke<boolean>("save_multiple_files", {
paths,
data: data.map((d) => Array.from(d)),
})

0 comments on commit 623b16b

Please sign in to comment.