From 9ff1b5230fda1411d031cca528a2b6febed788e4 Mon Sep 17 00:00:00 2001 From: Stypox Date: Sat, 23 Dec 2023 17:53:27 +0100 Subject: [PATCH] Improve TextEllipsizer class --- .../newpipe/util/text/TextEllipsizer.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/text/TextEllipsizer.java b/app/src/main/java/org/schabi/newpipe/util/text/TextEllipsizer.java index 41084926b..184b73304 100644 --- a/app/src/main/java/org/schabi/newpipe/util/text/TextEllipsizer.java +++ b/app/src/main/java/org/schabi/newpipe/util/text/TextEllipsizer.java @@ -33,7 +33,7 @@ public final class TextEllipsizer { @Nullable private StreamingService streamingService; @Nullable private String streamUrl; private boolean isEllipsized = false; - @Nullable private Boolean caBeEllipsized = null; + @Nullable private Boolean canBeEllipsized = null; @NonNull private final Paint paintAtContentSize = new Paint(); private final float ellipsisWidthPx; @@ -45,6 +45,7 @@ public final class TextEllipsizer { @Nullable final StreamingService streamingService) { this.view = view; this.maxLines = maxLines; + this.content = Description.EMPTY_DESCRIPTION; this.streamingService = streamingService; paintAtContentSize.setTextSize(view.getTextSize()); @@ -57,14 +58,14 @@ public final class TextEllipsizer { public void setContent(@NonNull final Description content) { this.content = content; - caBeEllipsized = null; + canBeEllipsized = null; linkifyContentView(v -> { final int currentMaxLines = view.getMaxLines(); view.setMaxLines(EXPANDED_LINES); - caBeEllipsized = view.getLineCount() > maxLines; + canBeEllipsized = view.getLineCount() > maxLines; view.setMaxLines(currentMaxLines); if (onContentChanged != null) { - onContentChanged.accept(caBeEllipsized); + onContentChanged.accept(canBeEllipsized); } }); } @@ -135,7 +136,7 @@ public final class TextEllipsizer { } /** - * Toggle the view between the ellipsed and expanded state. + * Toggle the view between the ellipsized and expanded state. */ public void toggle() { if (isEllipsized) { @@ -146,16 +147,17 @@ public final class TextEllipsizer { } /** - * Whether the {@link view} can be ellipsized. - * This is only the case when the {@link content} has more lines - * than allowed via {@link maxLines}. - * @return {@code true} if the {@link content} has more lines than allowed via {@link maxLines} - * and thus can be shortened, {@code false} if the {@code content} fits into the {@link view} - * without being shortened and {@code null} if the initialization is not completed yet. + * Whether the {@link #view} can be ellipsized. + * This is only the case when the {@link #content} has more lines + * than allowed via {@link #maxLines}. + * @return {@code true} if the {@link #content} has more lines than allowed via + * {@link #maxLines} and thus can be shortened, {@code false} if the {@code content} fits into + * the {@link #view} without being shortened and {@code null} if the initialization is not + * completed yet. */ @Nullable public Boolean canBeEllipsized() { - return caBeEllipsized; + return canBeEllipsized; } private void linkifyContentView(final Consumer consumer) { @@ -173,19 +175,15 @@ public final class TextEllipsizer { /** * Add a listener which is called when the given content is changed, * either from ellipsized to full or vice versa. - * @param listener The listener to be called. + * @param listener The listener to be called, or {@code null} to remove it. * The Boolean parameter is the new state. * Ellipsized content is represented as {@code true}, * normal or full content by {@code false}. */ - public void setStateChangeListener(final Consumer listener) { + public void setStateChangeListener(@Nullable final Consumer listener) { this.stateChangeListener = listener; } - public void removeStateChangeListener() { - this.stateChangeListener = null; - } - private void notifyStateChangeListener(final boolean oldState) { if (oldState != isEllipsized && stateChangeListener != null) { stateChangeListener.accept(isEllipsized);