mirror of
https://github.com/YTVanced/Integrations
synced 2024-11-30 15:13:03 +00:00
add proper response handling to setUsername
This commit is contained in:
parent
85665f3f2a
commit
aedb3ab171
3 changed files with 24 additions and 14 deletions
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -182,18 +182,22 @@
|
|||
<string name="skip_automatically">Skip automatically</string>
|
||||
<string name="skip_showbutton">Show a skip button</string>
|
||||
<string name="skip_ignore">Don\'t do anything</string>
|
||||
|
||||
<string name="stats">Stats</string>
|
||||
<string name="stats_loading">Loading..</string>
|
||||
<string name="stats_sb_disabled">SponsorBlock is disabled</string>
|
||||
<string name="stats_username">Your username: <b>%s</b></string>
|
||||
<string name="stats_username" formatted="false">Your username: <b>%s</b></string>
|
||||
<string name="stats_username_change">Click to change your username</string>
|
||||
<string name="stats_submissions">Submissions: <b>%s</b></string>
|
||||
<string name="stats_saved">You\'ve saved people from <b>%s</b> segments.</string>
|
||||
<string name="stats_saved_sum">That\'s <b>%s</b> of their lives. Click to see the leaderboard</string>
|
||||
<string name="stats_self_saved">You\'ve skipped <b>%s</b> segments.</string>
|
||||
<string name="stats_self_saved_sum">That\'s <b>%s</b>.</string>
|
||||
<string name="stats_username_change_unknown_error" formatted="false">Unable to change username: Status: %d %s</string>
|
||||
<string name="stats_username_changed">Username changed successfully</string>
|
||||
<string name="stats_submissions" formatted="false">Submissions: <b>%s</b></string>
|
||||
<string name="stats_saved" formatted="false">You\'ve saved people from <b>%s</b> segments.</string>
|
||||
<string name="stats_saved_sum" formatted="false">That\'s <b>%s</b> of their lives. Click to see the leaderboard</string>
|
||||
<string name="stats_self_saved" formatted="false">You\'ve skipped <b>%s</b> segments.</string>
|
||||
<string name="stats_self_saved_sum" formatted="false">That\'s <b>%s</b>.</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_madeby">Integration made by JakubWeg</string>
|
||||
<string name="tap_skip">Tap to skip</string>
|
||||
|
@ -206,7 +210,6 @@
|
|||
<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_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_succeeded">Voted successfully</string>
|
||||
<string name="vote_started">Voting for segment…</string>
|
||||
|
|
Loading…
Reference in a new issue