1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-25 12:32:31 +01:00

Merge pull request #6531 from XiangRongLin/immediat_pref_commit

Remove option to immediately commit pref changes on import
This commit is contained in:
Tobi 2021-07-14 09:48:58 +02:00 committed by GitHub
commit 1584028995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,6 @@ import android.widget.Toast;
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;
@ -33,6 +32,7 @@ import org.schabi.newpipe.util.NavigationHelper;
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;
@ -43,7 +43,8 @@ import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
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;
@ -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;
@ -86,7 +86,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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;
});
@ -162,19 +162,21 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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);
@ -182,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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()
@ -190,33 +192,33 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
}
}
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)) {
@ -231,17 +233,17 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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);
@ -250,10 +252,12 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
/**
* 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());
}
@ -263,16 +267,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
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();
}
}