Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove option to immediately commit pref changes on import #6531

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.preference.Preference;
Expand All @@ -33,6 +32,7 @@
import org.schabi.newpipe.util.ZipHelper;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand All @@ -43,7 +43,8 @@

public class ContentSettingsFragment extends BasePreferenceFragment {
private static final String ZIP_MIME_TYPE = "application/zip";
private static final SimpleDateFormat EXPORT_DATE_FORMAT

private final SimpleDateFormat exportDateFormat
= new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);

private ContentSettingsManager manager;
Expand All @@ -52,7 +53,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private String thumbnailLoadToggleKey;
private String youtubeRestrictedModeEnabledKey;

@Nullable private Uri lastImportExportDataUri = null;
private Localization initialSelectedLocalization;
private ContentCountry initialSelectedContentCountry;
private String initialLanguage;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro

requestExportPathLauncher.launch(
StoredFileHelper.getNewPicker(requireContext(),
"NewPipeData-" + EXPORT_DATE_FORMAT.format(new Date()) + ".zip",
"NewPipeData-" + exportDateFormat.format(new Date()) + ".zip",
ZIP_MIME_TYPE, getImportExportDataUri()));
return true;
});
Expand Down Expand Up @@ -162,61 +162,63 @@ public void onDestroy() {
private void requestExportPathResult(final ActivityResult result) {
assureCorrectAppLanguage(getContext());
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
lastImportExportDataUri = result.getData().getData(); // will be saved only on success
// will be saved only on success
final Uri lastExportDataUri = result.getData().getData();

final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);

exportDatabase(file);
exportDatabase(file, lastExportDataUri);
}
}

private void requestImportPathResult(final ActivityResult result) {
assureCorrectAppLanguage(getContext());
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
lastImportExportDataUri = result.getData().getData(); // will be saved only on success
// will be saved only on success
final Uri lastImportDataUri = result.getData().getData();

final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);

new AlertDialog.Builder(requireActivity())
.setMessage(R.string.override_current_data)
.setPositiveButton(R.string.finish, (d, id) ->
importDatabase(file))
importDatabase(file, lastImportDataUri))
.setNegativeButton(R.string.cancel, (d, id) ->
d.cancel())
.create()
.show();
}
}

private void exportDatabase(final StoredFileHelper file) {
private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri) {
try {
//checkpoint before export
NewPipeDatabase.checkpoint();

final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(requireContext());
.getDefaultSharedPreferences(requireContext());
manager.exportDatabase(preferences, file);

saveLastImportExportDataUri(false); // save export path only on success
saveLastImportExportDataUri(exportDataUri); // save export path only on success
Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e);
}
}

private void importDatabase(final StoredFileHelper file) {
private void importDatabase(final StoredFileHelper file, final Uri importDataUri) {
// check if file is supported
if (!ZipHelper.isValidZipFile(file)) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show();
.show();
return;
}

try {
if (!manager.ensureDbDirectoryExists()) {
throw new Exception("Could not create databases dir");
throw new IOException("Could not create databases dir");
}

if (!manager.extractDb(file)) {
Expand All @@ -231,17 +233,17 @@ private void importDatabase(final StoredFileHelper file) {

alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
dialog.dismiss();
finishImport();
finishImport(importDataUri);
});
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext()));
finishImport();
.getDefaultSharedPreferences(requireContext()));
finishImport(importDataUri);
});
alert.show();
} else {
finishImport();
finishImport(importDataUri);
}
} catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
Expand All @@ -250,10 +252,12 @@ private void importDatabase(final StoredFileHelper file) {

/**
* Save import path and restart system.
*
* @param importDataUri The import path to save
*/
private void finishImport() {
// save import path only on success; save immediately because app is about to exit
saveLastImportExportDataUri(true);
private void finishImport(final Uri importDataUri) {
// save import path only on success
saveLastImportExportDataUri(importDataUri);
// restart app to properly load db
NavigationHelper.restartApp(requireActivity());
}
Expand All @@ -263,16 +267,9 @@ private Uri getImportExportDataUri() {
return isBlank(path) ? null : Uri.parse(path);
}

private void saveLastImportExportDataUri(final boolean immediately) {
if (lastImportExportDataUri != null) {
final SharedPreferences.Editor editor = defaultPreferences.edit()
.putString(importExportDataPathKey, lastImportExportDataUri.toString());
if (immediately) {
// noinspection ApplySharedPref
editor.commit(); // app about to be restarted, commit immediately
} else {
editor.apply();
}
}
private void saveLastImportExportDataUri(final Uri importExportDataUri) {
final SharedPreferences.Editor editor = defaultPreferences.edit()
.putString(importExportDataPathKey, importExportDataUri.toString());
editor.apply();
}
}