Enable exporting plans as CSV - 2.33 🚀

This is to help migrations across to Flexify.
Also is generally useful if someone wants
to do some Excel magic on their plans.
This commit is contained in:
Brandon Presley 2024-03-23 14:45:53 +13:00
parent 52f642c2af
commit d0e76f574b
5 changed files with 60 additions and 8 deletions

View File

@ -511,7 +511,7 @@ export default function SettingsPage() {
style={{ alignSelf: "flex-start" }}
onPress={async () => {
const result = await DocumentPicker.pickDirectory();
await NativeModules.BackupModule.exportToCSV(result.uri);
await NativeModules.BackupModule.exportSets(result.uri);
toast("Exported sets as CSV.");
}}
>
@ -519,6 +519,21 @@ export default function SettingsPage() {
</Button>
),
},
{
name: "Export plans as CSV",
renderItem: (name: string) => (
<Button
style={{ alignSelf: "flex-start" }}
onPress={async () => {
const result = await DocumentPicker.pickDirectory();
await NativeModules.BackupModule.exportPlans(result.uri);
toast("Exported plans as CSV.");
}}
>
{name}
</Button>
),
},
{
name: "Import database",
renderItem: (name: string) => (

View File

@ -87,8 +87,8 @@ android {
applicationId "com.massive"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 36247
versionName "2.32"
versionCode 36248
versionName "2.33"
}
signingConfigs {
release {

View File

@ -95,10 +95,22 @@ class BackupModule(context: ReactApplicationContext?) :
}
@ReactMethod
fun exportToCSV(target: String, promise: Promise) {
fun exportPlans(target: String, promise: Promise) {
try {
val db = DatabaseHelper(reactApplicationContext)
db.exportToCSV(target, reactApplicationContext)
db.exportPlans(target, reactApplicationContext)
promise.resolve("Export successful!")
}
catch (e: Exception) {
promise.reject("ERROR", e)
}
}
@ReactMethod
fun exportSets(target: String, promise: Promise) {
try {
val db = DatabaseHelper(reactApplicationContext)
db.exportSets(target, reactApplicationContext)
promise.resolve("Export successful!")
}
catch (e: Exception) {

View File

@ -18,8 +18,8 @@ class DatabaseHelper(context: Context) :
private const val DATABASE_VERSION = 1
}
fun exportToCSV(target: String, context: Context) {
Log.d("DatabaseHelper", "exportToCSV $target")
fun exportSets(target: String, context: Context) {
Log.d("DatabaseHelper", "exportSets $target")
val treeUri: Uri = Uri.parse(target)
val documentFile = context.let { DocumentFile.fromTreeUri(it, treeUri) }
val file = documentFile?.createFile("application/octet-stream", "sets.csv") ?: return
@ -43,6 +43,31 @@ class DatabaseHelper(context: Context) :
}
}
fun exportPlans(target: String, context: Context) {
Log.d("DatabaseHelper", "exportPlans $target")
val treeUri: Uri = Uri.parse(target)
val documentFile = context.let { DocumentFile.fromTreeUri(it, treeUri) }
val file = documentFile?.createFile("application/octet-stream", "plans.csv") ?: return
context.contentResolver.openOutputStream(file.uri).use { outputStream ->
val csvWrite = CSVWriter(outputStream?.writer())
val db = this.readableDatabase
val cursor = db.rawQuery("SELECT * FROM plans", null)
csvWrite.writeNext(cursor.columnNames)
while(cursor.moveToNext()) {
val arrStr = arrayOfNulls<String>(cursor.columnCount)
for(i in 0 until cursor.columnCount) {
arrStr[i] = cursor.getString(i)
}
csvWrite.writeNext(arrStr)
}
csvWrite.close()
cursor.close()
}
}
override fun onCreate(db: SQLiteDatabase) {
}

View File

@ -1,6 +1,6 @@
{
"name": "massive",
"version": "2.32",
"version": "2.33",
"private": true,
"license": "GPL-3.0-only",
"scripts": {