|
20 | 20 |
|
21 | 21 | public class ReturnYouTubeDislike { |
22 | 22 | 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 | + |
23 | 37 | private static Thread _dislikeFetchThread = null; |
24 | 38 | private static Thread _votingThread = null; |
25 | 39 | private static Registration registration; |
@@ -77,31 +91,29 @@ public static void onComponentCreated(Object conversionContext, AtomicReference< |
77 | 91 | if (!isEnabled) return; |
78 | 92 |
|
79 | 93 | 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 | + |
91 | 102 |
|
92 | 103 | // Have to block the current thread until fetching is done |
93 | 104 | // There's no known way to edit the text after creation yet |
94 | 105 | if (_dislikeFetchThread != null) _dislikeFetchThread.join(); |
95 | 106 |
|
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); |
99 | 111 | } catch (Exception ex) { |
100 | 112 | LogHelper.printException(ReturnYouTubeDislike.class, "Error while trying to set dislikes text", ex); |
101 | 113 | } |
102 | 114 | } |
103 | 115 |
|
104 | | - public static void sendVote(int vote) { |
| 116 | + public static void sendVote(Vote vote) { |
105 | 117 | if (!isEnabled) return; |
106 | 118 |
|
107 | 119 | Context context = ReVancedUtils.getContext(); |
@@ -129,16 +141,23 @@ public static void sendVote(int vote) { |
129 | 141 | _votingThread.start(); |
130 | 142 | } |
131 | 143 |
|
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 | + ); |
135 | 156 |
|
136 | 157 | // 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)); |
142 | 161 |
|
143 | 162 | textRef.set(newString); |
144 | 163 | } |
|
0 commit comments