SB request fixes

This commit is contained in:
caneleex 2022-01-24 13:45:07 +01:00
parent da23cb732d
commit 6a08ef6742
4 changed files with 63 additions and 53 deletions

View File

@ -3,11 +3,10 @@ package fi.vanced.libraries.youtube.whitelisting.requests;
import static fi.razerman.youtube.XGlobals.debug; import static fi.razerman.youtube.XGlobals.debug;
import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId; import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId;
import static fi.vanced.libraries.youtube.ui.AdButton.TAG; import static fi.vanced.libraries.youtube.ui.AdButton.TAG;
import static fi.vanced.utils.VancedUtils.runOnMainThread;
import static pl.jakubweg.StringRef.str; import static pl.jakubweg.StringRef.str;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
@ -89,10 +88,6 @@ public class WhitelistRequester {
// helpers // helpers
private static void runOnMainThread(Runnable runnable) {
new Handler(Looper.getMainLooper()).post(runnable);
}
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException { private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {
return Requester.getConnectionFromRoute(YT_API_URL, route, params); return Requester.getConnectionFromRoute(YT_API_URL, route, params);
} }

View File

@ -2,6 +2,8 @@ package fi.vanced.utils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application; import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application;
@ -40,4 +42,8 @@ public class VancedUtils {
} }
return count; return count;
} }
public static void runOnMainThread(Runnable runnable) {
new Handler(Looper.getMainLooper()).post(runnable);
}
} }

View File

