From b7bbbbe96249a49f9ac2b60d102c8687f0b9fca0 Mon Sep 17 00:00:00 2001 From: nyagami Date: Sat, 8 Jun 2024 01:42:47 +0700 Subject: [PATCH] Fix: revert to app storage --- android/app/src/main/AndroidManifest.xml | 4 + .../FileManager/FileManager.kt | 6 +- src/database/queries/ChapterQueries.ts | 4 +- src/database/queries/NovelQueries.ts | 4 +- src/hooks/persisted/useNovel.ts | 3 +- src/navigators/Main.tsx | 16 +- src/plugins/pluginManager.ts | 16 +- src/screens/onboarding/OnboardingScreen.tsx | 52 ++----- src/screens/onboarding/StorageStep.tsx | 146 ------------------ src/screens/reader/ReaderScreen.tsx | 3 +- src/services/backup/drive/index.ts | 4 +- src/services/backup/remote/index.ts | 4 +- src/services/backup/utils.ts | 12 +- src/services/epub/import.ts | 5 +- src/services/updates/LibraryUpdateQueries.ts | 3 +- src/utils/Storages.ts | 40 +---- 16 files changed, 48 insertions(+), 274 deletions(-) delete mode 100644 src/screens/onboarding/StorageStep.tsx diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index f11d3f8b..04080be3 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,10 @@ + + + + diff --git a/android/app/src/main/java/com/rajarsheechatterjee/FileManager/FileManager.kt b/android/app/src/main/java/com/rajarsheechatterjee/FileManager/FileManager.kt index e03da03c..b24c32e7 100644 --- a/android/app/src/main/java/com/rajarsheechatterjee/FileManager/FileManager.kt +++ b/android/app/src/main/java/com/rajarsheechatterjee/FileManager/FileManager.kt @@ -179,8 +179,10 @@ class FileManager(context: ReactApplicationContext) : fun mkdir(filepath: String, promise: Promise) { try { val file = File(filepath) - val created = file.mkdirs() - if (!created) throw Exception("Directory could not be created") + if(!file.exists()){ + val created = file.mkdirs() + if (!created) throw Exception("Directory could not be created") + } promise.resolve(null) } catch (e: Exception) { promise.reject(e) diff --git a/src/database/queries/ChapterQueries.ts b/src/database/queries/ChapterQueries.ts index 88bbdd46..2c701755 100644 --- a/src/database/queries/ChapterQueries.ts +++ b/src/database/queries/ChapterQueries.ts @@ -11,7 +11,7 @@ import { Update } from '../types'; import { noop } from 'lodash-es'; import { getString } from '@strings/translations'; import FileManager from '@native/FileManager'; -import { getAppStorages } from '@utils/Storages'; +import { NOVEL_STORAGE } from '@utils/Storages'; const db = SQLite.openDatabase('lnreader.db'); const insertChapterQuery = ` @@ -247,7 +247,6 @@ const downloadFiles = async ( chapterId: number, ): Promise => { try { - const { NOVEL_STORAGE } = getAppStorages(); const folder = await createChapterFolder(NOVEL_STORAGE, { pluginId: plugin.id, novelId, @@ -307,7 +306,6 @@ const deleteDownloadedFiles = async ( chapterId: number, ) => { try { - const { NOVEL_STORAGE } = getAppStorages(); const path = await createChapterFolder(NOVEL_STORAGE, { pluginId, novelId, diff --git a/src/database/queries/NovelQueries.ts b/src/database/queries/NovelQueries.ts index 5dd283cb..00dbda8c 100644 --- a/src/database/queries/NovelQueries.ts +++ b/src/database/queries/NovelQueries.ts @@ -12,7 +12,7 @@ import { noop } from 'lodash-es'; import { getString } from '@strings/translations'; import { BackupNovel, NovelInfo } from '../types'; import { SourceNovel } from '@plugins/types'; -import { getAppStorages } from '@utils/Storages'; +import { NOVEL_STORAGE } from '@utils/Storages'; import FileManager from '@native/FileManager'; export const insertNovelAndChapters = async ( @@ -45,7 +45,6 @@ export const insertNovelAndChapters = async ( if (novelId) { const promises = [insertChapters(novelId, sourceNovel.chapters)]; if (sourceNovel.cover) { - const { NOVEL_STORAGE } = getAppStorages(); const novelDir = NOVEL_STORAGE + '/' + pluginId + '/' + novelId; await FileManager.mkdir(novelDir); const novelCoverUri = 'file://' + novelDir + '/cover.png'; @@ -271,7 +270,6 @@ export const updateNovelInfo = async (info: NovelInfo) => { export const pickCustomNovelCover = async (novel: NovelInfo) => { const image = await DocumentPicker.getDocumentAsync({ type: 'image/*' }); if (image.assets && image.assets[0]) { - const { NOVEL_STORAGE } = getAppStorages(); const novelDir = NOVEL_STORAGE + '/' + novel.pluginId + '/' + novel.id; let novelCoverUri = 'file://' + novelDir + '/cover.png'; if (!(await FileManager.exists(novelDir))) { diff --git a/src/hooks/persisted/useNovel.ts b/src/hooks/persisted/useNovel.ts index 324fbac9..dbb13b35 100644 --- a/src/hooks/persisted/useNovel.ts +++ b/src/hooks/persisted/useNovel.ts @@ -28,7 +28,7 @@ import { useCallback, useEffect, useState } from 'react'; import { getString } from '@strings/translations'; import dayjs from 'dayjs'; import { parseChapterNumber } from '@utils/parseChapterNumber'; -import { getAppStorages } from '@utils/Storages'; +import { NOVEL_STORAGE } from '@utils/Storages'; import FileManager from '@native/FileManager'; // store key: '__', @@ -417,7 +417,6 @@ export const deleteCachedNovels = async () => { `${NOVEL_SETTINSG_PREFIX}_${novel.pluginId}_${novel.path}`, ); MMKVStorage.delete(`${LAST_READ_PREFIX}_${novel.pluginId}_${novel.path}`); - const { NOVEL_STORAGE } = getAppStorages(); const novelDir = NOVEL_STORAGE + '/' + novel.pluginId + '/' + novel.id; if (await FileManager.exists(novelDir)) { await FileManager.unlink(novelDir); diff --git a/src/navigators/Main.tsx b/src/navigators/Main.tsx index 7d58f936..eb1a3952 100644 --- a/src/navigators/Main.tsx +++ b/src/navigators/Main.tsx @@ -35,17 +35,15 @@ import { updateLibrary } from '@services/updates'; import WebviewScreen from '@screens/WebviewScreen/WebviewScreen'; import { RootStackParamList } from './types'; import Color from 'color'; -import OnboardingScreen from '../screens/onboarding/OnboardingScreen'; -import { APP_STORAGE_KEY } from '@utils/Storages'; -import { useMMKVString } from 'react-native-mmkv'; - +import { useMMKVBoolean } from 'react-native-mmkv'; +import OnboardingScreen from '@screens/onboarding/OnboardingScreen'; const Stack = createStackNavigator(); const MainNavigator = () => { const theme = useTheme(); - const [ROOT_STORAGE] = useMMKVString(APP_STORAGE_KEY); const { updateLibraryOnLaunch } = useAppSettings(); const { refreshPlugins } = usePlugins(); + const [isOnboarded] = useMMKVBoolean('IS_ONBOARDED'); useEffect(() => { const timer = setTimeout(async () => { @@ -62,16 +60,18 @@ const MainNavigator = () => { if (updateLibraryOnLaunch) { updateLibrary(); } - if (ROOT_STORAGE) { + if (isOnboarded) { // hack this helps app has enough time to initialize database; refreshPlugins(); } - }, [ROOT_STORAGE]); + }, [isOnboarded]); const { isNewVersion, latestRelease } = useGithubUpdateChecker(); - if (!ROOT_STORAGE) { + + if (!isOnboarded) { return ; } + return ( {isNewVersion && } diff --git a/src/plugins/pluginManager.ts b/src/plugins/pluginManager.ts index bfdee4dc..a51fd68c 100644 --- a/src/plugins/pluginManager.ts +++ b/src/plugins/pluginManager.ts @@ -17,12 +17,9 @@ import { Parser } from 'htmlparser2'; import FileManager from '@native/FileManager'; import { getRepositoriesFromDb } from '@database/queries/RepositoryQueries'; import { showToast } from '@utils/showToast'; -import { getAppStorages } from '@utils/Storages'; +import { PLUGIN_STORAGE } from '@utils/Storages'; -const _getPluginsFilePath = () => { - const { PLUGIN_STORAGE } = getAppStorages(); - return PLUGIN_STORAGE + '/plugins.json'; -}; +const pluginFilePath = PLUGIN_STORAGE + '/plugins.json'; const packages: Record = { 'htmlparser2': { Parser }, @@ -72,8 +69,8 @@ const serializePlugin = async ( installed: boolean, ) => { if (!serializedPlugins) { - if (await FileManager.exists(_getPluginsFilePath())) { - const content = await FileManager.readFile(_getPluginsFilePath()); + if (await FileManager.exists(pluginFilePath)) { + const content = await FileManager.readFile(pluginFilePath); serializedPlugins = JSON.parse(content); } else { serializedPlugins = {}; @@ -84,18 +81,17 @@ const serializePlugin = async ( } else { serializedPlugins[pluginId] = undefined; } - const { PLUGIN_STORAGE } = getAppStorages(); if (!(await FileManager.exists(PLUGIN_STORAGE))) { await FileManager.mkdir(PLUGIN_STORAGE); } await FileManager.writeFile( - _getPluginsFilePath(), + pluginFilePath, JSON.stringify(serializedPlugins), ); }; const deserializePlugins = () => { - return FileManager.readFile(_getPluginsFilePath()) + return FileManager.readFile(pluginFilePath) .then(content => { serializedPlugins = JSON.parse(content); Object.entries(serializedPlugins).forEach(([pluginId, script]) => { diff --git a/src/screens/onboarding/OnboardingScreen.tsx b/src/screens/onboarding/OnboardingScreen.tsx index f0a3f80c..96c95761 100644 --- a/src/screens/onboarding/OnboardingScreen.tsx +++ b/src/screens/onboarding/OnboardingScreen.tsx @@ -5,30 +5,19 @@ import { Image, StyleSheet, View } from 'react-native'; import { Button } from '@components'; import PickThemeStep from './PickThemeStep'; import { useState } from 'react'; -import StorageStep from './StorageStep'; -import { setAppStorage } from '@utils/Storages'; -import { useBackHandler } from '@hooks/index'; +import { MMKVStorage } from '@utils/mmkv/mmkv'; enum OnboardingStep { PICK_THEME, - STORAGE_LOCATION, } export default function OnboardingScreen() { const theme = useTheme(); - const [rootStorage, setRootStorage] = useState(''); - const [step, setStep] = useState(OnboardingStep.PICK_THEME); + const [step] = useState(OnboardingStep.PICK_THEME); const renderStep = () => { switch (step) { case OnboardingStep.PICK_THEME: return ; - case OnboardingStep.STORAGE_LOCATION: - return ( - - ); default: return ; } @@ -37,21 +26,11 @@ export default function OnboardingScreen() { switch (step) { case OnboardingStep.PICK_THEME: return 'Pick a theme'; - case OnboardingStep.STORAGE_LOCATION: - return 'Select storage for LNReader'; default: return ; } }; - useBackHandler(() => { - if (step === OnboardingStep.STORAGE_LOCATION) { - setStep(OnboardingStep.PICK_THEME); - return true; - } - return false; - }); - return ( - {step !== OnboardingStep.STORAGE_LOCATION ? ( - - - - - - Select an empty folder. - - {rootStorage ? ( - - Selected folder: {rootStorage} - - ) : null} - - - -