Skip to content

Commit

Permalink
Commit path immediately when import backup
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangRongLin committed May 31, 2021
1 parent f13a1b0 commit 05eb0d0
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.schabi.newpipe.settings;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -205,7 +206,7 @@ private void exportDatabase(final File folder) {
.getDefaultSharedPreferences(requireContext());
manager.exportDatabase(preferences, path);

setImportExportDataPath(folder);
setImportExportDataPath(folder, false);

Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Expand All @@ -230,7 +231,7 @@ private void importDatabase(final File file) {

if (!manager.extractDb(filePath)) {
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
.show();
.show();
}

//If settings file exist, ask if it should be imported.
Expand Down Expand Up @@ -264,14 +265,16 @@ private void importDatabase(final File file) {
*/
private void finishImport(final File file) {
if (file.getParentFile() != null) {
setImportExportDataPath(file.getParentFile());
//immediately because app is about to exit
setImportExportDataPath(file.getParentFile(), true);
}

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

private void setImportExportDataPath(final File file) {
@SuppressLint("ApplySharedPref")
private void setImportExportDataPath(final File file, final boolean immediately) {
final String directoryPath;
if (file.isDirectory()) {
directoryPath = file.getAbsolutePath();
Expand All @@ -283,6 +286,13 @@ private void setImportExportDataPath(final File file) {
directoryPath = "";
}
}
defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply();
final SharedPreferences.Editor editor = defaultPreferences
.edit()
.putString(importExportDataPathKey, directoryPath);
if (immediately) {
editor.commit();
} else {
editor.apply();
}
}
}

0 comments on commit 05eb0d0

Please sign in to comment.