Skip to content

Commit

Permalink
Allow comment links (if any) to gain focus
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderR committed Oct 9, 2019
1 parent 098b97b commit 5e70744
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.schabi.newpipe.info_list.holder;

import androidx.appcompat.app.AppCompatActivity;

import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.ViewGroup;
import android.widget.TextView;
Expand Down Expand Up @@ -114,15 +117,35 @@ public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager h
});
}

private void allowLinkFocus() {
if (itemView.isInTouchMode()) {
return;
}

URLSpan[] urls = itemContentView.getUrls();

if (urls != null && urls.length != 0) {
itemContentView.setMovementMethod(LinkMovementMethod.getInstance());
}
}

private void ellipsize() {
boolean hasEllipsis = false;

if (itemContentView.getLineCount() > commentDefaultLines){
int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1);
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);
hasEllipsis = true;
}

linkify();

if (!hasEllipsis) {
allowLinkFocus();
}
}

private void toggleEllipsize() {
Expand All @@ -137,11 +160,13 @@ private void expand() {
itemContentView.setMaxLines(commentExpandedLines);
itemContentView.setText(commentText);
linkify();
allowLinkFocus();
}

private void linkify(){
Linkify.addLinks(itemContentView, Linkify.WEB_URLS);
Linkify.addLinks(itemContentView, pattern, null, null, timestampLink);

itemContentView.setMovementMethod(null);
}
}

0 comments on commit 5e70744

Please sign in to comment.