-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
settings: add UI options to import/export subscriptions #12344
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -34,6 +34,8 @@ | |||
import org.schabi.newpipe.streams.io.StoredFileHelper; | ||||
import org.schabi.newpipe.util.NavigationHelper; | ||||
import org.schabi.newpipe.util.ZipHelper; | ||||
import org.schabi.newpipe.local.subscription.services.SubscriptionsExportService; | ||||
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService; | ||||
|
||||
import java.io.File; | ||||
import java.io.IOException; | ||||
|
@@ -56,6 +58,13 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment { | |||
private final ActivityResultLauncher<Intent> requestExportPathLauncher = | ||||
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), | ||||
this::requestExportPathResult); | ||||
private final ActivityResultLauncher<Intent> requestExportSubsLauncher = | ||||
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), | ||||
this::requestExportSubsResult); | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
private final ActivityResultLauncher<Intent> requestImportSubsLauncher = | ||||
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), | ||||
this::requestImportSubsResult); | ||||
|
||||
|
||||
@Override | ||||
|
@@ -123,6 +132,42 @@ ZIP_MIME_TYPE, getImportExportDataUri()), | |||
alertDialog.show(); | ||||
return true; | ||||
}); | ||||
|
||||
final Preference exportSubsPreference = findPreference("export_subscriptions"); | ||||
if (exportSubsPreference != null) { | ||||
exportSubsPreference.setOnPreferenceClickListener(preference -> { | ||||
NoFileManagerSafeGuard.launchSafe( | ||||
requestExportSubsLauncher, | ||||
StoredFileHelper.getNewPicker( | ||||
requireContext(), | ||||
"newpipe_subscriptions_" + exportDateFormat.format(new Date()) | ||||
+ ".json", | ||||
"application/json", | ||||
null | ||||
), | ||||
TAG, | ||||
getContext() | ||||
); | ||||
return true; | ||||
}); | ||||
} | ||||
|
||||
final Preference importSubsPreference = findPreference("import_subscriptions"); | ||||
if (importSubsPreference != null) { | ||||
importSubsPreference.setOnPreferenceClickListener(preference -> { | ||||
NoFileManagerSafeGuard.launchSafe( | ||||
requestImportSubsLauncher, | ||||
StoredFileHelper.getPicker( | ||||
requireContext(), | ||||
"application/json" | ||||
), | ||||
TAG, | ||||
getContext() | ||||
); | ||||
return true; | ||||
}); | ||||
} | ||||
|
||||
} | ||||
|
||||
private void requestExportPathResult(final ActivityResult result) { | ||||
|
@@ -157,6 +202,36 @@ private void requestImportPathResult(final ActivityResult result) { | |||
} | ||||
} | ||||
|
||||
private void requestExportSubsResult(final ActivityResult result) { | ||||
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { | ||||
final Uri fileUri = result.getData().getData(); | ||||
if (fileUri != null) { | ||||
final Intent intent = new Intent( | ||||
requireContext(), SubscriptionsExportService.class); | ||||
intent.putExtra(SubscriptionsExportService.KEY_FILE_PATH, fileUri); | ||||
requireContext().startService(intent); | ||||
Toast.makeText(requireContext(), R.string.exporting, Toast.LENGTH_SHORT).show(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of showing a Toast I would do the same as in |
||||
} | ||||
} | ||||
} | ||||
|
||||
private void requestImportSubsResult(final ActivityResult result) { | ||||
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { | ||||
final Uri fileUri = result.getData().getData(); | ||||
if (fileUri != null) { | ||||
final Intent intent = new Intent( | ||||
requireContext(), SubscriptionsImportService.class); | ||||
intent.putExtra( | ||||
SubscriptionsImportService.KEY_MODE, | ||||
SubscriptionsImportService.PREVIOUS_EXPORT_MODE | ||||
); | ||||
intent.putExtra(SubscriptionsImportService.KEY_VALUE, fileUri); | ||||
requireContext().startService(intent); | ||||
Toast.makeText(requireContext(), R.string.importing, Toast.LENGTH_SHORT).show(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||||
} | ||||
} | ||||
} | ||||
|
||||
private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri) { | ||||
try { | ||||
//checkpoint before export | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,18 @@ | |
android:summary="@string/reset_settings_summary" | ||
app:singleLineTitle="false" | ||
app:iconSpaceReserved="false" /> | ||
|
||
<Preference | ||
android:key="export_subscriptions" | ||
android:title="Export subscriptions" | ||
android:summary="Export your subscriptions to a .json file" | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new strings under |
||
app:singleLineTitle="false" | ||
app:iconSpaceReserved="false" /> | ||
|
||
<Preference | ||
android:key="import_subscriptions" | ||
android:title="Import subscriptions" | ||
android:summary="Import subscriptions from a previous .json export" | ||
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
app:singleLineTitle="false" | ||
app:iconSpaceReserved="false" /> | ||
</PreferenceScreen> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code you added in this class should be deduplicated with respect to the code in
SubscriptionFragment.kt
. You could create a few utils functions in a new fileSubscriptionsImportExportHelper.kt
underapp/src/main/java/org/schabi/newpipe/local/subscription/services
, and call them both from here and fromSubscriptionFragment.kt
. Obviously theActivityResultLauncher
cannot be deduplicated, but the rest of the code should be.