Remove variable ContentSettingsFragment.lastImportExportDataUri

Instead pass the value through the methods as parameter
This commit is contained in:
XiangRongLin 2021-06-20 09:28:39 +02:00
parent 0bdf8de38e
commit 785c0376f8

View file

@ -12,7 +12,6 @@ import android.widget.Toast;
import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.preference.Preference; import androidx.preference.Preference;
@ -54,8 +53,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private String thumbnailLoadToggleKey; private String thumbnailLoadToggleKey;
private String youtubeRestrictedModeEnabledKey; private String youtubeRestrictedModeEnabledKey;
@Nullable
private Uri lastImportExportDataUri = null;
private Localization initialSelectedLocalization; private Localization initialSelectedLocalization;
private ContentCountry initialSelectedContentCountry; private ContentCountry initialSelectedContentCountry;
private String initialLanguage; private String initialLanguage;
@ -165,19 +162,21 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private void requestExportPathResult(final ActivityResult result) { private void requestExportPathResult(final ActivityResult result) {
assureCorrectAppLanguage(getContext()); assureCorrectAppLanguage(getContext());
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { 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 final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);
exportDatabase(file); exportDatabase(file, lastExportDataUri);
} }
} }
private void requestImportPathResult(final ActivityResult result) { private void requestImportPathResult(final ActivityResult result) {
assureCorrectAppLanguage(getContext()); assureCorrectAppLanguage(getContext());
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { 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 final StoredFileHelper file
= new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE); = new StoredFileHelper(getContext(), result.getData().getData(), ZIP_MIME_TYPE);
@ -185,7 +184,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
new AlertDialog.Builder(requireActivity()) new AlertDialog.Builder(requireActivity())
.setMessage(R.string.override_current_data) .setMessage(R.string.override_current_data)
.setPositiveButton(R.string.finish, (d, id) -> .setPositiveButton(R.string.finish, (d, id) ->
importDatabase(file)) importDatabase(file, lastImportDataUri))
.setNegativeButton(R.string.cancel, (d, id) -> .setNegativeButton(R.string.cancel, (d, id) ->
d.cancel()) d.cancel())
.create() .create()
@ -193,7 +192,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
} }
} }
private void exportDatabase(final StoredFileHelper file) { private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri) {
try { try {
//checkpoint before export //checkpoint before export
NewPipeDatabase.checkpoint(); NewPipeDatabase.checkpoint();
@ -202,14 +201,14 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
.getDefaultSharedPreferences(requireContext()); .getDefaultSharedPreferences(requireContext());
manager.exportDatabase(preferences, file); manager.exportDatabase(preferences, file);
saveLastImportExportDataUri(); // 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(); Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show();
} catch (final Exception e) { } catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", 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 // check if file is supported
if (!ZipHelper.isValidZipFile(file)) { if (!ZipHelper.isValidZipFile(file)) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT) Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
@ -234,17 +233,17 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
alert.setNegativeButton(android.R.string.no, (dialog, which) -> { alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
finishImport(); finishImport(importDataUri);
}); });
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> { alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext())); .getDefaultSharedPreferences(requireContext()));
finishImport(); finishImport(importDataUri);
}); });
alert.show(); alert.show();
} else { } else {
finishImport(); finishImport(importDataUri);
} }
} catch (final Exception e) { } catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e); ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
@ -253,10 +252,12 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
/** /**
* Save import path and restart system. * Save import path and restart system.
*
* @param importDataUri The import path to save
*/ */
private void finishImport() { private void finishImport(final Uri importDataUri) {
// save import path only on success // save import path only on success
saveLastImportExportDataUri(); saveLastImportExportDataUri(importDataUri);
// restart app to properly load db // restart app to properly load db
NavigationHelper.restartApp(requireActivity()); NavigationHelper.restartApp(requireActivity());
} }
@ -266,11 +267,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
return isBlank(path) ? null : Uri.parse(path); return isBlank(path) ? null : Uri.parse(path);
} }
private void saveLastImportExportDataUri() { private void saveLastImportExportDataUri(final Uri importExportDataUri) {
if (lastImportExportDataUri != null) {
final SharedPreferences.Editor editor = defaultPreferences.edit() final SharedPreferences.Editor editor = defaultPreferences.edit()
.putString(importExportDataPathKey, lastImportExportDataUri.toString()); .putString(importExportDataPathKey, importExportDataUri.toString());
editor.apply(); editor.apply();
} }
}
} }