Skip to content

Commit

Permalink
Set ImportExportDataPath only on successful import
Browse files Browse the repository at this point in the history
Also set the folder instead of the file itself as path
  • Loading branch information
XiangRongLin committed May 31, 2021
1 parent fd4408e commit f13a1b0
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,14 @@ public void onActivityResult(final int requestCode, final int resultCode,
if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH)
&& resultCode == Activity.RESULT_OK && data.getData() != null) {
final File file = Utils.getFileForUri(data.getData());
final String path = file.getAbsolutePath();

if (requestCode == REQUEST_EXPORT_PATH) {
exportDatabase(file);
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setMessage(R.string.override_current_data)
.setPositiveButton(getString(R.string.finish),
(d, id) -> importDatabase(path))
(d, id) -> importDatabase(file))
.setNegativeButton(android.R.string.cancel,
(d, id) -> d.cancel());
builder.create().show();
Expand Down Expand Up @@ -214,11 +213,13 @@ private void exportDatabase(final File folder) {
}
}

private void importDatabase(final String filePath) {
private void importDatabase(final File file) {
final String filePath = file.getAbsolutePath();

// check if file is supported
if (!ZipHelper.isValidZipFile(filePath)) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show();
.show();
return;
}

Expand All @@ -239,26 +240,37 @@ private void importDatabase(final String filePath) {

alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
dialog.dismiss();
// restart app to properly load db
System.exit(0);
finishImport(file);
});
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext()));
// restart app to properly load db
System.exit(0);
.getDefaultSharedPreferences(requireContext()));
finishImport(file);
});
alert.show();
} else {
// restart app to properly load db
System.exit(0);
finishImport(file);
}
} catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
}
}

/**
* Save import path and restart system.
*
* @param file The file of the created backup
*/
private void finishImport(final File file) {
if (file.getParentFile() != null) {
setImportExportDataPath(file.getParentFile());
}

// restart app to properly load db
System.exit(0);
}

private void setImportExportDataPath(final File file) {
final String directoryPath;
if (file.isDirectory()) {
Expand Down

0 comments on commit f13a1b0

Please sign in to comment.