From 587090636dfff0b358b15026cf7d47c65a4296dc Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 17 Dec 2024 04:22:37 +0400 Subject: [PATCH] fix(YouTube - SponsorBlock): Show create new segment error messages using a dialog (#4148) --- .../SponsorBlockPreferenceFragment.java | 2 +- .../sponsorblock/SponsorBlockUtils.java | 10 ++++ .../sponsorblock/requests/SBRequester.java | 57 +++++++++---------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/SponsorBlockPreferenceFragment.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/SponsorBlockPreferenceFragment.java index 9fa4a942a..2d1324d09 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/SponsorBlockPreferenceFragment.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/SponsorBlockPreferenceFragment.java @@ -507,7 +507,7 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment { Utils.showToastLong(str("revanced_sb_stats_username_changed")); } else { preference.setText(userName); // revert to previous - Utils.showToastLong(errorMessage); + SponsorBlockUtils.showErrorDialog(errorMessage); } }); }); diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/SponsorBlockUtils.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/SponsorBlockUtils.java index a8b62e7e6..3fdfe1f9f 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/SponsorBlockUtils.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/SponsorBlockUtils.java @@ -363,6 +363,16 @@ public class SponsorBlockUtils { } } + public static void showErrorDialog(String dialogMessage) { + Utils.runOnMainThreadNowOrLater(() -> + new AlertDialog.Builder(SponsorBlockViewController.getOverLaysViewGroupContext()) + .setMessage(dialogMessage) + .setPositiveButton(android.R.string.ok, null) + .setCancelable(false) + .show() + ); + } + public static void onEditByHandClicked() { try { Utils.verifyOnMainThread(); diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/requests/SBRequester.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/requests/SBRequester.java index f67489850..6a36a2525 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/requests/SBRequester.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/sponsorblock/requests/SBRequester.java @@ -19,6 +19,7 @@ import java.util.concurrent.TimeUnit; import app.revanced.extension.youtube.settings.Settings; import app.revanced.extension.youtube.sponsorblock.SponsorBlockSettings; +import app.revanced.extension.youtube.sponsorblock.SponsorBlockUtils; import app.revanced.extension.youtube.sponsorblock.objects.SegmentCategory; import app.revanced.extension.youtube.sponsorblock.objects.SponsorSegment; import app.revanced.extension.youtube.sponsorblock.objects.SponsorSegment.SegmentVote; @@ -142,6 +143,7 @@ public class SBRequester { public static void submitSegments(@NonNull String videoId, @NonNull String category, long startTime, long endTime, long videoLength) { Utils.verifyOffMainThread(); + try { String privateUserId = SponsorBlockSettings.getSBPrivateUserID(); String start = String.format(Locale.US, TIME_TEMPLATE, startTime / 1000f); @@ -151,35 +153,29 @@ public class SBRequester { HttpURLConnection connection = getConnectionFromRoute(SBRoutes.SUBMIT_SEGMENTS, privateUserId, videoId, category, start, end, duration); final int responseCode = connection.getResponseCode(); - final String messageToToast; - switch (responseCode) { - case HTTP_STATUS_CODE_SUCCESS: - messageToToast = str("revanced_sb_submit_succeeded"); - break; - case 409: - messageToToast = str("revanced_sb_submit_failed_duplicate"); - break; - case 403: - messageToToast = str("revanced_sb_submit_failed_forbidden", Requester.parseErrorStringAndDisconnect(connection)); - break; - case 429: - messageToToast = str("revanced_sb_submit_failed_rate_limit"); - break; - case 400: - messageToToast = str("revanced_sb_submit_failed_invalid", Requester.parseErrorStringAndDisconnect(connection)); - break; - default: - messageToToast = str("revanced_sb_submit_failed_unknown_error", responseCode, connection.getResponseMessage()); - break; - } - Utils.showToastLong(messageToToast); + String userMessage = switch (responseCode) { + case HTTP_STATUS_CODE_SUCCESS -> str("revanced_sb_submit_succeeded"); + case 409 -> str("revanced_sb_submit_failed_duplicate"); + case 403 -> str("revanced_sb_submit_failed_forbidden", + Requester.parseErrorStringAndDisconnect(connection)); + case 429 -> str("revanced_sb_submit_failed_rate_limit"); + case 400 -> str("revanced_sb_submit_failed_invalid", + Requester.parseErrorStringAndDisconnect(connection)); + default -> str("revanced_sb_submit_failed_unknown_error", + responseCode, connection.getResponseMessage()); + }; + + // Message might be about the users account or an error too large to show in a toast. + // Use a dialog instead. + SponsorBlockUtils.showErrorDialog(userMessage); } catch (SocketTimeoutException ex) { - // Always show, even if show connection toasts is turned off + Logger.printDebug(() -> "Timeout", ex); Utils.showToastLong(str("revanced_sb_submit_failed_timeout")); } catch (IOException ex) { + Logger.printDebug(() -> "IOException", ex); Utils.showToastLong(str("revanced_sb_submit_failed_unknown_error", 0, ex.getMessage())); } catch (Exception ex) { - Logger.printException(() -> "failed to submit segments", ex); + Logger.printException(() -> "failed to submit segments", ex); // Should never happen. } } @@ -218,19 +214,22 @@ public class SBRequester { : getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_QUALITY, uuid, segmentUuid, String.valueOf(voteOption.apiVoteType)); final int responseCode = connection.getResponseCode(); + String userMessage; switch (responseCode) { case HTTP_STATUS_CODE_SUCCESS: Logger.printDebug(() -> "Vote success for segment: " + segment); - break; + return; case 403: - Utils.showToastLong( - str("revanced_sb_vote_failed_forbidden", Requester.parseErrorStringAndDisconnect(connection))); + userMessage = str("revanced_sb_vote_failed_forbidden", + Requester.parseErrorStringAndDisconnect(connection)); break; default: - Utils.showToastLong( - str("revanced_sb_vote_failed_unknown_error", responseCode, connection.getResponseMessage())); + userMessage = str("revanced_sb_vote_failed_unknown_error", + responseCode, connection.getResponseMessage()); break; } + + SponsorBlockUtils.showErrorDialog(userMessage); } catch (SocketTimeoutException ex) { Utils.showToastShort(str("revanced_sb_vote_failed_timeout")); } catch (IOException ex) {