NewPipe/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder....

151 lines
6.0 KiB
Java
Raw Normal View History

2018-09-03 01:22:59 +02:00
package org.schabi.newpipe.info_list.holder;
import android.support.annotation.Nullable;
2018-09-03 01:22:59 +02:00
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
2018-09-03 01:22:59 +02:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
2019-03-22 19:52:59 +01:00
import org.jsoup.helper.StringUtil;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
import org.schabi.newpipe.info_list.InfoItemBuilder;
import org.schabi.newpipe.local.history.HistoryRecordManager;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.util.CommentTextOnTouchListener;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.util.ImageDisplayConstants;
import org.schabi.newpipe.util.NavigationHelper;
2019-03-02 00:42:06 +01:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2018-09-03 01:22:59 +02:00
import de.hdodenhof.circleimageview.CircleImageView;
public class CommentsMiniInfoItemHolder extends InfoItemHolder {
public final CircleImageView itemThumbnailView;
private final TextView itemContentView;
private final TextView itemLikesCountView;
private final TextView itemDislikesCountView;
private final TextView itemPublishedTime;
2018-09-03 01:22:59 +02:00
private static final int commentDefaultLines = 2;
private static final int commentExpandedLines = 1000;
private String commentText;
2019-03-02 00:42:06 +01:00
private String streamUrl;
private static final Pattern pattern = Pattern.compile("(\\d+:)?(\\d+)?:(\\d+)");
private final Linkify.TransformFilter timestampLink = new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
int timestamp = 0;
String hours = match.group(1);
String minutes = match.group(2);
String seconds = match.group(3);
if(hours != null) timestamp += (Integer.parseInt(hours.replace(":", ""))*3600);
if(minutes != null) timestamp += (Integer.parseInt(minutes.replace(":", ""))*60);
if(seconds != null) timestamp += (Integer.parseInt(seconds));
return streamUrl + url.replace(match.group(0), "#timestamp=" + String.valueOf(timestamp));
2019-03-02 00:42:06 +01:00
}
};
2018-09-03 01:22:59 +02:00
CommentsMiniInfoItemHolder(InfoItemBuilder infoItemBuilder, int layoutId, ViewGroup parent) {
super(infoItemBuilder, layoutId, parent);
itemThumbnailView = itemView.findViewById(R.id.itemThumbnailView);
itemLikesCountView = itemView.findViewById(R.id.detail_thumbs_up_count_view);
itemDislikesCountView = itemView.findViewById(R.id.detail_thumbs_down_count_view);
itemPublishedTime = itemView.findViewById(R.id.itemPublishedTime);
2018-12-07 02:15:33 +01:00
itemContentView = itemView.findViewById(R.id.itemCommentContentView);
2018-09-03 01:22:59 +02:00
}
public CommentsMiniInfoItemHolder(InfoItemBuilder infoItemBuilder, ViewGroup parent) {
this(infoItemBuilder, R.layout.list_comments_mini_item, parent);
}
@Override
public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) {
2018-09-03 01:22:59 +02:00
if (!(infoItem instanceof CommentsInfoItem)) return;
final CommentsInfoItem item = (CommentsInfoItem) infoItem;
itemBuilder.getImageLoader()
.displayImage(item.getAuthorThumbnail(),
itemThumbnailView,
ImageDisplayConstants.DISPLAY_THUMBNAIL_OPTIONS);
itemThumbnailView.setOnClickListener(view -> {
if(StringUtil.isBlank(item.getAuthorEndpoint())) return;
try {
final AppCompatActivity activity = (AppCompatActivity) itemBuilder.getContext();
NavigationHelper.openChannelFragment(
activity.getSupportFragmentManager(),
item.getServiceId(),
item.getAuthorEndpoint(),
item.getAuthorName());
} catch (Exception e) {
ErrorActivity.reportUiError((AppCompatActivity) itemBuilder.getContext(), e);
2018-09-03 01:22:59 +02:00
}
});
2019-03-02 00:42:06 +01:00
streamUrl = item.getUrl();
2019-03-22 00:27:33 +01:00
itemContentView.setLines(commentDefaultLines);
commentText = item.getCommentText();
itemContentView.setText(commentText);
itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE);
2019-03-22 00:27:33 +01:00
if (itemContentView.getLineCount() == 0) {
itemContentView.post(() -> ellipsize());
2019-03-22 00:27:33 +01:00
} else {
ellipsize();
2018-12-07 02:15:33 +01:00
}
2018-09-03 01:22:59 +02:00
if (null != item.getLikeCount()) {
itemLikesCountView.setText(String.valueOf(item.getLikeCount()));
}
itemPublishedTime.setText(item.getPublishedTime());
2018-09-03 01:22:59 +02:00
itemView.setOnClickListener(view -> {
toggleEllipsize();
2018-09-03 01:22:59 +02:00
if (itemBuilder.getOnCommentsSelectedListener() != null) {
itemBuilder.getOnCommentsSelectedListener().selected(item);
}
});
}
private void ellipsize() {
if (itemContentView.getLineCount() > commentDefaultLines){
int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1);
2019-03-22 01:26:56 +01:00
int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine -2);
if(end == -1) end = Math.max(endOfLastLine -2, 0);
String newVal = itemContentView.getText().subSequence(0, end) + "";
itemContentView.setText(newVal);
}
2019-03-22 00:27:33 +01:00
linkify();
}
private void toggleEllipsize() {
if (itemContentView.getText().toString().equals(commentText)) {
2019-03-22 00:27:33 +01:00
if (itemContentView.getLineCount() > commentDefaultLines) ellipsize();
2018-09-03 01:22:59 +02:00
} else {
expand();
2018-09-03 01:22:59 +02:00
}
}
private void expand() {
itemContentView.setMaxLines(commentExpandedLines);
itemContentView.setText(commentText);
2019-03-02 00:42:06 +01:00
linkify();
}
2019-03-02 00:42:06 +01:00
private void linkify(){
Linkify.addLinks(itemContentView, Linkify.WEB_URLS);
Linkify.addLinks(itemContentView, pattern, null, null, timestampLink);
itemContentView.setMovementMethod(null);
}
2018-09-03 01:22:59 +02:00
}