Skip to content

Commit

Permalink
ADD exitCodes to onCompleteListener
Browse files Browse the repository at this point in the history
  • Loading branch information
rafi0101 committed Jan 6, 2022
1 parent 5f3ef8a commit 635f79e
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 48 deletions.
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Usage
-----------

* [Properties](#Properties)
* [Exit Codes](#Exit-Codes)
* [Example Kotlin](#Example-Kotlin)
* [Example Java](#Example-Java)

Expand Down Expand Up @@ -215,17 +216,39 @@ The following options are optional and the default options

* Restart your Application. Can be implemented in the onCompleteListener, when "success == true"

**Attention**\
it does not always work reliably!\
But you can use other methods.\
Important is that all activities / fragments that are still open must be closed and reopened\
Because the Database instance is a new one, and the old activities / fragments are trying to work with the old instance

**Attention**\
it does not always work reliably!\
But you can use other methods.\
Important is that all activities / fragments that are still open must be closed and reopened\
Because the Database instance is a new one, and the old activities / fragments are trying to
work with the old instance

```kotlin
.restartApp(Intent(this@MainActivity, MainActivity::class.java))
```

### Exit Codes

Here are all exit codes for the onCompleteListener.
They can be calles using ```OnCompleteListener.$NAME$```

| Exit Code | Name | Description |
| --------- | :--------------------------------------------------- | ------------ |
| 0 | ```EXIT_CODE_SUCCESS``` | No error, action successful |
| 1 | ```EXIT_CODE_ERROR``` | Other Error |
| 2 | ```EXIT_CODE_ERROR_BACKUP_FILE_CHOOSER``` | Error while choosing backup to restore. Maybe no file selected |
| 3 | ```EXIT_CODE_ERROR_BACKUP_FILE_CREATOR``` | Error while choosing backup file to create. Maybe no file selected |
| 4 | ```EXIT_CODE_ERROR_BACKUP_LOCATION_FILE_MISSING``` | [BACKUP_FILE_LOCATION_CUSTOM_FILE] is set but [RoomBackup.backupLocationCustomFile] is not set |
| 5 | ```EXIT_CODE_ERROR_BACKUP_LOCATION_MISSING``` | [RoomBackup.backupLocation] is not set |
| 6 | ```EXIT_CODE_ERROR_BY_USER_CANCELED``` | Restore dialog for internal/external storage was canceled by user |
| 7 | ```EXIT_CODE_ERROR_DECRYPTION_ERROR``` | Cannot decrypt provided backup file |
| 8 | ```EXIT_CODE_ERROR_ENCRYPTION_ERROR``` | Cannot encrypt database backup |
| 9 | ```EXIT_CODE_ERROR_RESTORE_BACKUP_IS_ENCRYPTED``` | You tried to restore a encrypted backup but [RoomBackup.backupIsEncrypted] is set to false |
| 10 | ```EXIT_CODE_ERROR_RESTORE_NO_BACKUPS_AVAILABLE``` | No backups to restore are available in internal/external sotrage |
| 11 | ```EXIT_CODE_ERROR_ROOM_DATABASE_MISSING``` | No room database to backup is provided |
| 12 | ```EXIT_CODE_ERROR_STORAGE_PERMISSONS_NOT_GRANTED``` | Storage permissions not granted for custom dialog |
| 13 | ```EXIT_CODE_ERROR_WRONG_DECRYPTION_PASSWORD``` | Cannot decrypt provided backup file because the password is incorrect |

### Example Kotlin

* ##### Backup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import de.raphaelebner.roomdatabasebackup.core.RoomBackup
import de.raphaelebner.roomdatabasebackup.sample.database.main.FruitDatabase
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.Fruit
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitListAdapter
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import java.io.File


Expand Down Expand Up @@ -189,9 +189,13 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
//maxFileCount: else 1000 because i cannot surround it with if condition
.maxFileCount(if (useMaxFileCount) 5 else 1000)
.apply {
onCompleteListener { success, message ->
Log.d(TAG, "success: $success, message: $message")
Toast.makeText(this@MainActivity, "success: $success, message: $message", Toast.LENGTH_LONG).show()
onCompleteListener { success, message, exitCode ->
Log.d(TAG, "success: $success, message: $message, exitCode: $exitCode")
Toast.makeText(
this@MainActivity,
"success: $success, message: $message, exitCode: $exitCode",
Toast.LENGTH_LONG
).show()
if (success) restartApp(Intent(this@MainActivity, MainActivity::class.java))
}
}
Expand All @@ -208,9 +212,13 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
.backupIsEncrypted(encryptBackup)
.customEncryptPassword(SECRET_PASSWORD)
.apply {
onCompleteListener { success, message ->
Log.d(TAG, "success: $success, message: $message")
Toast.makeText(this@MainActivity, "success: $success, message: $message", Toast.LENGTH_LONG).show()
onCompleteListener { success, message, exitCode ->
Log.d(TAG, "success: $success, message: $message, exitCode: $exitCode")
Toast.makeText(
this@MainActivity,
"success: $success, message: $message, exitCode: $exitCode",
Toast.LENGTH_LONG
).show()
if (success) restartApp(Intent(this@MainActivity, MainActivity::class.java))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import de.raphaelebner.roomdatabasebackup.core.RoomBackup;
import de.raphaelebner.roomdatabasebackup.sample.database.main.FruitDatabase;
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.Fruit;
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitListAdapter;
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitViewModel;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.jetbrains.annotations.NotNull;

import java.io.File;

import de.raphaelebner.roomdatabasebackup.core.RoomBackup;
import de.raphaelebner.roomdatabasebackup.sample.database.main.FruitDatabase;
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.Fruit;
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitListAdapter;
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitViewModel;

/**
* MIT License
* <p>
Expand Down Expand Up @@ -213,9 +214,10 @@ protected void onCreate(Bundle savedInstanceState) {
roomBackup.backupIsEncrypted(encryptBackup);
roomBackup.customEncryptPassword(MainActivity.SECRET_PASSWORD);
if (useMaxFileCount) roomBackup.maxFileCount(5);
roomBackup.onCompleteListener((success, message) -> {
roomBackup.onCompleteListener((success, message, exitCode) -> {
Log.d(TAG, "oncomplete: " + success + ", message: " + message);
if (success) roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
if (success)
roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
});
roomBackup.backup();

Expand All @@ -228,9 +230,10 @@ protected void onCreate(Bundle savedInstanceState) {
roomBackup.enableLogDebug(enableLog);
roomBackup.backupIsEncrypted(encryptBackup);
roomBackup.customEncryptPassword(MainActivity.SECRET_PASSWORD);
roomBackup.onCompleteListener((success, message) -> {
roomBackup.onCompleteListener((success, message, exitCode) -> {
Log.d(TAG, "oncomplete: " + success + ", message: " + message);
if (success) roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
if (success)
roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
});
roomBackup.restore();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.raphaelebner.roomdatabasebackup.core

import de.raphaelebner.roomdatabasebackup.core.RoomBackup.Companion.BACKUP_FILE_LOCATION_CUSTOM_FILE

/**
* MIT License
*
Expand All @@ -24,5 +26,51 @@ package de.raphaelebner.roomdatabasebackup.core
* SOFTWARE.
*/
interface OnCompleteListener {
fun onComplete(success: Boolean, message: String)
}
fun onComplete(success: Boolean, message: String, exitCode: Int)

companion object {

/** Other Error */
const val EXIT_CODE_ERROR = 1

/** Error while choosing backup to restore. Maybe no file selected */
const val EXIT_CODE_ERROR_BACKUP_FILE_CHOOSER = 2

/** Error while choosing backup file to create. Maybe no file selected */
const val EXIT_CODE_ERROR_BACKUP_FILE_CREATOR = 3

/** [BACKUP_FILE_LOCATION_CUSTOM_FILE] is set but [RoomBackup.backupLocationCustomFile] is not set */
const val EXIT_CODE_ERROR_BACKUP_LOCATION_FILE_MISSING = 4

/** [RoomBackup.backupLocation] is not set */
const val EXIT_CODE_ERROR_BACKUP_LOCATION_MISSING = 5

/** Restore dialog for internal/external storage was canceled by user */
const val EXIT_CODE_ERROR_BY_USER_CANCELED = 6

/** Cannot decrypt provided backup file */
const val EXIT_CODE_ERROR_DECRYPTION_ERROR = 7

/** Cannot encrypt database backup */
const val EXIT_CODE_ERROR_ENCRYPTION_ERROR = 8

/** You tried to restore a encrypted backup but [RoomBackup.backupIsEncrypted] is set to false */
const val EXIT_CODE_ERROR_RESTORE_BACKUP_IS_ENCRYPTED = 9

/** No backups to restore are available in internal/external sotrage */
const val EXIT_CODE_ERROR_RESTORE_NO_BACKUPS_AVAILABLE = 10

/** No room database to backup is provided */
const val EXIT_CODE_ERROR_ROOM_DATABASE_MISSING = 11

/** Storage permissions not granted for custom dialog */
const val EXIT_CODE_ERROR_STORAGE_PERMISSONS_NOT_GRANTED = 12

/** Cannot decrypt provided backup file because the password is incorrect */
const val EXIT_CODE_ERROR_WRONG_DECRYPTION_PASSWORD = 13

/** No error, action successful */
const val EXIT_CODE_SUCCESS = 0
}

}
Loading

0 comments on commit 635f79e

Please sign in to comment.