From 66521415c283fd7b51512f34d4a3bcd8ae1b7774 Mon Sep 17 00:00:00 2001
From: Levi Bostian
Date: Mon, 11 Jun 2018 16:58:59 -0500
Subject: [PATCH 1/3] Fix crash when deleting database. Add sync .clear()
function for clearing data to close
https://github.com/levibostian/Wendy-Android/issues/44
---
.../levibostian/wendyexample/MainActivity.kt | 13 +++-
.../wendy/db/PendingTasksManager.kt | 2 +-
.../com/levibostian/wendy/service/Wendy.kt | 71 ++++++++++++-------
3 files changed, 58 insertions(+), 28 deletions(-)
diff --git a/app/src/main/java/com/levibostian/wendyexample/MainActivity.kt b/app/src/main/java/com/levibostian/wendyexample/MainActivity.kt
index 0c5f0e3..afe9dc5 100644
--- a/app/src/main/java/com/levibostian/wendyexample/MainActivity.kt
+++ b/app/src/main/java/com/levibostian/wendyexample/MainActivity.kt
@@ -16,6 +16,7 @@ import com.levibostian.wendy.service.Wendy
import com.levibostian.wendy.types.ReasonPendingTaskSkipped
import com.levibostian.wendyexample.extension.closeKeyboard
import kotlinx.android.synthetic.main.activity_main.*
+import java.util.*
class MainActivity : AppCompatActivity(), TaskRunnerListener {
@@ -48,9 +49,17 @@ class MainActivity : AppCompatActivity(), TaskRunnerListener {
}
activity_main_clear_all_data_button.setOnClickListener {
Toast.makeText(this, "Clearing data...", Toast.LENGTH_SHORT).show()
- Wendy.shared.clear {
+
+ // This way we can test both the async and sync version.
+ if (Random().nextFloat() >= 0.5f) {
+ Wendy.shared.clear()
refreshListOfTasks()
- Toast.makeText(this, "Data cleared!", Toast.LENGTH_SHORT).show()
+ Toast.makeText(this, "Data cleared (sync)!", Toast.LENGTH_SHORT).show()
+ } else {
+ Wendy.shared.clearAsync {
+ refreshListOfTasks()
+ Toast.makeText(this, "Data cleared (async)!", Toast.LENGTH_SHORT).show()
+ }
}
}
diff --git a/wendy/src/main/java/com/levibostian/wendy/db/PendingTasksManager.kt b/wendy/src/main/java/com/levibostian/wendy/db/PendingTasksManager.kt
index 280ced4..f4ae6b7 100644
--- a/wendy/src/main/java/com/levibostian/wendy/db/PendingTasksManager.kt
+++ b/wendy/src/main/java/com/levibostian/wendy/db/PendingTasksManager.kt
@@ -232,7 +232,7 @@ internal class PendingTasksManager(private val context: Context) {
@Synchronized
internal fun clear() {
- if (!context.deleteDatabase(PendingTasksDatabaseHelper.DATABASE_NAME)) throw RuntimeException("Wendy database did not delete successfully.")
+ context.deleteDatabase(PendingTasksDatabaseHelper.DATABASE_NAME)
}
}
\ No newline at end of file
diff --git a/wendy/src/main/java/com/levibostian/wendy/service/Wendy.kt b/wendy/src/main/java/com/levibostian/wendy/service/Wendy.kt
index 3387854..bf6b00e 100644
--- a/wendy/src/main/java/com/levibostian/wendy/service/Wendy.kt
+++ b/wendy/src/main/java/com/levibostian/wendy/service/Wendy.kt
@@ -339,21 +339,16 @@ class Wendy private constructor(private val context: Context, internal val tasks
}
/**
- * Stop the task runner and clear all of the [PendingTask]s added to Wendy.
- *
- * This function was written with the intention of using when a user of your app is logged out and you want to clear all of the data associated with that user.
+ * Async version of [clear].
*
- * This function will stop the task runner any tasks, deletes all of the data from the task database, and deletes all the data stored in
+ * @see clear
*/
- fun clear(complete: () -> Unit?) {
- LogUtil.d("Cancel running all PendingTasks.")
- runAllTasksAsyncTask?.cancel(true)
- LogUtil.d("Cancel running given set of PendingTasks.")
- runGivenSetTasksAsyncTask?.cancel(true)
+ fun clearAsync(complete: () -> Unit?) {
+ cancelTaskRunner()
- ClearAsyncTask(tasksManager, PreferenceManager.getDefaultSharedPreferences(context), { error ->
+ ClearAsyncTask(this, { error ->
error?.let {
- LogUtil.d("Error deleting data: $it")
+ LogUtil.d("Error deleting data.")
throw it
}
LogUtil.d("Data cleared successfully.")
@@ -361,6 +356,44 @@ class Wendy private constructor(private val context: Context, internal val tasks
}).execute()
}
+ /**
+ * Stop the task runner and clear all of the [PendingTask]s added to Wendy.
+ *
+ * This function was written with the intention of using when a user of your app is logged out and you want to clear all of the data associated with that user.
+ *
+ * This function will stop the task runner any tasks, deletes all of the data from the task database, and deletes all the data stored in SharedPreferences.
+ *
+ * @see clearAsync For async version.
+ */
+ fun clear() {
+ cancelTaskRunner()
+
+ deleteAllData()
+ }
+
+ private fun deleteAllData() {
+ LogUtil.d("Deleting database.")
+ tasksManager.clear()
+
+ LogUtil.d("Deleting SharedPreferences entries.")
+ val sharedPrefsToDelete: ArrayList = ArrayList()
+ val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
+ sharedPreferences.all.forEach {
+ if (it.key.startsWith(PendingTasksUtil.PREFIX)) sharedPrefsToDelete.add(it.key)
+ }
+ sharedPreferences.edit().apply {
+ sharedPrefsToDelete.forEach { remove(it) }
+ commit()
+ }
+ }
+
+ private fun cancelTaskRunner() {
+ LogUtil.d("Cancel running all PendingTasks.")
+ runAllTasksAsyncTask?.cancel(true)
+ LogUtil.d("Cancel running given set of PendingTasks.")
+ runGivenSetTasksAsyncTask?.cancel(true)
+ }
+
/**
* Get all errors that currently exist for [PendingTask]s.
*/
@@ -368,26 +401,14 @@ class Wendy private constructor(private val context: Context, internal val tasks
return tasksManager.getAllErrors()
}
- private class ClearAsyncTask(private val tasksManager: PendingTasksManager,
- private val sharedPreferences: SharedPreferences,
+ private class ClearAsyncTask(private val wendy: Wendy,
private val complete: (error: Throwable?) -> Unit?): AsyncTask() {
private var doInBackgroundError: Throwable? = null
override fun doInBackground(vararg params: Unit?): Unit? {
try {
- LogUtil.d("Deleting database.")
- tasksManager.clear()
-
- LogUtil.d("Deleting SharedPreferences entries.")
- val sharedPrefsToDelete: ArrayList = ArrayList()
- sharedPreferences.all.forEach {
- if (it.key.startsWith(PendingTasksUtil.PREFIX)) sharedPrefsToDelete.add(it.key)
- }
- sharedPreferences.edit().apply {
- sharedPrefsToDelete.forEach { remove(it) }
- commit()
- }
+ wendy.deleteAllData()
} catch (e: Throwable) {
doInBackgroundError = e
}
From b7e5ec67893121575b3dcafe444656f8917daf51 Mon Sep 17 00:00:00 2001
From: Levi Bostian
Date: Mon, 11 Jun 2018 18:01:29 -0500
Subject: [PATCH 2/3] Update changelog for release.
---
.idea/caches/build_file_checksums.ser | Bin 588 -> 588 bytes
CHANGELOG.md | 9 +++++++++
README.md | 2 ++
wendy/build.gradle | 2 +-
4 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser
index 46645458dbf2b1e7b0eb423e69e91bfe77cc61ed..141645bc980f871e032b74cb39e2f11ba301a2eb 100644
GIT binary patch
delta 35
tcmV+;0Nnq~1k41Gm;~_?)OC@Z`w$6FoD;Yj)Q4BBD~oCEEkBc|0i_@?58(g+
delta 35
tcmV+;0Nnq~1k41Gm;~>hPWX|W`w(A>Le{xzq=0QH5X11=aOab!0i{MO5{du-
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05c1890..b9cbbf1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,12 @@
+### [0.2.1-alpha] 2018-06-11
+
+### Fixed
+- Fix RuntimeException while deleting SQLite database.
+
+### Added
+- Wendy `.clear()` has an async and sync version.
+- **Breaking Change** `Wendy.clear()` has been renamed to `Wendy.clearAsync()`. `Wendy.clear()` is the synchronous version now.
+
### [0.2.0-alpha] 2018-06-11
### Added
diff --git a/README.md b/README.md
index 7fb71f0..e4bedd6 100644
--- a/README.md
+++ b/README.md
@@ -183,6 +183,8 @@ fun createNewGroceryStoreItem(itemName: String) {
Done! Wendy takes care of all the rest. Wendy will try to run your task right away but if you're offline or in a spotty Internet connection, Wendy will wait and try again later.
+Oh, and lastly. If your user decides to logout of your app and you want to delete all of Wendy's data, you can do so via `Wendy.shared.clear()` or `Wendy.shared.clearAsync()`.
+
There is a document on [best practices when using Wendy](BEST_PRACTICES.md). Check that out to answer your questions you have about why Wendy works the way that it does.
## Example app
diff --git a/wendy/build.gradle b/wendy/build.gradle
index 694dea9..bd8105d 100644
--- a/wendy/build.gradle
+++ b/wendy/build.gradle
@@ -15,7 +15,7 @@ android {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
- versionName "0.2.0-alpha"
+ versionName "0.2.1-alpha"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
From 5593c4aa8f623b84807824859cc651f570ba7e8d Mon Sep 17 00:00:00 2001
From: Levi Bostian
Date: Mon, 11 Jun 2018 18:03:33 -0500
Subject: [PATCH 3/3] Generate docs
---
.../-wendy/clear-async.html | 18 ++++++++++++++++++
.../-wendy/clear.html | 9 ++++++---
.../-wendy/index.html | 11 ++++++++++-
docs/wendy/index-outline.html | 6 ++++--
4 files changed, 38 insertions(+), 6 deletions(-)
create mode 100644 docs/wendy/com.levibostian.wendy.service/-wendy/clear-async.html
diff --git a/docs/wendy/com.levibostian.wendy.service/-wendy/clear-async.html b/docs/wendy/com.levibostian.wendy.service/-wendy/clear-async.html
new file mode 100644
index 0000000..9d12da6
--- /dev/null
+++ b/docs/wendy/com.levibostian.wendy.service/-wendy/clear-async.html
@@ -0,0 +1,18 @@
+
+
+
+Wendy.clearAsync - wendy
+
+
+
+wendy / com.levibostian.wendy.service / Wendy / clearAsync
+
+
-
-fun clear(complete:()->Unit?): Unit
+
+fun clear(): Unit
Stop the task runner and clear all of the PendingTasks added to Wendy.
This function was written with the intention of using when a user of your app is logged out and you want to clear all of the data associated with that user.
-
This function will stop the task runner any tasks, deletes all of the data from the task database, and deletes all the data stored in
+
This function will stop the task runner any tasks, deletes all of the data from the task database, and deletes all the data stored in SharedPreferences.