From 71f31ad1569290154e63982cb485f20aa97f2ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=B5=E5=91=B5=E5=B0=8F=E9=B1=BC?= Date: Wed, 22 Jul 2020 20:04:41 +0800 Subject: [PATCH] Fix bugs --- README-EN.md | 2 +- README.md | 2 +- app-kt/src/main/AndroidManifest.xml | 3 +- .../main/java/com/liyu/demokt/MainActivity.kt | 34 +++++++++++++++++- app-kt/src/main/res/layout/activity_main.xml | 3 +- build.gradle | 2 +- .../liyu/sqlitetoexcel/kt/SQLiteToExcel.kt | 35 +++++++++++-------- sqlitetoexcel/build.gradle | 10 +++--- .../com/liyu/sqlitetoexcel/SQLiteToExcel.java | 21 ++++++----- 9 files changed, 77 insertions(+), 35 deletions(-) diff --git a/README-EN.md b/README-EN.md index 0ce2ba0..64c908a 100644 --- a/README-EN.md +++ b/README-EN.md @@ -16,7 +16,7 @@ From v1.0.8, support custom SQL query. ## How to use #### 1. Add Gradle dependencies ``` Gradle -compile 'com.liyu.tools:sqlitetoexcel:1.0.9' +compile 'com.liyu.tools:sqlitetoexcel:1.0.10' ``` #### 2. SQLite -> Excel [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java) diff --git a/README.md b/README.md index 14a174d..be1ada3 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ v1.0.8 版本开始支持自定义 SQL 查询导出了。 ## 如何使用 #### 1. 添加 Gradle 依赖 ``` Gradle -implementation 'com.liyu.tools:sqlitetoexcel:1.0.9' +implementation 'com.liyu.tools:sqlitetoexcel:1.0.10' ``` #### 2. SQLite -> Excel [demo](https://github.com/li-yu/SQLiteToExcel/blob/master/app/src/main/java/com/liyu/demo/MainActivity.java) diff --git a/app-kt/src/main/AndroidManifest.xml b/app-kt/src/main/AndroidManifest.xml index 508c04b..a777106 100644 --- a/app-kt/src/main/AndroidManifest.xml +++ b/app-kt/src/main/AndroidManifest.xml @@ -1,7 +1,8 @@ - + + - = arrayOf("") @@ -16,31 +16,36 @@ class SQLiteToExcel(val context: Context) { private var onError: ((Throwable) -> Unit)? = null private var onStart: (() -> Unit)? = null - fun start(block: SQLiteToExcel.() -> Unit) { + fun start(block: SQLiteToExcel.() -> Unit): String? { block() - com.liyu.sqlitetoexcel.SQLiteToExcel.Builder(context) + val sqLiteToExcel = com.liyu.sqlitetoexcel.SQLiteToExcel.Builder(context) .setDataBase(databasePath) .setTables(*tables) .setOutputPath(outputPath) .setOutputFileName(outputFileName) .setEncryptKey(encryptKey) .setProtectKey(protectKey) - .start(object : com.liyu.sqlitetoexcel.SQLiteToExcel.ExportListener { - override fun onStart() { - onStartInner() - } + .build() + if (onCompleted == null && onError == null && onStart == null) { + return sqLiteToExcel.start() + } else { + sqLiteToExcel.start(object : com.liyu.sqlitetoexcel.SQLiteToExcel.ExportListener { + override fun onStart() { + onStartInner() + } - override fun onCompleted(filePath: String) { - onCompletedInner(filePath) - } + override fun onCompleted(filePath: String) { + onCompletedInner(filePath) + } - override fun onError(e: Exception) { - onErrorInner(e) - } - }) + override fun onError(e: Exception) { + onErrorInner(e) + } + }) + return null + } } - fun onCompleted(block: (String) -> Unit) { onCompleted = block } diff --git a/sqlitetoexcel/build.gradle b/sqlitetoexcel/build.gradle index 2201ceb..f975c60 100644 --- a/sqlitetoexcel/build.gradle +++ b/sqlitetoexcel/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 27 - versionCode 9 - versionName "1.0.9" + versionCode 10 + versionName "1.0.10" } buildTypes { release { @@ -28,7 +28,7 @@ dependencies { def siteUrl = 'https://github.com/li-yu/SQLiteToExcel' def gitUrl = 'https://github.com/li-yu/SQLiteToExcel.git' -version = "1.0.9" +version = "1.0.10" group = "com.liyu.tools" Properties properties = new Properties() @@ -45,9 +45,9 @@ bintray { licenses = ["Apache-2.0"] publish = true version { - name = '1.0.9' + name = '1.0.10' released = new Date() - vcsTag = 'v1.0.9' + vcsTag = 'v1.0.10' attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin'] desc = 'A simple lib for Android to export SQLite to Excel.' } diff --git a/sqlitetoexcel/src/main/java/com/liyu/sqlitetoexcel/SQLiteToExcel.java b/sqlitetoexcel/src/main/java/com/liyu/sqlitetoexcel/SQLiteToExcel.java index 5eab243..696e6c0 100644 --- a/sqlitetoexcel/src/main/java/com/liyu/sqlitetoexcel/SQLiteToExcel.java +++ b/sqlitetoexcel/src/main/java/com/liyu/sqlitetoexcel/SQLiteToExcel.java @@ -52,24 +52,27 @@ public static class Builder { private List tables; private String sql; private String sheetName; + private Context context; public Builder(Context context) { - this.filePath = context.getExternalFilesDir(null).getPath(); + this.context = context; } public SQLiteToExcel build() { if (TextUtils.isEmpty(dataBaseName)) { throw new IllegalArgumentException("Database name must not be null."); } + if (TextUtils.isEmpty(filePath)) { + this.filePath = context.getExternalFilesDir(null).getPath(); + } if (TextUtils.isEmpty(fileName)) { - throw new IllegalArgumentException("Output file name must not be null."); + this.fileName = new File(dataBaseName).getName() + ".xls"; } return new SQLiteToExcel(tables, protectKey, encryptKey, fileName, dataBaseName, filePath, sql, sheetName); } public Builder setDataBase(String dataBaseName) { this.dataBaseName = dataBaseName; - this.fileName = new File(dataBaseName).getName() + ".xls"; return this; } @@ -128,7 +131,7 @@ public Builder setSQL(@NonNull String sql) { return setSQL("Sheet1", sql); } - public String start() { + public String start() throws Exception { final SQLiteToExcel sqliteToExcel = build(); return sqliteToExcel.start(); } @@ -144,7 +147,7 @@ public void start(ExportListener listener) { * * @return output file path */ - public String start() { + public String start() throws Exception { try { if (tables == null || tables.size() == 0) { tables = getTablesName(database); @@ -154,7 +157,7 @@ public String start() { if (database != null && database.isOpen()) { database.close(); } - return null; + throw e; } } @@ -175,12 +178,12 @@ public void run() { if (tables == null || tables.size() == 0) { tables = getTablesName(database); } - final String filePath = exportTables(tables, fileName); + final String finalFilePath = exportTables(tables, fileName); if (listener != null) { handler.post(new Runnable() { @Override public void run() { - listener.onCompleted(filePath); + listener.onCompleted(finalFilePath); } }); } @@ -208,7 +211,7 @@ private SQLiteToExcel(List tables, String protectKey, String encryptKey, this.filePath = filePath; this.sql = sql; this.sheetName = sheetName; - + this.tables = tables; try { database = SQLiteDatabase.openOrCreateDatabase(dataBaseName, null); } catch (Exception e) {