1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 00:20:32 +00:00

Improve TextEllipsizer class

This commit is contained in:
Stypox 2023-12-23 17:53:27 +01:00
parent 65eb631711
commit 9ff1b5230f
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -33,7 +33,7 @@ public final class TextEllipsizer {
@Nullable private StreamingService streamingService; @Nullable private StreamingService streamingService;
@Nullable private String streamUrl; @Nullable private String streamUrl;
private boolean isEllipsized = false; private boolean isEllipsized = false;
@Nullable private Boolean caBeEllipsized = null; @Nullable private Boolean canBeEllipsized = null;
@NonNull private final Paint paintAtContentSize = new Paint(); @NonNull private final Paint paintAtContentSize = new Paint();
private final float ellipsisWidthPx; private final float ellipsisWidthPx;
@ -45,6 +45,7 @@ public final class TextEllipsizer {
@Nullable final StreamingService streamingService) { @Nullable final StreamingService streamingService) {
this.view = view; this.view = view;
this.maxLines = maxLines; this.maxLines = maxLines;
this.content = Description.EMPTY_DESCRIPTION;
this.streamingService = streamingService; this.streamingService = streamingService;
paintAtContentSize.setTextSize(view.getTextSize()); paintAtContentSize.setTextSize(view.getTextSize());
@ -57,14 +58,14 @@ public final class TextEllipsizer {
public void setContent(@NonNull final Description content) { public void setContent(@NonNull final Description content) {
this.content = content; this.content = content;
caBeEllipsized = null; canBeEllipsized = null;
linkifyContentView(v -> { linkifyContentView(v -> {
final int currentMaxLines = view.getMaxLines(); final int currentMaxLines = view.getMaxLines();
view.setMaxLines(EXPANDED_LINES); view.setMaxLines(EXPANDED_LINES);
caBeEllipsized = view.getLineCount() > maxLines; canBeEllipsized = view.getLineCount() > maxLines;
view.setMaxLines(currentMaxLines); view.setMaxLines(currentMaxLines);
if (onContentChanged != null) { 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() { public void toggle() {
if (isEllipsized) { if (isEllipsized) {
@ -146,16 +147,17 @@ public final class TextEllipsizer {
} }
/** /**
* Whether the {@link view} can be ellipsized. * Whether the {@link #view} can be ellipsized.
* This is only the case when the {@link content} has more lines * This is only the case when the {@link #content} has more lines
* than allowed via {@link maxLines}. * than allowed via {@link #maxLines}.
* @return {@code true} if the {@link content} has more lines than allowed via {@link maxLines} * @return {@code true} if the {@link #content} has more lines than allowed via
* and thus can be shortened, {@code false} if the {@code content} fits into the {@link view} * {@link #maxLines} and thus can be shortened, {@code false} if the {@code content} fits into
* without being shortened and {@code null} if the initialization is not completed yet. * the {@link #view} without being shortened and {@code null} if the initialization is not
* completed yet.
*/ */
@Nullable @Nullable
public Boolean canBeEllipsized() { public Boolean canBeEllipsized() {
return caBeEllipsized; return canBeEllipsized;
} }
private void linkifyContentView(final Consumer<View> consumer) { private void linkifyContentView(final Consumer<View> consumer) {
@ -173,19 +175,15 @@ public final class TextEllipsizer {
/** /**
* Add a listener which is called when the given content is changed, * Add a listener which is called when the given content is changed,
* either from <em>ellipsized</em> to <em>full</em> or vice versa. * either from <em>ellipsized</em> to <em>full</em> 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. * The Boolean parameter is the new state.
* <em>Ellipsized</em> content is represented as {@code true}, * <em>Ellipsized</em> content is represented as {@code true},
* normal or <em>full</em> content by {@code false}. * normal or <em>full</em> content by {@code false}.
*/ */
public void setStateChangeListener(final Consumer<Boolean> listener) { public void setStateChangeListener(@Nullable final Consumer<Boolean> listener) {
this.stateChangeListener = listener; this.stateChangeListener = listener;
} }
public void removeStateChangeListener() {
this.stateChangeListener = null;
}
private void notifyStateChangeListener(final boolean oldState) { private void notifyStateChangeListener(final boolean oldState) {
if (oldState != isEllipsized && stateChangeListener != null) { if (oldState != isEllipsized && stateChangeListener != null) {
stateChangeListener.accept(isEllipsized); stateChangeListener.accept(isEllipsized);