@ -223,10 +223,10 @@ public abstract class SponsorBlockUtils {
appContext = new WeakReference<>(context.getApplicationContext()); appContext = new WeakReference<>(context.getApplicationContext());
switch (voteOptions[which1]) { switch (voteOptions[which1]) {
case UPVOTE: case UPVOTE:
voteForSegment(segment, VoteOption.UPVOTE, appContext.get(), toastRunnable); voteForSegment(segment, VoteOption.UPVOTE, appContext.get());
break; break;
case DOWNVOTE: case DOWNVOTE:
voteForSegment(segment, VoteOption.DOWNVOTE, appContext.get(), toastRunnable); voteForSegment(segment, VoteOption.DOWNVOTE, appContext.get());
break; break;
case CATEGORY_CHANGE: case CATEGORY_CHANGE:
onNewCategorySelect(segment, context); onNewCategorySelect(segment, context);
@ -401,7 +401,7 @@ public abstract class SponsorBlockUtils {
new AlertDialog.Builder(context) new AlertDialog.Builder(context)
.setTitle(str("new_segment_choose_category")) .setTitle(str("new_segment_choose_category"))
.setItems(titles, (dialog, which) -> voteForSegment(segment, VoteOption.CATEGORY_CHANGE, appContext.get(), toastRunnable, values[which].key)) .setItems(titles, (dialog, which) -> voteForSegment(segment, VoteOption.CATEGORY_CHANGE, appContext.get(), values[which].key))
.show(); .show();
} }
@ -505,7 +505,7 @@ public abstract class SponsorBlockUtils {
preference.setText(userName); preference.setText(userName);
preference.setOnPreferenceChangeListener((preference1, newUsername) -> { preference.setOnPreferenceChangeListener((preference1, newUsername) -> {
appContext = new WeakReference<>(context.getApplicationContext()); appContext = new WeakReference<>(context.getApplicationContext());
SBRequester.setUsername((String) newUsername, toastRunnable); SBRequester.setUsername((String) newUsername, preference, toastRunnable);
return false; return false;
}); });
} }

View File

@ -1,12 +1,13 @@
package pl.jakubweg.requests; package pl.jakubweg.requests;
import static android.text.Html.fromHtml;
import static fi.vanced.utils.VancedUtils.runOnMainThread;
import static pl.jakubweg.SponsorBlockUtils.timeWithoutSegments; import static pl.jakubweg.SponsorBlockUtils.timeWithoutSegments;
import static pl.jakubweg.SponsorBlockUtils.videoHasSegments; import static pl.jakubweg.SponsorBlockUtils.videoHasSegments;
import static pl.jakubweg.StringRef.str; import static pl.jakubweg.StringRef.str;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.preference.EditTextPreference;
import android.os.Looper;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.widget.Toast; import android.widget.Toast;
@ -92,7 +93,7 @@ public class SBRequester {
SponsorBlockUtils.messageToToast = str("submit_failed_unknown_error", responseCode, connection.getResponseMessage()); SponsorBlockUtils.messageToToast = str("submit_failed_unknown_error", responseCode, connection.getResponseMessage());
break; break;
} }
new Handler(Looper.getMainLooper()).post(toastRunnable); runOnMainThread(toastRunnable);
connection.disconnect(); connection.disconnect();
} }
catch (Exception ex) { catch (Exception ex) {
@ -110,36 +111,38 @@ public class SBRequester {
} }
} }
public static void voteForSegment(SponsorSegment segment, VoteOption voteOption, Context context, Runnable toastRunnable, String... args) { public static void voteForSegment(SponsorSegment segment, VoteOption voteOption, Context context, String... args) {
try { new Thread(() -> {
String segmentUuid = segment.UUID; try {
String uuid = SponsorBlockSettings.uuid; String segmentUuid = segment.UUID;
String vote = Integer.toString(voteOption == VoteOption.UPVOTE ? 1 : 0); String uuid = SponsorBlockSettings.uuid;
String vote = Integer.toString(voteOption == VoteOption.UPVOTE ? 1 : 0);
Toast.makeText(context, str("vote_started"), Toast.LENGTH_SHORT).show(); runOnMainThread(() -> Toast.makeText(context, str("vote_started"), Toast.LENGTH_SHORT).show());
HttpURLConnection connection = voteOption == VoteOption.CATEGORY_CHANGE HttpURLConnection connection = voteOption == VoteOption.CATEGORY_CHANGE
? getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_CATEGORY, segmentUuid, uuid, args[0]) ? getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_CATEGORY, segmentUuid, uuid, args[0])
: getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_QUALITY, segmentUuid, uuid, vote); : getConnectionFromRoute(SBRoutes.VOTE_ON_SEGMENT_QUALITY, segmentUuid, uuid, vote);
int responseCode = connection.getResponseCode(); int responseCode = connection.getResponseCode();
switch (responseCode) { switch (responseCode) {
case 200: case 200:
SponsorBlockUtils.messageToToast = str("vote_succeeded"); SponsorBlockUtils.messageToToast = str("vote_succeeded");
break; break;
case 403: case 403:
SponsorBlockUtils.messageToToast = str("vote_failed_forbidden"); SponsorBlockUtils.messageToToast = str("vote_failed_forbidden");
break; 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;
}
runOnMainThread(() -> Toast.makeText(context, SponsorBlockUtils.messageToToast, Toast.LENGTH_LONG).show());
connection.disconnect();
} }
new Handler(Looper.getMainLooper()).post(toastRunnable); catch (Exception ex) {
connection.disconnect(); ex.printStackTrace();
} }
catch (Exception ex) { }).start();
ex.printStackTrace();
}
} }
public static void retrieveUserStats(PreferenceCategory category, Preference loadingPreference) { public static void retrieveUserStats(PreferenceCategory category, Preference loadingPreference) {
@ -161,23 +164,29 @@ public class SBRequester {
}).start(); }).start();
} }
public static void setUsername(String username, Runnable toastRunnable) { public static void setUsername(String username, EditTextPreference preference, Runnable toastRunnable) {
try { new Thread(() -> {
HttpURLConnection connection = getConnectionFromRoute(SBRoutes.CHANGE_USERNAME, SponsorBlockSettings.uuid, username); try {
int responseCode = connection.getResponseCode(); HttpURLConnection connection = getConnectionFromRoute(SBRoutes.CHANGE_USERNAME, SponsorBlockSettings.uuid, username);
int responseCode = connection.getResponseCode();
if (responseCode == 200) { if (responseCode == 200) {
SponsorBlockUtils.messageToToast = str("stats_username_changed"); SponsorBlockUtils.messageToToast = str("stats_username_changed");
runOnMainThread(() -> {
preference.setTitle(fromHtml(str("stats_username", username)));
preference.setText(username);
});
}
else {
SponsorBlockUtils.messageToToast = str("stats_username_change_unknown_error", responseCode, connection.getResponseMessage());
}
runOnMainThread(toastRunnable);
connection.disconnect();
} }
else { catch (Exception ex) {
SponsorBlockUtils.messageToToast = str("stats_username_change_unknown_error", responseCode, connection.getResponseMessage()); ex.printStackTrace();
} }
new Handler(Looper.getMainLooper()).post(toastRunnable); }).start();
connection.disconnect();
}
catch (Exception ex) {
ex.printStackTrace();
}
} }
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException { private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {