add proper response handling to setUsername

This commit is contained in:
caneleex 2021-07-24 18:53:59 +02:00
parent a030e6e280
commit 518572967b
3 changed files with 24 additions and 14 deletions

View File

@ -190,6 +190,7 @@ public abstract class SponsorBlockUtils {
}; };
private static final Runnable toastRunnable = () -> { private static final Runnable toastRunnable = () -> {
Context context = appContext.get(); Context context = appContext.get();
Log.d("cock", String.valueOf(context));
if (context != null && messageToToast != null) if (context != null && messageToToast != null)
Toast.makeText(context, messageToToast, Toast.LENGTH_LONG).show(); Toast.makeText(context, messageToToast, Toast.LENGTH_LONG).show();
}; };
@ -452,7 +453,8 @@ public abstract class SponsorBlockUtils {
preference.setSummary(str("stats_username_change")); preference.setSummary(str("stats_username_change"));
preference.setText(userName); preference.setText(userName);
preference.setOnPreferenceChangeListener((preference1, newUsername) -> { preference.setOnPreferenceChangeListener((preference1, newUsername) -> {
Requester.setUsername((String) newUsername); appContext = new WeakReference<>(context.getApplicationContext());
Requester.setUsername((String) newUsername, toastRunnable);
return false; return false;
}); });
} }

View File

@ -134,9 +134,6 @@ public class Requester {
case 403: case 403:
SponsorBlockUtils.messageToToast = str("vote_failed_forbidden"); SponsorBlockUtils.messageToToast = str("vote_failed_forbidden");
break; break;
case 429:
SponsorBlockUtils.messageToToast = str("vote_failed_rate_limit");
break;
default: default:
SponsorBlockUtils.messageToToast = str("vote_failed_unknown_error", responseCode, connection.getResponseMessage()); SponsorBlockUtils.messageToToast = str("vote_failed_unknown_error", responseCode, connection.getResponseMessage());
break; break;
@ -170,9 +167,18 @@ public class Requester {
}).start(); }).start();
} }
public static void setUsername(String username) { public static void setUsername(String username, Runnable toastRunnable) {
try { try {
HttpURLConnection connection = getConnectionFromRoute(Route.CHANGE_USERNAME, SponsorBlockSettings.uuid, username); 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(); connection.disconnect();
} }
catch (Exception ex) { catch (Exception ex) {
@ -184,7 +190,6 @@ public class Requester {
String url = SPONSORBLOCK_API_URL + route.compile(params).getCompiledRoute(); String url = SPONSORBLOCK_API_URL + route.compile(params).getCompiledRoute();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod(route.getMethod().name()); 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; return connection;
} }

View File

@ -182,18 +182,22 @@
<string name="skip_automatically">Skip automatically</string> <string name="skip_automatically">Skip automatically</string>
<string name="skip_showbutton">Show a skip button</string> <string name="skip_showbutton">Show a skip button</string>
<string name="skip_ignore">Don\'t do anything</string> <string name="skip_ignore">Don\'t do anything</string>
<string name="stats">Stats</string> <string name="stats">Stats</string>
<string name="stats_loading">Loading..</string> <string name="stats_loading">Loading..</string>
<string name="stats_sb_disabled">SponsorBlock is disabled</string> <string name="stats_sb_disabled">SponsorBlock is disabled</string>
<string name="stats_username">Your username: &lt;b&gt;%s&lt;/b&gt;</string> <string name="stats_username" formatted="false">Your username: &lt;b&gt;%s&lt;/b&gt;</string>
<string name="stats_username_change">Click to change your username</string> <string name="stats_username_change">Click to change your username</string>
<string name="stats_submissions">Submissions: &lt;b&gt;%s&lt;/b&gt;</string> <string name="stats_username_change_unknown_error" formatted="false">Unable to change username: Status: %d %s</string>
<string name="stats_saved">You\'ve saved people from &lt;b&gt;%s&lt;/b&gt; segments.</string> <string name="stats_username_changed">Username changed successfully</string>
<string name="stats_saved_sum">That\'s &lt;b&gt;%s&lt;/b&gt; of their lives. Click to see the leaderboard</string> <string name="stats_submissions" formatted="false">Submissions: &lt;b&gt;%s&lt;/b&gt;</string>
<string name="stats_self_saved">You\'ve skipped &lt;b&gt;%s&lt;/b&gt; segments.</string> <string name="stats_saved" formatted="false">You\'ve saved people from &lt;b&gt;%s&lt;/b&gt; segments.</string>
<string name="stats_self_saved_sum">That\'s &lt;b&gt;%s&lt;/b&gt;.</string> <string name="stats_saved_sum" formatted="false">That\'s &lt;b&gt;%s&lt;/b&gt; of their lives. Click to see the leaderboard</string>
<string name="stats_self_saved" formatted="false">You\'ve skipped &lt;b&gt;%s&lt;/b&gt; segments.</string>
<string name="stats_self_saved_sum" formatted="false">That\'s &lt;b&gt;%s&lt;/b&gt;.</string>
<string name="about">About</string> <string name="about">About</string>
<string name="about_api">This app uses the API from Sponsor Block</string> <string name="about_api">This app uses the API from SponsorBlock</string>
<string name="about_api_sum">Tap to learn more, and see downloads for other platforms at: sponsor.ajay.app</string> <string name="about_api_sum">Tap to learn more, and see downloads for other platforms at: sponsor.ajay.app</string>
<string name="about_madeby">Integration made by JakubWeg</string> <string name="about_madeby">Integration made by JakubWeg</string>
<string name="tap_skip">Tap to skip</string> <string name="tap_skip">Tap to skip</string>
@ -206,7 +210,6 @@
<string name="submit_started">Submitting segment…</string> <string name="submit_started">Submitting segment…</string>
<string name="vote_failed_unknown_error" formatted="false">Unable to vote for segment: Status: %d %s</string> <string name="vote_failed_unknown_error" formatted="false">Unable to vote for segment: Status: %d %s</string>
<string name="vote_failed_rate_limit">Can\'t vote for segment.\nRate Limited (Too many from the same user or IP)</string>
<string name="vote_failed_forbidden">Can\'t vote for segment.\nA moderator has decided that this segment is correct</string> <string name="vote_failed_forbidden">Can\'t vote for segment.\nA moderator has decided that this segment is correct</string>
<string name="vote_succeeded">Voted successfully</string> <string name="vote_succeeded">Voted successfully</string>
<string name="vote_started">Voting for segment…</string> <string name="vote_started">Voting for segment…</string>