diff --git a/src/lib/storage/backends.ts b/src/lib/storage/backends.ts index 8a544c1..6296181 100644 --- a/src/lib/storage/backends.ts +++ b/src/lib/storage/backends.ts @@ -32,41 +32,52 @@ export const purgeStorage = async (store: string) => { export const createMMKVBackend = (store: string) => { const mmkvPath = getMMKVPath(store); return createFileBackend(mmkvPath, (async () => { + const path = `${FileManager.getConstants().DocumentsDirPath}/${mmkvPath}`; + if (await FileManager.fileExists(path)) return; + + let oldData = await MMKVManager.getItem(store) ?? "{}"; + + // From the testing on Android, it seems to return this if the data is too large + if (oldData === "!!LARGE_VALUE!!") { + const cachePath = `${FileManager.getConstants().CacheDirPath}/mmkv/${store}`; + if (await FileManager.fileExists(cachePath)) { + oldData = await FileManager.readFile(cachePath, "utf8") + } else { + console.log(`${store}: Experienced data loss :(`); + oldData = "{}"; + } + } + try { - const path = `${FileManager.getConstants().DocumentsDirPath}/${mmkvPath}`; - if (await FileManager.fileExists(path)) return; + JSON.parse(oldData); + } catch { + console.error(`${store} had an unparseable data while migrating`); + oldData = "{}"; + } - let oldData = await MMKVManager.getItem(store) ?? "{}"; - - // From the testing on Android, it seems to return this if the data is too large - if (oldData === "!!LARGE_VALUE!!") { - const cachePath = `${FileManager.getConstants().CacheDirPath}/mmkv/${store}`; - if (await FileManager.fileExists(cachePath)) { - oldData = await FileManager.readFile(cachePath, "utf8") - } else { - console.log(`${store}: Experienced data loss :(`); - oldData = "{}"; - } - } - - await FileManager.writeFile("documents", filePathFixer(mmkvPath), oldData, "utf8"); - if (await MMKVManager.getItem(store) !== null) { - MMKVManager.removeItem(store); - console.log(`Successfully migrated ${store} store from MMKV storage to fs`); - } - } catch (err) { - console.error("Failed to migrate to fs from MMKVManager ", err) + await FileManager.writeFile("documents", filePathFixer(mmkvPath), oldData, "utf8"); + if (await MMKVManager.getItem(store) !== null) { + MMKVManager.removeItem(store); + console.log(`Successfully migrated ${store} store from MMKV storage to fs`); } })()); } export const createFileBackend = (file: string, migratePromise?: Promise): StorageBackend => { - let created: boolean; return { get: async () => { await migratePromise; const path = `${FileManager.getConstants().DocumentsDirPath}/${file}`; - if (!created && !(await FileManager.fileExists(path))) return (created = true), FileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"); + if (await FileManager.fileExists(path)) { + const content = await FileManager.readFile(path, "utf8"); + try { + return JSON.parse(content); + } catch { + // Corrupted content, ignore + } + } + + await FileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"); return JSON.parse(await FileManager.readFile(path, "utf8")); }, set: async (data) => { diff --git a/src/ui/toasts.ts b/src/ui/toasts.ts index 72bf588..4115eea 100644 --- a/src/ui/toasts.ts +++ b/src/ui/toasts.ts @@ -13,9 +13,9 @@ export const showToast = (content: string, asset?: number) => toasts.open({ icon: asset, }); -showToast.showCopyToClipboard = () => { +showToast.showCopyToClipboard = (message = "Copied plugin URL to clipboard.") => { // On Android, only show toast for 12 and lower if (ReactNative.Platform.OS !== "android" || ReactNative.Platform.Version <= 32) { - showToast("Copied plugin URL to clipboard.", getAssetIDByName("toast_copy_link")); + showToast(message, getAssetIDByName("toast_copy_link")); } } \ No newline at end of file