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

258 lines
9.2 KiB
Java
Raw Normal View History

2018-09-03 01:22:59 +02:00
package org.schabi.newpipe.info_list.holder;
import android.content.SharedPreferences;
2020-05-03 11:54:37 +02:00
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.View;
2018-09-03 01:22:59 +02:00
import android.view.ViewGroup;
import android.widget.RelativeLayout;
2018-09-03 01:22:59 +02:00
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.R;
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;
import org.schabi.newpipe.util.DeviceUtils;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.util.ImageDisplayConstants;
import org.schabi.newpipe.util.Localization;
2018-09-03 01:22:59 +02:00
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.ShareUtils;
2018-09-03 01:22:59 +02:00
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 {
private static final int COMMENT_DEFAULT_LINES = 2;
private static final int COMMENT_EXPANDED_LINES = 1000;
private static final Pattern PATTERN = Pattern.compile("(\\d+:)?(\\d+)?:(\\d+)");
private final String downloadThumbnailKey;
private final int commentHorizontalPadding;
private final int commentVerticalPadding;
private SharedPreferences preferences = null;
private final RelativeLayout itemRoot;
2018-09-03 01:22:59 +02:00
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 String commentText;
2019-03-02 00:42:06 +01:00
private String streamUrl;
private final Linkify.TransformFilter timestampLink = new Linkify.TransformFilter() {
@Override
public String transformUrl(final Matcher match, final String url) {
2019-03-02 00:42:06 +01:00
int timestamp = 0;
2020-08-16 10:24:58 +02:00
final String hours = match.group(1);
final String minutes = match.group(2);
final 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=" + timestamp);
2019-03-02 00:42:06 +01:00
}
};
CommentsMiniInfoItemHolder(final InfoItemBuilder infoItemBuilder, final int layoutId,
final ViewGroup parent) {
2018-09-03 01:22:59 +02:00
super(infoItemBuilder, layoutId, parent);
itemRoot = itemView.findViewById(R.id.itemRoot);
2018-09-03 01:22:59 +02:00
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);
downloadThumbnailKey = infoItemBuilder.getContext().
getString(R.string.download_thumbnail_key);
commentHorizontalPadding = (int) infoItemBuilder.getContext()
.getResources().getDimension(R.dimen.comments_horizontal_padding);
commentVerticalPadding = (int) infoItemBuilder.getContext()
.getResources().getDimension(R.dimen.comments_vertical_padding);
2018-09-03 01:22:59 +02:00
}
public CommentsMiniInfoItemHolder(final InfoItemBuilder infoItemBuilder,
final ViewGroup parent) {
2018-09-03 01:22:59 +02:00
this(infoItemBuilder, R.layout.list_comments_mini_item, parent);
}
@Override
public void updateFromItem(final InfoItem infoItem,
final HistoryRecordManager historyRecordManager) {
if (!(infoItem instanceof CommentsInfoItem)) {
return;
}
2018-09-03 01:22:59 +02:00
final CommentsInfoItem item = (CommentsInfoItem) infoItem;
preferences = PreferenceManager.getDefaultSharedPreferences(itemBuilder.getContext());
2018-09-03 01:22:59 +02:00
itemBuilder.getImageLoader()
.displayImage(item.getUploaderAvatarUrl(),
2018-09-03 01:22:59 +02:00
itemThumbnailView,
ImageDisplayConstants.DISPLAY_THUMBNAIL_OPTIONS);
if (preferences.getBoolean(downloadThumbnailKey, true)) {
itemThumbnailView.setVisibility(View.VISIBLE);
itemRoot.setPadding(commentVerticalPadding, commentVerticalPadding,
commentVerticalPadding, commentVerticalPadding);
} else {
itemThumbnailView.setVisibility(View.GONE);
itemRoot.setPadding(commentHorizontalPadding, commentVerticalPadding,
commentHorizontalPadding, commentVerticalPadding);
}
itemThumbnailView.setOnClickListener(view -> openCommentAuthor(item));
2018-09-03 01:22:59 +02:00
2019-03-02 00:42:06 +01:00
streamUrl = item.getUrl();
itemContentView.setLines(COMMENT_DEFAULT_LINES);
commentText = item.getCommentText();
itemContentView.setText(commentText);
itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE);
2019-03-22 00:27:33 +01:00
if (itemContentView.getLineCount() == 0) {
itemContentView.post(this::ellipsize);
2019-03-22 00:27:33 +01:00
} else {
ellipsize();
2018-12-07 02:15:33 +01:00
}
if (item.getLikeCount() >= 0) {
2018-09-03 01:22:59 +02:00
itemLikesCountView.setText(String.valueOf(item.getLikeCount()));
} else {
itemLikesCountView.setText("-");
}
if (item.getUploadDate() != null) {
itemPublishedTime.setText(Localization.relativeTime(item.getUploadDate()
.offsetDateTime()));
} else {
itemPublishedTime.setText(item.getTextualUploadDate());
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);
}
});
itemView.setOnLongClickListener(view -> {
2020-07-21 00:43:49 +02:00
if (DeviceUtils.isTv(itemBuilder.getContext())) {
openCommentAuthor(item);
2020-06-11 18:36:05 +02:00
} else {
ShareUtils.copyToClipboard(itemBuilder.getContext(), commentText);
2020-02-04 13:31:39 +01:00
}
return true;
2020-02-04 13:31:39 +01:00
});
}
private void openCommentAuthor(final CommentsInfoItem item) {
2020-05-03 11:54:37 +02:00
if (TextUtils.isEmpty(item.getUploaderUrl())) {
return;
}
try {
final AppCompatActivity activity = (AppCompatActivity) itemBuilder.getContext();
NavigationHelper.openChannelFragment(
activity.getSupportFragmentManager(),
item.getServiceId(),
item.getUploaderUrl(),
item.getUploaderName());
2020-08-16 10:24:58 +02:00
} catch (final Exception e) {
ErrorActivity.reportUiError((AppCompatActivity) itemBuilder.getContext(), e);
}
2018-09-03 01:22:59 +02:00
}
private void allowLinkFocus() {
2019-11-14 14:51:35 +01:00
itemContentView.setMovementMethod(LinkMovementMethod.getInstance());
}
private void denyLinkFocus() {
itemContentView.setMovementMethod(null);
}
private boolean shouldFocusLinks() {
if (itemView.isInTouchMode()) {
2019-11-14 14:51:35 +01:00
return false;
}
2020-08-16 10:24:58 +02:00
final URLSpan[] urls = itemContentView.getUrls();
2019-11-14 14:51:35 +01:00
return urls != null && urls.length != 0;
}
private void determineLinkFocus() {
if (shouldFocusLinks()) {
allowLinkFocus();
} else {
denyLinkFocus();
}
}
private void ellipsize() {
boolean hasEllipsis = false;
if (itemContentView.getLineCount() > COMMENT_DEFAULT_LINES) {
2020-08-16 10:24:58 +02:00
final int endOfLastLine
= itemContentView.getLayout().getLineEnd(COMMENT_DEFAULT_LINES - 1);
int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine - 2);
if (end == -1) {
end = Math.max(endOfLastLine - 2, 0);
}
2020-08-16 10:24:58 +02:00
final String newVal = itemContentView.getText().subSequence(0, end) + "";
itemContentView.setText(newVal);
hasEllipsis = true;
}
2019-03-22 00:27:33 +01:00
linkify();
2019-11-14 14:51:35 +01:00
if (hasEllipsis) {
denyLinkFocus();
} else {
determineLinkFocus();
}
}
private void toggleEllipsize() {
if (itemContentView.getText().toString().equals(commentText)) {
if (itemContentView.getLineCount() > COMMENT_DEFAULT_LINES) {
ellipsize();
}
2018-09-03 01:22:59 +02:00
} else {
expand();
2018-09-03 01:22:59 +02:00
}
}
private void expand() {
itemContentView.setMaxLines(COMMENT_EXPANDED_LINES);
itemContentView.setText(commentText);
2019-03-02 00:42:06 +01:00
linkify();
2019-11-14 14:51:35 +01:00
determineLinkFocus();
}
private void linkify() {
2019-03-02 00:42:06 +01:00
Linkify.addLinks(itemContentView, Linkify.WEB_URLS);
Linkify.addLinks(itemContentView, PATTERN, null, null, timestampLink);
}
2018-09-03 01:22:59 +02:00
}