mirror of
https://github.com/revanced/revanced-integrations.git
synced 2025-01-05 17:45:49 +01:00
feat: return-youtube-dislikes
patch (#81)
* feat: update dislike button text * refactor(ryd): remove unused code * refactor: create patch class for ryd * refactor: move VideoInformation
This commit is contained in:
parent
508f49622c
commit
2d513b5100
@ -0,0 +1,33 @@
|
||||
package app.revanced.integrations.patches;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import app.revanced.integrations.ryd.ReturnYouTubeDislikes;
|
||||
|
||||
/**
|
||||
* Used by app.revanced.patches.youtube.layout.returnyoutubedislikes.patch.RYDPatch
|
||||
*/
|
||||
public class ReturnYouTubeDislikesPatch {
|
||||
|
||||
/**
|
||||
* Called when the video id changes
|
||||
*/
|
||||
public static void newVideoLoaded(String videoId) {
|
||||
ReturnYouTubeDislikes.newVideoLoaded(videoId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a litho text component is created
|
||||
*/
|
||||
public static void onComponentCreated(Object conversionContext, AtomicReference<Object> textRef) {
|
||||
ReturnYouTubeDislikes.onComponentCreated(conversionContext, textRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the like/dislike button is clicked
|
||||
* @param vote -1 (dislike), 0 (none) or 1 (like)
|
||||
*/
|
||||
public static void sendVote(int vote) {
|
||||
ReturnYouTubeDislikes.sendVote(vote);
|
||||
}
|
||||
}
|
@ -1,35 +1,29 @@
|
||||
package app.revanced.integrations.ryd;
|
||||
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.dislikeCount;
|
||||
import static app.revanced.integrations.utils.ReVancedUtils.getIdentifier;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.dislikeCount;
|
||||
|
||||
import android.content.Context;
|
||||
import android.icu.text.CompactDecimalFormat;
|
||||
import android.os.Build;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.text.SpannableString;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import app.revanced.integrations.ryd.requests.RYDRequester;
|
||||
import app.revanced.integrations.settings.SettingsEnum;
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.ryd.requests.RYDRequester;
|
||||
import app.revanced.integrations.utils.ReVancedUtils;
|
||||
import app.revanced.integrations.utils.SharedPrefHelper;
|
||||
|
||||
public class ReturnYouTubeDislikes {
|
||||
public static boolean isEnabled;
|
||||
private static View _dislikeView = null;
|
||||
private static boolean isEnabled;
|
||||
private static Thread _dislikeFetchThread = null;
|
||||
private static Thread _votingThread = null;
|
||||
private static Registration registration;
|
||||
private static Voting voting;
|
||||
private static boolean likeActive;
|
||||
private static boolean dislikeActive;
|
||||
private static int votingValue = 0; // 1 = like, -1 = dislike, 0 = no vote
|
||||
private static CompactDecimalFormat compactNumberFormatter;
|
||||
|
||||
static {
|
||||
@ -60,7 +54,6 @@ public class ReturnYouTubeDislikes {
|
||||
}
|
||||
}
|
||||
|
||||
//Was called in SB->player->VideoInformation->setCurrentVideoId(final String videoId) before, has to be called on its own at the same place now.
|
||||
public static void newVideoLoaded(String videoId) {
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "newVideoLoaded - " + videoId);
|
||||
|
||||
@ -80,162 +73,34 @@ public class ReturnYouTubeDislikes {
|
||||
_dislikeFetchThread.start();
|
||||
}
|
||||
|
||||
// Call to this needs to be injected in YT code
|
||||
public static void setLikeTag(View view) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
setTag(view, "like");
|
||||
}
|
||||
|
||||
public static void setLikeTag(View view, boolean active) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
likeActive = active;
|
||||
if (likeActive) {
|
||||
votingValue = 1;
|
||||
}
|
||||
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "Like tag active " + likeActive);
|
||||
setTag(view, "like");
|
||||
}
|
||||
|
||||
// Call to this needs to be injected in YT code
|
||||
public static void setDislikeTag(View view) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
_dislikeView = view;
|
||||
setTag(view, "dislike");
|
||||
}
|
||||
|
||||
public static void setDislikeTag(View view, boolean active) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
dislikeActive = active;
|
||||
if (dislikeActive) {
|
||||
votingValue = -1;
|
||||
}
|
||||
_dislikeView = view;
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "Dislike tag active " + dislikeActive);
|
||||
setTag(view, "dislike");
|
||||
}
|
||||
|
||||
// Call to this needs to be injected in YT code
|
||||
public static CharSequence onSetText(View view, CharSequence originalText) {
|
||||
if (!isEnabled) return originalText;
|
||||
return handleOnSetText(view, originalText);
|
||||
}
|
||||
|
||||
// Call to this needs to be injected in YT code
|
||||
public static void onClick(View view, boolean inactive) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
handleOnClick(view, inactive);
|
||||
}
|
||||
|
||||
private static CharSequence handleOnSetText(View view, CharSequence originalText) {
|
||||
if (!isEnabled) return originalText;
|
||||
|
||||
try {
|
||||
CharSequence tag = (CharSequence) view.getTag();
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "handleOnSetText - " + tag + " - original text - " + originalText);
|
||||
if (tag == null) return originalText;
|
||||
|
||||
if (tag == "like") {
|
||||
return originalText;
|
||||
} else if (tag == "dislike") {
|
||||
return dislikeCount != null ? formatDislikes(dislikeCount) : originalText;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LogHelper.printException(ReturnYouTubeDislikes.class, "Error while handling the setText", ex);
|
||||
}
|
||||
|
||||
return originalText;
|
||||
}
|
||||
|
||||
public static void trySetDislikes(String dislikeCount) {
|
||||
public static void onComponentCreated(Object conversionContext, AtomicReference<Object> textRef) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
try {
|
||||
// Try to set normal video dislike count
|
||||
if (_dislikeView == null) {
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "_dislikeView was null");
|
||||
return;
|
||||
}
|
||||
// Contains a pathBuilder string, used to distinguish from other litho components
|
||||
if (!conversionContext.toString().contains("dislike_button")) return;
|
||||
|
||||
View buttonView = _dislikeView.findViewById(getIdentifier("button_text", "id"));
|
||||
if (buttonView == null) {
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "buttonView was null");
|
||||
return;
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "dislike button was created");
|
||||
|
||||
// Have to block the current thread until fetching is done
|
||||
// There's no known way to edit the text after creation yet
|
||||
if (_dislikeFetchThread != null) _dislikeFetchThread.join();
|
||||
|
||||
if (dislikeCount != null) {
|
||||
updateDislikeText(textRef, formatDislikes(dislikeCount));
|
||||
}
|
||||
TextView button = (TextView) buttonView;
|
||||
button.setText(dislikeCount);
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "trySetDislikes - " + dislikeCount);
|
||||
} catch (Exception ex) {
|
||||
LogHelper.printException(ReturnYouTubeDislikes.class, "Error while trying to set dislikes text", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleOnClick(View view, boolean previousState) {
|
||||
Context context = ReVancedUtils.getContext();
|
||||
if (!isEnabled || SharedPrefHelper.getBoolean(Objects.requireNonNull(context), SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true))
|
||||
return;
|
||||
|
||||
try {
|
||||
String tag = (String) view.getTag();
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "handleOnClick - " + tag + " - previousState - " + previousState);
|
||||
if (tag == null) return;
|
||||
|
||||
// If active status was removed, vote should be none
|
||||
if (previousState) {
|
||||
votingValue = 0;
|
||||
}
|
||||
if (tag.equals("like")) {
|
||||
|
||||
// Like was activated
|
||||
if (!previousState) {
|
||||
votingValue = 1;
|
||||
likeActive = true;
|
||||
} else {
|
||||
likeActive = false;
|
||||
}
|
||||
|
||||
// Like was activated and dislike was previously activated
|
||||
if (!previousState && dislikeActive) {
|
||||
dislikeCount--;
|
||||
trySetDislikes(formatDislikes(dislikeCount));
|
||||
}
|
||||
dislikeActive = false;
|
||||
} else if (tag.equals("dislike")) {
|
||||
likeActive = false;
|
||||
|
||||
// Dislike was activated
|
||||
if (!previousState) {
|
||||
votingValue = -1;
|
||||
dislikeActive = true;
|
||||
dislikeCount++;
|
||||
}
|
||||
// Dislike was removed
|
||||
else {
|
||||
dislikeActive = false;
|
||||
dislikeCount--;
|
||||
}
|
||||
trySetDislikes(formatDislikes(dislikeCount));
|
||||
} else {
|
||||
// Unknown tag
|
||||
return;
|
||||
}
|
||||
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "New vote status - " + votingValue);
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "Like button " + likeActive + " | Dislike button " + dislikeActive);
|
||||
sendVote(votingValue);
|
||||
} catch (Exception ex) {
|
||||
LogHelper.printException(ReturnYouTubeDislikes.class, "Error while handling the onClick", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendVote(int vote) {
|
||||
public static void sendVote(int vote) {
|
||||
if (!isEnabled) return;
|
||||
|
||||
Context context = ReVancedUtils.getContext();
|
||||
if (SharedPrefHelper.getBoolean(Objects.requireNonNull(context), SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true))
|
||||
return;
|
||||
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "sending vote - " + vote + " for video " + currentVideoId);
|
||||
try {
|
||||
if (_votingThread != null && _votingThread.getState() != Thread.State.TERMINATED) {
|
||||
@ -257,22 +122,21 @@ public class ReturnYouTubeDislikes {
|
||||
_votingThread.start();
|
||||
}
|
||||
|
||||
private static void setTag(View view, String tag) {
|
||||
if (!isEnabled) return;
|
||||
private static void updateDislikeText(AtomicReference<Object> textRef, String text) {
|
||||
SpannableString oldString = (SpannableString) textRef.get();
|
||||
SpannableString newString = new SpannableString(text);
|
||||
|
||||
try {
|
||||
if (view == null) {
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "View was empty");
|
||||
return;
|
||||
}
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "setTag - " + tag);
|
||||
view.setTag(tag);
|
||||
} catch (Exception ex) {
|
||||
LogHelper.printException(ReturnYouTubeDislikes.class, "Error while trying to set tag to view", ex);
|
||||
// Copy style (foreground color, etc) to new string
|
||||
Object[] spans = oldString.getSpans(0, oldString.length(), Object.class);
|
||||
for (Object span : spans) {
|
||||
int flags = oldString.getSpanFlags(span);
|
||||
newString.setSpan(span, 0, newString.length(), flags);
|
||||
}
|
||||
|
||||
textRef.set(newString);
|
||||
}
|
||||
|
||||
public static String formatDislikes(int dislikes) {
|
||||
private static String formatDislikes(int dislikes) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && compactNumberFormatter != null) {
|
||||
final String formatted = compactNumberFormatter.format(dislikes);
|
||||
LogHelper.debug(ReturnYouTubeDislikes.class, "Formatting dislikes - " + dislikes + " - " + formatted);
|
||||
|
@ -1,11 +1,8 @@
|
||||
package app.revanced.integrations.ryd.requests;
|
||||
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.dislikeCount;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.dislikeCount;
|
||||
import static app.revanced.integrations.whitelist.requests.Requester.parseJson;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@ -16,7 +13,6 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.ryd.Registration;
|
||||
import app.revanced.integrations.ryd.ReturnYouTubeDislikes;
|
||||
import app.revanced.integrations.whitelist.requests.Requester;
|
||||
import app.revanced.integrations.whitelist.requests.Route;
|
||||
|
||||
@ -33,13 +29,8 @@ public class RYDRequester {
|
||||
connection.setConnectTimeout(5 * 1000);
|
||||
if (connection.getResponseCode() == 200) {
|
||||
JSONObject json = getJSONObject(connection);
|
||||
int dislikes = json.getInt("dislikes");
|
||||
dislikeCount = dislikes;
|
||||
dislikeCount = json.getInt("dislikes");
|
||||
LogHelper.debug(RYDRequester.class, "dislikes fetched - " + dislikeCount);
|
||||
|
||||
|
||||
// Set the dislikes
|
||||
new Handler(Looper.getMainLooper()).post(() -> ReturnYouTubeDislikes.trySetDislikes(ReturnYouTubeDislikes.formatDislikes(dislikes)));
|
||||
} else {
|
||||
LogHelper.debug(RYDRequester.class, "dislikes fetch response was " + connection.getResponseCode());
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import java.util.TimerTask;
|
||||
|
||||
import app.revanced.integrations.settings.SettingsEnum;
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.sponsorblock.player.VideoInformation;
|
||||
import app.revanced.integrations.videoplayer.VideoInformation;
|
||||
import app.revanced.integrations.whitelist.Whitelist;
|
||||
import app.revanced.integrations.sponsorblock.objects.SponsorSegment;
|
||||
import app.revanced.integrations.sponsorblock.requests.SBRequester;
|
||||
|
@ -7,6 +7,7 @@ import static app.revanced.integrations.sponsorblock.StringRef.str;
|
||||
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.utils.ReVancedUtils;
|
||||
import app.revanced.integrations.videoplayer.VideoInformation;
|
||||
|
||||
public class VideoHelpers {
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package app.revanced.integrations.sponsorblock.player.ui;
|
||||
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.sponsorblock.StringRef.str;
|
||||
|
||||
import android.content.Context;
|
||||
@ -10,7 +10,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.sponsorblock.player.VideoInformation;
|
||||
import app.revanced.integrations.videoplayer.VideoInformation;
|
||||
import app.revanced.integrations.whitelist.Whitelist;
|
||||
import app.revanced.integrations.whitelist.WhitelistType;
|
||||
import app.revanced.integrations.whitelist.requests.WhitelistRequester;
|
||||
|
@ -9,9 +9,8 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import app.revanced.integrations.settings.SettingsEnum;
|
||||
import app.revanced.integrations.sponsorblock.player.VideoInformation;
|
||||
import app.revanced.integrations.videoplayer.VideoInformation;
|
||||
import app.revanced.integrations.utils.ReVancedUtils;
|
||||
import app.revanced.integrations.sponsorblock.SponsorBlockSettings;
|
||||
import app.revanced.integrations.sponsorblock.SponsorBlockUtils;
|
||||
|
||||
public class SBBrowserButton extends SlimButton {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package app.revanced.integrations.sponsorblock.player.ui;
|
||||
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.sponsorblock.StringRef.str;
|
||||
|
||||
import android.content.Context;
|
||||
@ -10,7 +10,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.sponsorblock.player.VideoInformation;
|
||||
import app.revanced.integrations.videoplayer.VideoInformation;
|
||||
import app.revanced.integrations.whitelist.Whitelist;
|
||||
import app.revanced.integrations.whitelist.WhitelistType;
|
||||
import app.revanced.integrations.whitelist.requests.WhitelistRequester;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package app.revanced.integrations.sponsorblock.player;
|
||||
package app.revanced.integrations.videoplayer;
|
||||
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package app.revanced.integrations.whitelist;
|
||||
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.channelName;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.channelName;
|
||||
import static app.revanced.integrations.sponsorblock.player.ui.SlimButtonContainer.adBlockButton;
|
||||
import static app.revanced.integrations.sponsorblock.player.ui.SlimButtonContainer.sbWhitelistButton;
|
||||
import static app.revanced.integrations.sponsorblock.StringRef.str;
|
||||
@ -21,7 +21,7 @@ import java.util.Map;
|
||||
import app.revanced.integrations.settings.SettingsEnum;
|
||||
import app.revanced.integrations.utils.LogHelper;
|
||||
import app.revanced.integrations.sponsorblock.player.ChannelModel;
|
||||
import app.revanced.integrations.sponsorblock.player.VideoInformation;
|
||||
import app.revanced.integrations.videoplayer.VideoInformation;
|
||||
import app.revanced.integrations.utils.ReVancedUtils;
|
||||
import app.revanced.integrations.utils.SharedPrefHelper;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package app.revanced.integrations.whitelist.requests;
|
||||
|
||||
import static app.revanced.integrations.sponsorblock.player.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.videoplayer.VideoInformation.currentVideoId;
|
||||
import static app.revanced.integrations.utils.ReVancedUtils.runOnMainThread;
|
||||
import static app.revanced.integrations.sponsorblock.StringRef.str;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user