Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
implement long-click to copy links
Browse files Browse the repository at this point in the history
closes #84
  • Loading branch information
sk22 committed Dec 23, 2022
1 parent 3031cc4 commit b32e322
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private boolean onButtonTouch(View v, MotionEvent event){
v.removeCallbacks(longClickRunnable);
v.animate().scaleX(1).scaleY(1).setInterpolator(CubicBezierInterpolator.DEFAULT).setDuration(150).start();
if (disabled) return true;
if (action == MotionEvent.ACTION_UP && eventDuration < ViewConfiguration.getLongPressTimeout()) v.performClick();
if (action == MotionEvent.ACTION_UP && eventDuration <= ViewConfiguration.getLongPressTimeout()) v.performClick();
else v.startAnimation(opacityIn);
} else if (action == MotionEvent.ACTION_DOWN) {
touchingView = v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.text.Spanned;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.TextView;

import me.grishka.appkit.utils.V;
Expand All @@ -20,7 +22,11 @@ public class ClickableLinksDelegate {
private Path hlPath;
private LinkSpan selectedSpan;
private TextView view;


private final Runnable longClickRunnable = () -> {
if (selectedSpan != null) selectedSpan.onLongClick(view.getContext());
};

public ClickableLinksDelegate(TextView view) {
this.view=view;
hlPaint=new Paint();
Expand All @@ -30,6 +36,7 @@ public ClickableLinksDelegate(TextView view) {
}

public boolean onTouch(MotionEvent event) {
long eventDuration = event.getEventTime() - event.getDownTime();
if(event.getAction()==MotionEvent.ACTION_DOWN){
int line=-1;
Rect rect=new Rect();
Expand Down Expand Up @@ -63,6 +70,7 @@ public boolean onTouch(MotionEvent event) {
}
hlPath=new Path();
selectedSpan=span;
view.postDelayed(longClickRunnable, ViewConfiguration.getLongPressTimeout());
hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000);
//l.getSelectionPath(start, end, hlPath);
for(int j=lstart;j<=lend;j++){
Expand Down Expand Up @@ -90,8 +98,11 @@ public boolean onTouch(MotionEvent event) {
}
}
if(event.getAction()==MotionEvent.ACTION_UP && selectedSpan!=null){
view.playSoundEffect(SoundEffectConstants.CLICK);
selectedSpan.onClick(view.getContext());
if (eventDuration <= ViewConfiguration.getLongPressTimeout()) {
view.playSoundEffect(SoundEffectConstants.CLICK);
selectedSpan.onClick(view.getContext());
}
view.removeCallbacks(longClickRunnable);
hlPath=null;
selectedSpan=null;
view.invalidate();
Expand All @@ -100,6 +111,7 @@ public boolean onTouch(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_CANCEL){
hlPath=null;
selectedSpan=null;
view.removeCallbacks(longClickRunnable);
view.invalidate();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public void head(@NonNull Node node, int depth){
case "a" -> {
String href=el.attr("href");
LinkSpan.Type linkType;
String text=el.text();
if(el.hasClass("hashtag")){
String text=el.text();
if(text.startsWith("#")){
linkType=LinkSpan.Type.HASHTAG;
href=text.substring(1);
Expand All @@ -136,7 +136,7 @@ public void head(@NonNull Node node, int depth){
}else{
linkType=LinkSpan.Type.URL;
}
openSpans.add(new SpanInfo(new LinkSpan(href, null, linkType, accountID), ssb.length(), el));
openSpans.add(new SpanInfo(new LinkSpan(href, null, linkType, accountID, text), ssb.length(), el));
}
case "br" -> ssb.append('\n');
case "span" -> {
Expand Down Expand Up @@ -260,7 +260,7 @@ public static CharSequence parseLinks(String text){
String url=matcher.group(3);
if(TextUtils.isEmpty(matcher.group(4)))
url="http://"+url;
ssb.setSpan(new LinkSpan(url, null, LinkSpan.Type.URL, null), matcher.start(3), matcher.end(3), 0);
ssb.setSpan(new LinkSpan(url, null, LinkSpan.Type.URL, null, url), matcher.start(3), matcher.end(3), 0);
}while(matcher.find()); // Find more URLs
return ssb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class LinkSpan extends CharacterStyle {
private String link;
private Type type;
private String accountID;
private String text;

public LinkSpan(String link, OnLinkClickListener listener, Type type, String accountID){
public LinkSpan(String link, OnLinkClickListener listener, Type type, String accountID, String text){
this.listener=listener;
this.link=link;
this.type=type;
this.accountID=accountID;
this.text=text;
}

public int getColor(){
Expand All @@ -38,6 +40,10 @@ public void onClick(Context context){
}
}

public void onLongClick(Context context) {
UiUtils.copyText(context, getType() == Type.URL ? link : text);
}

public String getLink(){
return link;
}
Expand Down

0 comments on commit b32e322

Please sign in to comment.