From aedb3ab1718850983ecd6af887879e309330be5e Mon Sep 17 00:00:00 2001 From: caneleex Date: Sat, 24 Jul 2021 18:53:59 +0200 Subject: [PATCH] add proper response handling to setUsername --- .../java/pl/jakubweg/SponsorBlockUtils.java | 4 +++- .../java/pl/jakubweg/requests/Requester.java | 15 ++++++++++----- app/src/main/res/values/strings.xml | 19 +++++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index 127a07b..0695dcd 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -190,6 +190,7 @@ public abstract class SponsorBlockUtils { }; private static final Runnable toastRunnable = () -> { Context context = appContext.get(); + Log.d("cock", String.valueOf(context)); if (context != null && messageToToast != null) Toast.makeText(context, messageToToast, Toast.LENGTH_LONG).show(); }; @@ -452,7 +453,8 @@ public abstract class SponsorBlockUtils { preference.setSummary(str("stats_username_change")); preference.setText(userName); preference.setOnPreferenceChangeListener((preference1, newUsername) -> { - Requester.setUsername((String) newUsername); + appContext = new WeakReference<>(context.getApplicationContext()); + Requester.setUsername((String) newUsername, toastRunnable); return false; }); } diff --git a/app/src/main/java/pl/jakubweg/requests/Requester.java b/app/src/main/java/pl/jakubweg/requests/Requester.java index b3ae0cd..acef509 100644 --- a/app/src/main/java/pl/jakubweg/requests/Requester.java +++ b/app/src/main/java/pl/jakubweg/requests/Requester.java @@ -134,9 +134,6 @@ public class Requester { case 403: SponsorBlockUtils.messageToToast = str("vote_failed_forbidden"); break; - case 429: - SponsorBlockUtils.messageToToast = str("vote_failed_rate_limit"); - break; default: SponsorBlockUtils.messageToToast = str("vote_failed_unknown_error", responseCode, connection.getResponseMessage()); break; @@ -170,9 +167,18 @@ public class Requester { }).start(); } - public static void setUsername(String username) { + public static void setUsername(String username, Runnable toastRunnable) { try { HttpURLConnection connection = getConnectionFromRoute(Route.CHANGE_USERNAME, SponsorBlockSettings.uuid, username); + int responseCode = connection.getResponseCode(); + + if (responseCode == 200) { + SponsorBlockUtils.messageToToast = str("stats_username_changed"); + } + else { + SponsorBlockUtils.messageToToast = str("stats_username_change_unknown_error", responseCode, connection.getResponseMessage()); + } + new Handler(Looper.getMainLooper()).post(toastRunnable); connection.disconnect(); } catch (Exception ex) { @@ -184,7 +190,6 @@ public class Requester { String url = SPONSORBLOCK_API_URL + route.compile(params).getCompiledRoute(); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod(route.getMethod().name()); - connection.getInputStream().close(); // this is required so it properly establishes the connection when not reading the InputStream in any of the routes return connection; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e561d0a..2e68d5e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -182,18 +182,22 @@ Skip automatically Show a skip button Don\'t do anything + Stats Loading.. SponsorBlock is disabled - Your username: <b>%s</b> + Your username: <b>%s</b> Click to change your username - Submissions: <b>%s</b> - You\'ve saved people from <b>%s</b> segments. - That\'s <b>%s</b> of their lives. Click to see the leaderboard - You\'ve skipped <b>%s</b> segments. - That\'s <b>%s</b>. + Unable to change username: Status: %d %s + Username changed successfully + Submissions: <b>%s</b> + You\'ve saved people from <b>%s</b> segments. + That\'s <b>%s</b> of their lives. Click to see the leaderboard + You\'ve skipped <b>%s</b> segments. + That\'s <b>%s</b>. + About - This app uses the API from Sponsor Block + This app uses the API from SponsorBlock Tap to learn more, and see downloads for other platforms at: sponsor.ajay.app Integration made by JakubWeg Tap to skip @@ -206,7 +210,6 @@ Submitting segment… Unable to vote for segment: Status: %d %s - Can\'t vote for segment.\nRate Limited (Too many from the same user or IP) Can\'t vote for segment.\nA moderator has decided that this segment is correct Voted successfully Voting for segment…