diff --git a/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt b/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt index 30d31f0c..91ea3b03 100644 --- a/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt +++ b/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt @@ -3,6 +3,10 @@ package com.op.sqlite import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactMethod import com.facebook.react.module.annotations.ReactModule; +import java.io.File +import java.io.FileOutputStream +import java.io.InputStream +import java.io.OutputStream @ReactModule(name = OPSQLiteModule.NAME) internal class OPSQLiteModule(context: ReactApplicationContext?) : @@ -38,6 +42,49 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : } } + @ReactMethod(isBlockingSynchronousMethod = true) + override fun moveAssetsDatabase(name: String, extension: String): Boolean { + val context = reactApplicationContext + val assetsManager = context.assets + + try { + +// val assets = assetsManager.list(""); + // Open the input stream for the asset file + val inputStream: InputStream = assetsManager.open("custom/$name.$extension") + + // Create the output file in the documents directory + val databasesFolder = context + .getDatabasePath("defaultDatabase") + .absolutePath + .replace("defaultDatabase", "") + + val outputFile = File(databasesFolder, "$name.$extension") + + if (outputFile.exists()) { + return true + } + + // Open the output stream for the output file + val outputStream: OutputStream = FileOutputStream(outputFile) + + // Copy the contents from the input stream to the output stream + val buffer = ByteArray(1024) + var length: Int + while (inputStream.read(buffer).also { length = it } > 0) { + outputStream.write(buffer, 0, length) + } + + // Close the streams + inputStream.close() + outputStream.close() + + return true + } catch (exception: Exception) { + return false + } + } + override fun onCatalystInstanceDestroy() { OPSQLiteBridge.instance.clearState() } diff --git a/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java b/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java index b0dbf4bb..dc445573 100644 --- a/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java +++ b/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java @@ -70,4 +70,8 @@ public NativeOPSQLiteSpec(ReactApplicationContext reactContext) { @ReactMethod(isBlockingSynchronousMethod = true) @DoNotStrip public abstract boolean install(); + + @ReactMethod(isBlockingSynchronousMethod = true) + @DoNotStrip + public abstract boolean moveAssetsDatabase(String name, String extension); } diff --git a/example/src/App.tsx b/example/src/App.tsx index 9b288dd5..1dad4035 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -96,6 +96,9 @@ export default function App() { const openAssetsDb = async () => { const moved = moveAssetsDatabase('sample', 'sqlite'); console.log('moved', moved); + const db = open({name: 'sample.sqlite'}); + const users = db.execute('SELECT * FROM User'); + console.log('users', users.rows?._array); }; const allTestsPassed = results.reduce((acc: boolean, r: any) => {