Skip to content

Commit

Permalink
Android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Mar 16, 2024
1 parent 72dbdd0 commit 6887a81
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions android/src/main/java/com/op/sqlite/OPSQLiteModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?) :
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 6887a81

Please sign in to comment.