From d0e76f574b4c3ef8e3ca0d8431304e9c42653733 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Sat, 23 Mar 2024 14:45:53 +1300 Subject: [PATCH] =?UTF-8?q?Enable=20exporting=20plans=20as=20CSV=20-=202.3?= =?UTF-8?q?3=20=F0=9F=9A=80=20This=20is=20to=20help=20migrations=20across?= =?UTF-8?q?=20to=20Flexify.=20Also=20is=20generally=20useful=20if=20someon?= =?UTF-8?q?e=20wants=20to=20do=20some=20Excel=20magic=20on=20their=20plans?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SettingsPage.tsx | 17 ++++++++++- android/app/build.gradle | 4 +-- .../src/main/java/com/massive/BackupModule.kt | 16 ++++++++-- .../main/java/com/massive/DatabaseHelper.kt | 29 +++++++++++++++++-- package.json | 2 +- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/SettingsPage.tsx b/SettingsPage.tsx index 2501508..0d67d99 100644 --- a/SettingsPage.tsx +++ b/SettingsPage.tsx @@ -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() { ), }, + { + name: "Export plans as CSV", + renderItem: (name: string) => ( + + ), + }, { name: "Import database", renderItem: (name: string) => ( diff --git a/android/app/build.gradle b/android/app/build.gradle index fc809f5..39ca774 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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 { diff --git a/android/app/src/main/java/com/massive/BackupModule.kt b/android/app/src/main/java/com/massive/BackupModule.kt index 1424ffc..1bd0722 100644 --- a/android/app/src/main/java/com/massive/BackupModule.kt +++ b/android/app/src/main/java/com/massive/BackupModule.kt @@ -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) { diff --git a/android/app/src/main/java/com/massive/DatabaseHelper.kt b/android/app/src/main/java/com/massive/DatabaseHelper.kt index 3ee2921..34103f3 100644 --- a/android/app/src/main/java/com/massive/DatabaseHelper.kt +++ b/android/app/src/main/java/com/massive/DatabaseHelper.kt @@ -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(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) { } diff --git a/package.json b/package.json index 9814549..8944f71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "massive", - "version": "2.32", + "version": "2.33", "private": true, "license": "GPL-3.0-only", "scripts": {