Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit a92c932

Browse files
committed
feat(youtube/return-youtube-dislike): compatibility for old and new button layout
1 parent 3698a50 commit a92c932

5 files changed

Lines changed: 61 additions & 36 deletions

File tree

app/src/main/java/app/revanced/integrations/patches/LithoFilterPatch.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@
1313

1414
import app.revanced.integrations.settings.SettingsEnum;
1515
import app.revanced.integrations.utils.LogHelper;
16+
import app.revanced.integrations.utils.ReVancedUtils;
1617

1718
/**
1819
* Helper functions.
1920
*/
2021
final class Extensions {
21-
static boolean containsAny(final String value, final String... targets) {
22-
for (String string : targets)
23-
if (value.contains(string)) return true;
24-
return false;
25-
}
26-
2722
static boolean any(LithoBlockRegister register, String path) {
2823
for (var rule : register) {
2924
if (!rule.isEnabled()) continue;
@@ -76,7 +71,7 @@ public boolean isEnabled() {
7671
}
7772

7873
public BlockResult check(final String string) {
79-
return new BlockResult(setting, string != null && Extensions.containsAny(string, blocks));
74+
return new BlockResult(setting, string != null && ReVancedUtils.containsAny(string, blocks));
8075
}
8176
}
8277

@@ -262,7 +257,7 @@ public GeneralBytecodeAdsPatch() {
262257

263258
public boolean filter(final String path, final String identifier) {
264259
// Do not block on these
265-
if (Extensions.containsAny(path,
260+
if (ReVancedUtils.containsAny(path,
266261
"home_video_with_context",
267262
"related_video_with_context",
268263
"search_video_with_context",

app/src/main/java/app/revanced/integrations/patches/ReturnYouTubeDislikePatch.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ public static void onComponentCreated(Object conversionContext, AtomicReference<
2525

2626
/**
2727
* Called when the like/dislike button is clicked
28+
*
2829
* @param vote -1 (dislike), 0 (none) or 1 (like)
2930
*/
3031
public static void sendVote(int vote) {
31-
ReturnYouTubeDislike.sendVote(vote);
32+
for (ReturnYouTubeDislike.Vote v : ReturnYouTubeDislike.Vote.values()) {
33+
if (v.value == vote) {
34+
ReturnYouTubeDislike.sendVote(v);
35+
return;
36+
}
37+
}
3238
}
3339
}

app/src/main/java/app/revanced/integrations/returnyoutubedislike/ReturnYouTubeDislike.java

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020

2121
public class ReturnYouTubeDislike {
2222
private static boolean isEnabled;
23+
private static boolean segmentedButton;
24+
25+
public enum Vote {
26+
LIKE(1),
27+
DISLIKE(-1),
28+
LIKE_REMOVE(0);
29+
30+
public int value;
31+
32+
Vote(int value) {
33+
this.value = value;
34+
}
35+
}
36+
2337
private static Thread _dislikeFetchThread = null;
2438
private static Thread _votingThread = null;
2539
private static Registration registration;
@@ -77,31 +91,29 @@ public static void onComponentCreated(Object conversionContext, AtomicReference<
7791
if (!isEnabled) return;
7892

7993
try {
80-
// Contains a pathBuilder string, used to distinguish from other litho components:
81-
// video_action_bar.eml|27b56b54d5dcba20|video_action_bar_unwrapper.eml|c5a1d399b660e52e|CellType
82-
// |ScrollableContainerType|ContainerType|ContainerType|dislike_button.eml|966ee2cd7db5e29f
83-
// |video_actipathBuilder=video_action_bar.eml|27b56b54d5dcba20|video_action_bar_unwrapper.eml
84-
// |c5a1d399b660e52e|CellType|ScrollableContainerType|ContainerType|ContainerType|dislike_button.eml
85-
// |966ee2cd7db5e29f|video_action_toggle_button.eml|8fd9d44a8e3c9162|video_action_button.eml
86-
// |9dd3b4b44979c3af|ContainerType|TextType|on_toggle_button.eml|8fd9d44a8e3c9162|video_action_button.eml
87-
// |9dd3b4b44979c3af|ContainerType|TextType|
88-
if (!conversionContext.toString().contains("|dislike_button.eml|")) return;
89-
90-
LogHelper.debug(ReturnYouTubeDislike.class, "dislike button was created");
94+
var conversionContextString = conversionContext.toString();
95+
96+
// Check for new component
97+
if (conversionContextString.contains("|segmented_like_dislike_button.eml|"))
98+
segmentedButton = true;
99+
else if (!conversionContextString.contains("|dislike_button.eml|"))
100+
return;
101+
91102

92103
// Have to block the current thread until fetching is done
93104
// There's no known way to edit the text after creation yet
94105
if (_dislikeFetchThread != null) _dislikeFetchThread.join();
95106

96-
if (dislikeCount != null) {
97-
updateDislikeText(textRef, formatDislikes(dislikeCount));
98-
}
107+
if (dislikeCount == null) return;
108+
109+
updateDislike(textRef, dislikeCount);
110+
LogHelper.debug(ReturnYouTubeDislike.class, "Updated text on component" + conversionContextString);
99111
} catch (Exception ex) {
100112
LogHelper.printException(ReturnYouTubeDislike.class, "Error while trying to set dislikes text", ex);
101113
}
102114
}
103115

104-
public static void sendVote(int vote) {
116+
public static void sendVote(Vote vote) {
105117
if (!isEnabled) return;
106118

107119
Context context = ReVancedUtils.getContext();
@@ -129,16 +141,23 @@ public static void sendVote(int vote) {
129141
_votingThread.start();
130142
}
131143

132-
private static void updateDislikeText(AtomicReference<Object> textRef, String text) {
133-
SpannableString oldString = (SpannableString) textRef.get();
134-
SpannableString newString = new SpannableString(text);
144+
private static void updateDislike(AtomicReference<Object> textRef, Integer dislikeCount) {
145+
SpannableString oldSpannableString = (SpannableString) textRef.get();
146+
147+
// parse the buttons string
148+
// if the button is segmented, only get the like count as a string
149+
var oldButtonString = oldSpannableString.toString();
150+
if (segmentedButton) oldButtonString = oldButtonString.split(" \\| ")[0];
151+
152+
var dislikeString = formatDislikes(dislikeCount);
153+
SpannableString newString = new SpannableString(
154+
segmentedButton ? (oldButtonString + " | " + dislikeString) : dislikeString
155+
);
135156

136157
// Copy style (foreground color, etc) to new string
137-
Object[] spans = oldString.getSpans(0, oldString.length(), Object.class);
138-
for (Object span : spans) {
139-
int flags = oldString.getSpanFlags(span);
140-
newString.setSpan(span, 0, newString.length(), flags);
141-
}
158+
Object[] spans = oldSpannableString.getSpans(0, oldSpannableString.length(), Object.class);
159+
for (Object span : spans)
160+
newString.setSpan(span, 0, newString.length(), oldSpannableString.getSpanFlags(span));
142161

143162
textRef.set(newString);
144163
}

app/src/main/java/app/revanced/integrations/returnyoutubedislike/Voting.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public Voting(Registration registration) {
1010
this.registration = registration;
1111
}
1212

13-
public boolean sendVote(String videoId, int vote) {
13+
public boolean sendVote(String videoId, ReturnYouTubeDislike.Vote vote) {
1414
String userId = registration.getUserId();
1515
LogHelper.debug(Voting.class, "Trying to vote the following video: " + videoId + " with vote " + vote + " and userId: " + userId);
16-
return ReturnYouTubeDislikeApi.sendVote(videoId, userId, vote);
16+
return ReturnYouTubeDislikeApi.sendVote(videoId, userId, vote.value);
1717
}
1818
}

app/src/main/java/app/revanced/integrations/utils/ReVancedUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ public class ReVancedUtils {
1212
private static PlayerType env;
1313
public static boolean newVideo = false;
1414

15-
//Used by Integrations patch
1615
public static Context context;
17-
//Used by Integrations patch
16+
17+
public static boolean containsAny(final String value, final String... targets) {
18+
for (String string : targets)
19+
if (value.contains(string)) return true;
20+
return false;
21+
}
1822

1923
public static void setNewVideo(boolean started) {
2024
LogHelper.debug(ReVancedUtils.class, "New video started: " + started);
2125
newVideo = started;
2226
}
27+
2328
public static boolean isNewVideoStarted() {
2429
return newVideo;
2530
}

0 commit comments

Comments
 (0)