Skip to content

Commit

Permalink
fix: force reload on the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
obeim committed Jan 26, 2024
1 parent 602664b commit 129812b
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 65 deletions.
5 changes: 3 additions & 2 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Slot, SplashScreen } from "expo-router";
import { SafeAreaView, StatusBar, StyleSheet, I18nManager } from "react-native";
import { useFonts } from "expo-font";
import { openDatabase } from "@/db/utils";
import mydb from "@/assets/quran.db";
import * as Updates from "expo-updates";

SplashScreen.preventAutoHideAsync();

Expand All @@ -17,10 +17,11 @@ export default function RootLayout(): ReactNode {

I18nManager.allowRTL(true);
I18nManager.forceRTL(true);
if (!I18nManager.isRTL) Updates.reloadAsync();
openDatabase();

const onLayoutRootView = useCallback(async () => {
await SplashScreen.hideAsync();
openDatabase(mydb);
}, [fontsLoaded, fontError]);

if (!fontsLoaded && !fontError) {
Expand Down
40 changes: 25 additions & 15 deletions db/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@ import * as FileSystem from "expo-file-system";
import * as SQLite from "expo-sqlite";
import { Asset } from "expo-asset";

async function openDatabase(
pathToDatabaseFile: string
): Promise<SQLite.SQLiteDatabase> {
if (
!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + "SQLite"))
.exists
) {
await FileSystem.makeDirectoryAsync(
FileSystem.documentDirectory + "SQLite"
);
async function openDatabase() {
const localFolder = FileSystem.documentDirectory + "SQLite";
const dbName = "quran.db";
const localURI = localFolder + "/" + dbName;

if (!(await FileSystem.getInfoAsync(localFolder)).exists) {
await FileSystem.makeDirectoryAsync(localFolder);
}

await FileSystem.downloadAsync(
Asset.fromModule(pathToDatabaseFile).uri,
FileSystem.documentDirectory + "SQLite/quran.db"
);
let asset = Asset.fromModule(require("../assets/quran.db"));

if (!asset.downloaded) {
await asset.downloadAsync().then((value) => {
asset = value;
console.log("asset downloadAsync - finished");
});

let remoteURI = asset.localUri;

await FileSystem.copyAsync({
from: remoteURI as string,
to: localURI,
}).catch((error) => {
console.log("asset copyDatabase - finished with error: " + error);
});
}

return SQLite.openDatabase("quran.db");
return SQLite.openDatabase(dbName);
}

export { openDatabase };
227 changes: 227 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"react-native-web": "~0.19.6",
"expo-file-system": "~16.0.5",
"expo-asset": "~9.0.2",
"expo-sqlite": "11.6.0"
"expo-sqlite": "11.6.0",
"expo-updates": "~0.24.8"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
Loading

0 comments on commit 129812b

Please sign in to comment.