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

Commit b32e322

Browse files
committed
implement long-click to copy links
closes #84
1 parent 3031cc4 commit b32e322

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/FooterStatusDisplayItem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private boolean onButtonTouch(View v, MotionEvent event){
150150
v.removeCallbacks(longClickRunnable);
151151
v.animate().scaleX(1).scaleY(1).setInterpolator(CubicBezierInterpolator.DEFAULT).setDuration(150).start();
152152
if (disabled) return true;
153-
if (action == MotionEvent.ACTION_UP && eventDuration < ViewConfiguration.getLongPressTimeout()) v.performClick();
153+
if (action == MotionEvent.ACTION_UP && eventDuration <= ViewConfiguration.getLongPressTimeout()) v.performClick();
154154
else v.startAnimation(opacityIn);
155155
} else if (action == MotionEvent.ACTION_DOWN) {
156156
touchingView = v;

mastodon/src/main/java/org/joinmastodon/android/ui/text/ClickableLinksDelegate.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import android.text.Spanned;
1111
import android.view.MotionEvent;
1212
import android.view.SoundEffectConstants;
13+
import android.view.View;
14+
import android.view.ViewConfiguration;
1315
import android.widget.TextView;
1416

1517
import me.grishka.appkit.utils.V;
@@ -20,7 +22,11 @@ public class ClickableLinksDelegate {
2022
private Path hlPath;
2123
private LinkSpan selectedSpan;
2224
private TextView view;
23-
25+
26+
private final Runnable longClickRunnable = () -> {
27+
if (selectedSpan != null) selectedSpan.onLongClick(view.getContext());
28+
};
29+
2430
public ClickableLinksDelegate(TextView view) {
2531
this.view=view;
2632
hlPaint=new Paint();
@@ -30,6 +36,7 @@ public ClickableLinksDelegate(TextView view) {
3036
}
3137

3238
public boolean onTouch(MotionEvent event) {
39+
long eventDuration = event.getEventTime() - event.getDownTime();
3340
if(event.getAction()==MotionEvent.ACTION_DOWN){
3441
int line=-1;
3542
Rect rect=new Rect();
@@ -63,6 +70,7 @@ public boolean onTouch(MotionEvent event) {
6370
}
6471
hlPath=new Path();
6572
selectedSpan=span;
73+
view.postDelayed(longClickRunnable, ViewConfiguration.getLongPressTimeout());
6674
hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000);
6775
//l.getSelectionPath(start, end, hlPath);
6876
for(int j=lstart;j<=lend;j++){
@@ -90,8 +98,11 @@ public boolean onTouch(MotionEvent event) {
9098
}
9199
}
92100
if(event.getAction()==MotionEvent.ACTION_UP && selectedSpan!=null){
93-
view.playSoundEffect(SoundEffectConstants.CLICK);
94-
selectedSpan.onClick(view.getContext());
101+
if (eventDuration <= ViewConfiguration.getLongPressTimeout()) {
102+
view.playSoundEffect(SoundEffectConstants.CLICK);
103+
selectedSpan.onClick(view.getContext());
104+
}
105+
view.removeCallbacks(longClickRunnable);
95106
hlPath=null;
96107
selectedSpan=null;
97108
view.invalidate();
@@ -100,6 +111,7 @@ public boolean onTouch(MotionEvent event) {
100111
if(event.getAction()==MotionEvent.ACTION_CANCEL){
101112
hlPath=null;
102113
selectedSpan=null;
114+
view.removeCallbacks(longClickRunnable);
103115
view.invalidate();
104116
return false;
105117
}

mastodon/src/main/java/org/joinmastodon/android/ui/text/HtmlParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public void head(@NonNull Node node, int depth){
117117
case "a" -> {
118118
String href=el.attr("href");
119119
LinkSpan.Type linkType;
120+
String text=el.text();
120121
if(el.hasClass("hashtag")){
121-
String text=el.text();
122122
if(text.startsWith("#")){
123123
linkType=LinkSpan.Type.HASHTAG;
124124
href=text.substring(1);
@@ -136,7 +136,7 @@ public void head(@NonNull Node node, int depth){
136136
}else{
137137
linkType=LinkSpan.Type.URL;
138138
}
139-
openSpans.add(new SpanInfo(new LinkSpan(href, null, linkType, accountID), ssb.length(), el));
139+
openSpans.add(new SpanInfo(new LinkSpan(href, null, linkType, accountID, text), ssb.length(), el));
140140
}
141141
case "br" -> ssb.append('\n');
142142
case "span" -> {
@@ -260,7 +260,7 @@ public static CharSequence parseLinks(String text){
260260
String url=matcher.group(3);
261261
if(TextUtils.isEmpty(matcher.group(4)))
262262
url="http://"+url;
263-
ssb.setSpan(new LinkSpan(url, null, LinkSpan.Type.URL, null), matcher.start(3), matcher.end(3), 0);
263+
ssb.setSpan(new LinkSpan(url, null, LinkSpan.Type.URL, null, url), matcher.start(3), matcher.end(3), 0);
264264
}while(matcher.find()); // Find more URLs
265265
return ssb;
266266
}

mastodon/src/main/java/org/joinmastodon/android/ui/text/LinkSpan.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public class LinkSpan extends CharacterStyle {
1313
private String link;
1414
private Type type;
1515
private String accountID;
16+
private String text;
1617

17-
public LinkSpan(String link, OnLinkClickListener listener, Type type, String accountID){
18+
public LinkSpan(String link, OnLinkClickListener listener, Type type, String accountID, String text){
1819
this.listener=listener;
1920
this.link=link;
2021
this.type=type;
2122
this.accountID=accountID;
23+
this.text=text;
2224
}
2325

2426
public int getColor(){
@@ -38,6 +40,10 @@ public void onClick(Context context){
3840
}
3941
}
4042

43+
public void onLongClick(Context context) {
44+
UiUtils.copyText(context, getType() == Type.URL ? link : text);
45+
}
46+
4147
public String getLink(){
4248
return link;
4349
}

0 commit comments

Comments
 (0)