From d62ea313d7328bc4fdda8a4afd69bde78844c35d Mon Sep 17 00:00:00 2001 From: Ricardo Borges Jr Date: Sat, 26 Nov 2022 12:47:38 -0300 Subject: [PATCH] Add option to edit feed URL (#6185) --- .../dialog/EditUrlSettingsDialog.java | 88 +++++++++++++++++++ .../antennapod/fragment/FeedInfoFragment.java | 15 ++++ app/src/main/res/menu/feedinfo.xml | 3 + ui/i18n/src/main/res/values/strings.xml | 2 + 4 files changed, 108 insertions(+) create mode 100644 app/src/main/java/de/danoeh/antennapod/dialog/EditUrlSettingsDialog.java diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/EditUrlSettingsDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/EditUrlSettingsDialog.java new file mode 100644 index 000000000..e6ad78a89 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/EditUrlSettingsDialog.java @@ -0,0 +1,88 @@ +package de.danoeh.antennapod.dialog; + +import android.app.Activity; +import android.os.CountDownTimer; +import android.view.LayoutInflater; +import androidx.appcompat.app.AlertDialog; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.storage.DBTasks; +import de.danoeh.antennapod.core.storage.DBWriter; +import de.danoeh.antennapod.databinding.EditTextDialogBinding; +import de.danoeh.antennapod.model.feed.Feed; + +import java.lang.ref.WeakReference; +import java.util.Locale; +import java.util.concurrent.ExecutionException; + +public abstract class EditUrlSettingsDialog { + public static final String TAG = "EditUrlSettingsDialog"; + private final WeakReference activityRef; + private final Feed feed; + + public EditUrlSettingsDialog(Activity activity, Feed feed) { + this.activityRef = new WeakReference<>(activity); + this.feed = feed; + } + + public void show() { + Activity activity = activityRef.get(); + if (activity == null) { + return; + } + + final EditTextDialogBinding binding = EditTextDialogBinding.inflate(LayoutInflater.from(activity)); + + binding.urlEditText.setText(feed.getDownload_url()); + + new MaterialAlertDialogBuilder(activity) + .setView(binding.getRoot()) + .setTitle(R.string.edit_url_menu) + .setPositiveButton(android.R.string.ok, (d, input) -> + showConfirmAlertDialog(String.valueOf(binding.urlEditText.getText()))) + .setNegativeButton(R.string.cancel_label, null) + .show(); + } + + private void onConfirmed(String original, String updated) { + try { + DBWriter.updateFeedDownloadURL(original, updated).get(); + feed.setDownload_url(updated); + DBTasks.forceRefreshFeed(activityRef.get(), feed, false); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + private void showConfirmAlertDialog(String url) { + Activity activity = activityRef.get(); + + AlertDialog alertDialog = new MaterialAlertDialogBuilder(activity) + .setTitle(R.string.edit_url_menu) + .setMessage(R.string.edit_url_confirmation_msg) + .setPositiveButton(android.R.string.ok, (d, input) -> { + onConfirmed(feed.getDownload_url(), url); + setUrl(url); + }) + .setNegativeButton(R.string.cancel_label, null) + .show(); + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); + + new CountDownTimer(15000, 1000) { + @Override + public void onTick(long millisUntilFinished) { + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText( + String.format(Locale.getDefault(), "%s (%d)", + activity.getString(android.R.string.ok), millisUntilFinished / 1000 + 1)); + } + + @Override + public void onFinish() { + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(android.R.string.ok); + } + }.start(); + } + + protected abstract void setUrl(String url); +} diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedInfoFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedInfoFragment.java index 36a2994e6..4fe2f0386 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedInfoFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedInfoFragment.java @@ -40,6 +40,7 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.syndication.HtmlToPlainText; +import de.danoeh.antennapod.dialog.EditUrlSettingsDialog; import de.danoeh.antennapod.menuhandler.FeedMenuHandler; import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.model.feed.FeedFunding; @@ -277,6 +278,7 @@ public class FeedInfoFragment extends Fragment implements MaterialToolbar.OnMenu toolbar.getMenu().findItem(R.id.share_item).setVisible(feed != null && !feed.isLocalFeed()); toolbar.getMenu().findItem(R.id.visit_website_item).setVisible(feed != null && feed.getLink() != null && IntentUtils.isCallable(getContext(), new Intent(Intent.ACTION_VIEW, Uri.parse(feed.getLink())))); + toolbar.getMenu().findItem(R.id.edit_feed_url_item).setVisible(feed != null && !feed.isLocalFeed()); } @Override @@ -303,6 +305,19 @@ public class FeedInfoFragment extends Fragment implements MaterialToolbar.OnMenu return true; } + if (item.getItemId() == R.id.edit_feed_url_item) { + new EditUrlSettingsDialog(getActivity(), feed) { + @Override + protected void setUrl(String url) { + feed.setDownload_url(url); + txtvUrl.setText(feed.getDownload_url() + " {fa-paperclip}"); + Iconify.addIcons(txtvUrl); + } + }.show(); + + return true; + } + return handled; } diff --git a/app/src/main/res/menu/feedinfo.xml b/app/src/main/res/menu/feedinfo.xml index 254e1834b..c3ca66239 100644 --- a/app/src/main/res/menu/feedinfo.xml +++ b/app/src/main/res/menu/feedinfo.xml @@ -18,4 +18,7 @@ custom:showAsAction="collapseActionView" android:title="@string/reconnect_local_folder" android:visible="false" /> + \ No newline at end of file diff --git a/ui/i18n/src/main/res/values/strings.xml b/ui/i18n/src/main/res/values/strings.xml index f6213f9d4..327d758f0 100644 --- a/ui/i18n/src/main/res/values/strings.xml +++ b/ui/i18n/src/main/res/values/strings.xml @@ -706,6 +706,8 @@ Episodes started/total: View for all podcasts ยป {fa-spinner} + Edit feed URL + Changing the RSS address can easily break the playback state and episode listings of the podcast. We do NOT recommend changing it and will NOT provide support if anything goes wrong. This cannot be undone. The broken subscription CANNOT be repaired by simply changing the address back. We suggest creating a backup before continuing. Importing subscriptions from single-purpose apps…