Skip to content

Commit

Permalink
Improve TextEllipsizer class
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Dec 23, 2023
1 parent 65eb631 commit 9ff1b52
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions app/src/main/java/org/schabi/newpipe/util/text/TextEllipsizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,6 +45,7 @@ public TextEllipsizer(@NonNull final TextView view,
@Nullable final StreamingService streamingService) {
this.view = view;
this.maxLines = maxLines;
this.content = Description.EMPTY_DESCRIPTION;
this.streamingService = streamingService;

paintAtContentSize.setTextSize(view.getTextSize());
Expand All @@ -57,14 +58,14 @@ public void setOnContentChanged(@Nullable final Consumer<Boolean> onContentChang

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);
}
});
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public void ellipsize() {
}

/**
* Toggle the view between the ellipsed and expanded state.
* Toggle the view between the ellipsized and expanded state.
*/
public void toggle() {
if (isEllipsized) {
Expand All @@ -146,16 +147,17 @@ public void toggle() {
}

/**
* 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<View> consumer) {
Expand All @@ -173,19 +175,15 @@ private void linkifyContentView(final Consumer<View> consumer) {
/**
* Add a listener which is called when the given content is changed,
* 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.
* <em>Ellipsized</em> content is represented as {@code true},
* 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;
}

public void removeStateChangeListener() {
this.stateChangeListener = null;
}

private void notifyStateChangeListener(final boolean oldState) {
if (oldState != isEllipsized && stateChangeListener != null) {
stateChangeListener.accept(isEllipsized);
Expand Down

0 comments on commit 9ff1b52

Please sign in to comment.