mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 16:40:32 +00:00
make links in comments clickable, increase text size
This commit is contained in:
parent
554ebf7ab9
commit
c0004e988a
@ -1,7 +1,7 @@
|
|||||||
package org.schabi.newpipe.info_list.holder;
|
package org.schabi.newpipe.info_list.holder;
|
||||||
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.text.TextUtils;
|
import android.text.util.Linkify;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@ -11,6 +11,7 @@ import org.schabi.newpipe.extractor.InfoItem;
|
|||||||
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
|
||||||
import org.schabi.newpipe.info_list.InfoItemBuilder;
|
import org.schabi.newpipe.info_list.InfoItemBuilder;
|
||||||
import org.schabi.newpipe.report.ErrorActivity;
|
import org.schabi.newpipe.report.ErrorActivity;
|
||||||
|
import org.schabi.newpipe.util.CommentTextOnTouchListener;
|
||||||
import org.schabi.newpipe.util.ImageDisplayConstants;
|
import org.schabi.newpipe.util.ImageDisplayConstants;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
|
|
||||||
@ -26,6 +27,9 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
|
|||||||
private static final int commentDefaultLines = 2;
|
private static final int commentDefaultLines = 2;
|
||||||
private static final int commentExpandedLines = 1000;
|
private static final int commentExpandedLines = 1000;
|
||||||
|
|
||||||
|
private String commentText;
|
||||||
|
private boolean containsLinks = false;
|
||||||
|
|
||||||
CommentsMiniInfoItemHolder(InfoItemBuilder infoItemBuilder, int layoutId, ViewGroup parent) {
|
CommentsMiniInfoItemHolder(InfoItemBuilder infoItemBuilder, int layoutId, ViewGroup parent) {
|
||||||
super(infoItemBuilder, layoutId, parent);
|
super(infoItemBuilder, layoutId, parent);
|
||||||
|
|
||||||
@ -66,34 +70,57 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// ellipsize if not already ellipsized
|
itemContentView.setMaxLines(commentDefaultLines);
|
||||||
if (null == itemContentView.getEllipsize()) {
|
commentText = item.getCommentText();
|
||||||
itemContentView.setEllipsize(TextUtils.TruncateAt.END);
|
itemContentView.setText(commentText);
|
||||||
itemContentView.setMaxLines(commentDefaultLines);
|
containsLinks = linkify();
|
||||||
|
itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE);
|
||||||
|
|
||||||
|
if(itemContentView.getLineCount() == 0){
|
||||||
|
itemContentView.post(() -> ellipsize());
|
||||||
|
}else{
|
||||||
|
ellipsize();
|
||||||
}
|
}
|
||||||
|
|
||||||
itemContentView.setText(item.getCommentText());
|
|
||||||
if (null != item.getLikeCount()) {
|
if (null != item.getLikeCount()) {
|
||||||
itemLikesCountView.setText(String.valueOf(item.getLikeCount()));
|
itemLikesCountView.setText(String.valueOf(item.getLikeCount()));
|
||||||
}
|
}
|
||||||
itemPublishedTime.setText(item.getPublishedTime());
|
itemPublishedTime.setText(item.getPublishedTime());
|
||||||
|
|
||||||
itemView.setOnClickListener(view -> {
|
itemView.setOnClickListener(view -> {
|
||||||
toggleEllipsize(item.getCommentText());
|
toggleEllipsize();
|
||||||
if (itemBuilder.getOnCommentsSelectedListener() != null) {
|
if (itemBuilder.getOnCommentsSelectedListener() != null) {
|
||||||
itemBuilder.getOnCommentsSelectedListener().selected(item);
|
itemBuilder.getOnCommentsSelectedListener().selected(item);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void toggleEllipsize(String text) {
|
private void ellipsize() {
|
||||||
// toggle ellipsize
|
if (itemContentView.getLineCount() > commentDefaultLines){
|
||||||
if (null == itemContentView.getEllipsize()) {
|
int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1);
|
||||||
itemContentView.setEllipsize(TextUtils.TruncateAt.END);
|
String newVal = itemContentView.getText().subSequence(0, endOfLastLine - 3) + "...";
|
||||||
itemContentView.setMaxLines(commentDefaultLines);
|
itemContentView.setText(newVal);
|
||||||
} else {
|
if(containsLinks) linkify();
|
||||||
itemContentView.setEllipsize(null);
|
|
||||||
itemContentView.setMaxLines(commentExpandedLines);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void toggleEllipsize() {
|
||||||
|
if (itemContentView.getText().toString().equals(commentText)) {
|
||||||
|
ellipsize();
|
||||||
|
} else {
|
||||||
|
expand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expand() {
|
||||||
|
itemContentView.setMaxLines(commentExpandedLines);
|
||||||
|
itemContentView.setText(commentText);
|
||||||
|
if(containsLinks) linkify();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean linkify(){
|
||||||
|
boolean res = Linkify.addLinks(itemContentView, Linkify.WEB_URLS);
|
||||||
|
itemContentView.setMovementMethod(null);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,62 @@
|
|||||||
|
package org.schabi.newpipe.util;
|
||||||
|
|
||||||
|
import android.text.Layout;
|
||||||
|
import android.text.Selection;
|
||||||
|
import android.text.Spannable;
|
||||||
|
import android.text.Spanned;
|
||||||
|
import android.text.style.ClickableSpan;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
public class CommentTextOnTouchListener implements View.OnTouchListener {
|
||||||
|
|
||||||
|
public static final CommentTextOnTouchListener INSTANCE = new CommentTextOnTouchListener();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
if(!(v instanceof TextView)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TextView widget = (TextView) v;
|
||||||
|
Object text = widget.getText();
|
||||||
|
if (text instanceof Spanned) {
|
||||||
|
Spannable buffer = (Spannable) text;
|
||||||
|
|
||||||
|
int action = event.getAction();
|
||||||
|
|
||||||
|
if (action == MotionEvent.ACTION_UP
|
||||||
|
|| action == MotionEvent.ACTION_DOWN) {
|
||||||
|
int x = (int) event.getX();
|
||||||
|
int y = (int) event.getY();
|
||||||
|
|
||||||
|
x -= widget.getTotalPaddingLeft();
|
||||||
|
y -= widget.getTotalPaddingTop();
|
||||||
|
|
||||||
|
x += widget.getScrollX();
|
||||||
|
y += widget.getScrollY();
|
||||||
|
|
||||||
|
Layout layout = widget.getLayout();
|
||||||
|
int line = layout.getLineForVertical(y);
|
||||||
|
int off = layout.getOffsetForHorizontal(line, x);
|
||||||
|
|
||||||
|
ClickableSpan[] link = buffer.getSpans(off, off,
|
||||||
|
ClickableSpan.class);
|
||||||
|
|
||||||
|
if (link.length != 0) {
|
||||||
|
if (action == MotionEvent.ACTION_UP) {
|
||||||
|
link[0].onClick(widget);
|
||||||
|
} else if (action == MotionEvent.ACTION_DOWN) {
|
||||||
|
Selection.setSelection(buffer,
|
||||||
|
buffer.getSpanStart(link[0]),
|
||||||
|
buffer.getSpanEnd(link[0]));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -32,7 +32,7 @@
|
|||||||
android:ellipsize="end"
|
android:ellipsize="end"
|
||||||
android:lines="1"
|
android:lines="1"
|
||||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||||
android:textSize="@dimen/video_item_search_title_text_size"
|
android:textSize="@dimen/comment_item_title_text_size"
|
||||||
tools:text="Author Name, Lorem ipsum" />
|
tools:text="Author Name, Lorem ipsum" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
@ -43,10 +43,8 @@
|
|||||||
android:layout_marginBottom="@dimen/channel_item_description_to_details_margin"
|
android:layout_marginBottom="@dimen/channel_item_description_to_details_margin"
|
||||||
android:layout_toEndOf="@+id/itemThumbnailView"
|
android:layout_toEndOf="@+id/itemThumbnailView"
|
||||||
android:layout_toRightOf="@+id/itemThumbnailView"
|
android:layout_toRightOf="@+id/itemThumbnailView"
|
||||||
android:ellipsize="end"
|
|
||||||
android:lines="2"
|
|
||||||
android:textAppearance="?android:attr/textAppearanceLarge"
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||||
android:textSize="@dimen/video_item_search_uploader_text_size"
|
android:textSize="@dimen/comment_item_content_text_size"
|
||||||
tools:text="Comment Content, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blandit" />
|
tools:text="Comment Content, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blandit" />
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,10 +27,8 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="@dimen/channel_item_description_to_details_margin"
|
android:layout_marginBottom="@dimen/channel_item_description_to_details_margin"
|
||||||
android:layout_toRightOf="@+id/itemThumbnailView"
|
android:layout_toRightOf="@+id/itemThumbnailView"
|
||||||
android:ellipsize="end"
|
|
||||||
android:lines="2"
|
|
||||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||||
android:textSize="@dimen/video_item_search_uploader_text_size"
|
android:textSize="@dimen/comment_item_content_text_size"
|
||||||
tools:text="Channel description, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blandit" />
|
tools:text="Channel description, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blanditLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tristique vitae sem vitae blandit" />
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
|
@ -85,4 +85,8 @@
|
|||||||
|
|
||||||
<!-- File picker dimensions -->
|
<!-- File picker dimensions -->
|
||||||
<dimen name="file_picker_items_text_size">14sp</dimen>
|
<dimen name="file_picker_items_text_size">14sp</dimen>
|
||||||
|
|
||||||
|
<!-- Text Size -->
|
||||||
|
<dimen name="comment_item_title_text_size">12sp</dimen>
|
||||||
|
<dimen name="comment_item_content_text_size">12sp</dimen>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user