fix(YouTube - ReturnYouTubeDislike): Improve layout padding (#517)

This commit is contained in:
LisoUseInAIKyrios 2023-11-18 21:10:26 +02:00 committed by GitHub
parent 411b0f2ccc
commit ba15aba063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 50 deletions

View File

@ -4,7 +4,6 @@ import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.ShapeDrawable;
import android.os.Build; import android.os.Build;
import android.text.*; import android.text.*;
import android.view.Gravity;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -310,42 +309,50 @@ public class ReturnYouTubeDislikePatch {
} }
/** /**
* Remove Rolling Number text view modifications made by this patch. * Injection point.
* Required as it appears text views can be reused for other rolling numbers (view count, upload time, etc). *
* Called for all usage of Rolling Number.
* Modifies the measured String text width to include the left separator and padding, if needed.
*/ */
private static void removeRollingNumberPatchChanges(TextView view) { public static float onRollingNumberMeasured(String text, float measuredTextWidth) {
if (view.getCompoundDrawablePadding() != 0) { try {
LogHelper.printDebug(() -> "Removing rolling number styling from TextView"); if (SettingsEnum.RYD_ENABLED.getBoolean() && !SettingsEnum.RYD_COMPACT_LAYOUT.getBoolean()) {
view.setCompoundDrawablePadding(0); if (ReturnYouTubeDislike.isPreviouslyCreatedSegmentedSpan(text)) {
view.setCompoundDrawables(null, null, null, null); return measuredTextWidth + ReturnYouTubeDislike.leftSeparatorBounds.right
view.setGravity(Gravity.NO_GRAVITY); + ReturnYouTubeDislike.leftSeparatorShapePaddingPixels;
view.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT); }
view.setSingleLine(false); }
} catch (Exception ex) {
LogHelper.printException(() -> "onRollingNumberMeasured failure", ex);
} }
return measuredTextWidth;
} }
/** /**
* Add Rolling Number text view modifications. * Add Rolling Number text view modifications.
*/ */
private static void addRollingNumberPatchChanges(TextView view) { private static void addRollingNumberPatchChanges(TextView view) {
// YouTube Rolling Numbers do not use compound drawables or drawable padding.
if (view.getCompoundDrawablePadding() == 0) { if (view.getCompoundDrawablePadding() == 0) {
LogHelper.printDebug(() -> "Adding rolling number styling to TextView"); LogHelper.printDebug(() -> "Adding rolling number TextView changes");
// YouTube Rolling Numbers do not use compound drawables or drawable padding. ShapeDrawable leftSeparator = ReturnYouTubeDislike.getLeftSeparatorDrawable();
// view.setCompoundDrawables(leftSeparator, null, null, null);
// Single line mode prevents entire words from being entirely clipped,
// and instead only clips the portion of text that runs off.
// The text should not clip due to the empty end padding,
// but use the feature just in case.
view.setSingleLine(true);
// Center align to distribute the horizontal padding.
view.setGravity(Gravity.CENTER);
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
ShapeDrawable shapeDrawable = ReturnYouTubeDislike.getLeftSeparatorDrawable();
view.setCompoundDrawables(shapeDrawable, null, null, null);
view.setCompoundDrawablePadding(ReturnYouTubeDislike.leftSeparatorShapePaddingPixels); view.setCompoundDrawablePadding(ReturnYouTubeDislike.leftSeparatorShapePaddingPixels);
} }
} }
/**
* Remove Rolling Number text view modifications made by this patch.
* Required as it appears text views can be reused for other rolling numbers (view count, upload time, etc).
*/
private static void removeRollingNumberPatchChanges(TextView view) {
if (view.getCompoundDrawablePadding() != 0) {
LogHelper.printDebug(() -> "Removing rolling number TextView changes");
view.setCompoundDrawablePadding(0);
view.setCompoundDrawables(null, null, null, null);
}
}
/** /**
* Injection point. * Injection point.
*/ */
@ -372,14 +379,7 @@ public class ReturnYouTubeDislikePatch {
return original; return original;
} }
// TextView does not display the tall left separator correctly,
// as it goes outside the height bounds and messes up the layout.
// Fix this by applying the left separator as a text view compound drawable.
// This creates a new issue as the compound drawable is not taken into the
// layout width sizing, but that is fixed in the span itself where it uses a blank
// padding string that adds to the layout width but is later ignored during UI drawing.
if (SettingsEnum.RYD_COMPACT_LAYOUT.getBoolean()) { if (SettingsEnum.RYD_COMPACT_LAYOUT.getBoolean()) {
// Do not apply any TextView changes, and text should always fit without clipping.
removeRollingNumberPatchChanges(view); removeRollingNumberPatchChanges(view);
} else { } else {
addRollingNumberPatchChanges(view); addRollingNumberPatchChanges(view);

View File

@ -113,7 +113,7 @@ public class ReturnYouTubeDislike {
private static NumberFormat dislikePercentageFormatter; private static NumberFormat dislikePercentageFormatter;
// Used for segmented dislike spans in Litho regular player. // Used for segmented dislike spans in Litho regular player.
private static final Rect leftSeparatorBounds; public static final Rect leftSeparatorBounds;
private static final Rect middleSeparatorBounds; private static final Rect middleSeparatorBounds;
/** /**
@ -260,31 +260,17 @@ public class ReturnYouTubeDislike {
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape()); ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setColor(getSeparatorColor()); shapeDrawable.getPaint().setColor(getSeparatorColor());
shapeDrawable.setBounds(middleSeparatorBounds); shapeDrawable.setBounds(middleSeparatorBounds);
// Use original text width if using compact layout with Rolling Number, // Use original text width if using Rolling Number,
// as there is no empty padding to allow any layout width differences. // to ensure the replacement styled span has the same width as the measured String,
// otherwise layout can be broken (especially on devices with small system font sizes).
middleSeparatorSpan.setSpan( middleSeparatorSpan.setSpan(
new VerticallyCenteredImageSpan(shapeDrawable, isRollingNumber && compactLayout), new VerticallyCenteredImageSpan(shapeDrawable, isRollingNumber),
shapeInsertionIndex, shapeInsertionIndex + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); shapeInsertionIndex, shapeInsertionIndex + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
builder.append(middleSeparatorSpan); builder.append(middleSeparatorSpan);
// dislikes // dislikes
builder.append(newSpannableWithDislikes(oldSpannable, voteData)); builder.append(newSpannableWithDislikes(oldSpannable, voteData));
// Add some padding for Rolling Number segmented span.
// Use an empty width span, as the layout uses the measured text width and not the
// actual span width. So adding padding and then removing it while drawing gives some
// extra wiggle room for the left separator drawable (which is not included in layout width).
if (isRollingNumber && !compactLayout) {
// To test this, set the device system font to the smallest available.
// If text clipping still occurs, then increase the number of padding spaces below.
// Any extra width will be padded around the like/dislike string
// as it's set to center text alignment.
Spannable rightPaddingString = new SpannableString(" ");
rightPaddingString.setSpan(new FixedWidthEmptySpan(0), 0,
rightPaddingString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
builder.append(rightPaddingString);
}
return new SpannableString(builder); return new SpannableString(builder);
} }