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

Commit 5974fa7

Browse files
kunall17niftynei
authored andcommitted
Implement ClickableSpan for user mentions
1 parent 41c025d commit 5974fa7

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

app/src/main/java/com/zulip/android/ZulipApp.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.j256.ormlite.dao.Dao;
2525
import com.j256.ormlite.dao.RuntimeExceptionDao;
2626
import com.j256.ormlite.misc.TransactionManager;
27+
import com.zulip.android.activities.ZulipActivity;
2728
import com.zulip.android.database.DatabaseHelper;
2829
import com.zulip.android.models.Emoji;
2930
import com.zulip.android.models.Message;
@@ -59,6 +60,15 @@ public class ZulipApp extends Application {
5960
private DatabaseHelper databaseHelper;
6061
private Set<String> mutedTopics;
6162
private static final String MUTED_TOPIC_KEY = "mutedTopics";
63+
private ZulipActivity zulipActivity;
64+
65+
public ZulipActivity getZulipActivity() {
66+
return zulipActivity;
67+
}
68+
69+
public void setZulipActivity(ZulipActivity zulipActivity) {
70+
this.zulipActivity = zulipActivity;
71+
}
6272

6373
/**
6474
* Handler to manage batching of unread messages

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ protected void onCreate(Bundle savedInstanceState) {
289289
messageEt = (AutoCompleteTextView) findViewById(R.id.message_et);
290290
textView = (TextView) findViewById(R.id.textView);
291291
sendBtn = (ImageView) findViewById(R.id.send_btn);
292+
app.setZulipActivity(this);
292293
togglePrivateStreamBtn = (ImageView) findViewById(R.id.togglePrivateStream_btn);
293294
mutedTopics = new ArrayList<>();
294295
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

app/src/main/java/com/zulip/android/models/Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public int hashCode() {
127127
.toHashCode();
128128
}
129129

130-
private static Person getByEmail(ZulipApp app, String email) {
130+
public static Person getByEmail(ZulipApp app, String email) {
131131
try {
132132
Dao<Person, Integer> dao = app.getDatabaseHelper().getDao(
133133
Person.class);

app/src/main/java/com/zulip/android/util/CustomHtmlToSpannedConverter.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ private void handleStartTag(String tag, Attributes attributes) {
160160
start(mSpannableStringBuilder, new Monospace());
161161
} else if (tag.equalsIgnoreCase("a")) {
162162
startA(mSpannableStringBuilder, attributes, mBaseUri);
163+
} else if (tag.equalsIgnoreCase("span")
164+
&& attributes.getValue("class").equals("user-mention")) {
165+
startSpan(mSpannableStringBuilder, attributes);
163166
} else if (tag.equalsIgnoreCase("u")) {
164167
start(mSpannableStringBuilder, new Underline());
165168
} else if (tag.equalsIgnoreCase("sup")) {
@@ -257,6 +260,8 @@ private void handleEndTag(String tag) {
257260
MONOSPACE));
258261
} else if (tag.equalsIgnoreCase("a")) {
259262
endA(mSpannableStringBuilder);
263+
} else if (tag.equalsIgnoreCase("span")) {
264+
endSpan(mSpannableStringBuilder);
260265
} else if (tag.equalsIgnoreCase("u")) {
261266
end(mSpannableStringBuilder, Underline.class, new UnderlineSpan());
262267
} else if (tag.equalsIgnoreCase("sup")) {
@@ -418,6 +423,26 @@ private static void endFont(SpannableStringBuilder text) {
418423
}
419424
}
420425

426+
private static void startSpan(SpannableStringBuilder text, Attributes attributes) {
427+
String email = attributes.getValue("data-user-email");
428+
int len = text.length();
429+
text.setSpan(new Href(email), len, len, Spannable.SPAN_MARK_MARK);
430+
}
431+
432+
private static void endSpan(SpannableStringBuilder text) {
433+
int len = text.length();
434+
Object obj = getLast(text, Href.class);
435+
int where = text.getSpanStart(obj);
436+
text.removeSpan(obj);
437+
if (where != len) {
438+
Href h = (Href) obj;
439+
if (h != null && h.mHref != null) {
440+
text.setSpan(new ProfileSpan(h.mHref, userMentionColor), where, len,
441+
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
442+
}
443+
}
444+
}
445+
421446
private static void startA(SpannableStringBuilder text,
422447
Attributes attributes, String baseUri) {
423448
String href = attributes.getValue("", "href");
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.zulip.android.util;
2+
3+
import android.content.Context;
4+
import android.text.TextPaint;
5+
import android.text.style.ClickableSpan;
6+
import android.view.View;
7+
8+
import com.zulip.android.ZulipApp;
9+
import com.zulip.android.filters.NarrowFilterPM;
10+
import com.zulip.android.models.Person;
11+
12+
import java.sql.SQLException;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class ProfileSpan extends ClickableSpan {
17+
private String email;
18+
private int userMentionColor;
19+
20+
public ProfileSpan(String email, int color) {
21+
this.email = email;
22+
userMentionColor = color;
23+
}
24+
25+
@Override
26+
public void onClick(View widget) {
27+
Context context = widget.getContext().getApplicationContext();
28+
List<Person> people = new ArrayList<Person>();
29+
if (email.equals("*")) { //This is for "@all"
30+
try {
31+
people = Person.getAllPeople(ZulipApp.get());
32+
} catch (SQLException e) {
33+
ZLog.logException(e);
34+
return;
35+
}
36+
} else {
37+
for (String email : this.email.split(",")) {
38+
people.add(Person.getByEmail(ZulipApp.get(), email));
39+
}
40+
people.add(ZulipApp.get().getYou());
41+
}
42+
(((ZulipApp) context).getZulipActivity()).doNarrow(new NarrowFilterPM(people));
43+
}
44+
45+
@Override
46+
public void updateDrawState(TextPaint ds) {
47+
ds.setColor(userMentionColor);
48+
}
49+
}

app/src/main/java/com/zulip/android/viewholders/MessageHolder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.support.v7.widget.RecyclerView;
5+
import android.text.method.LinkMovementMethod;
56
import android.view.ContextMenu;
67
import android.view.MenuInflater;
78
import android.view.View;
@@ -31,6 +32,7 @@ public MessageHolder(final View itemView) {
3132
senderName = (TextView) itemView.findViewById(R.id.senderName);
3233
timestamp = (TextView) itemView.findViewById(R.id.timestamp);
3334
contentView = (TextView) itemView.findViewById(R.id.contentView);
35+
contentView.setMovementMethod(LinkMovementMethod.getInstance());
3436
leftBar = itemView.findViewById(R.id.leftBar);
3537
messageTile = (RelativeLayout) itemView.findViewById(R.id.messageTile);
3638
contentView.setOnClickListener(this);

0 commit comments

Comments
 (0)