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

Commit

Permalink
Implement ClickableSpan for user mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
kunall17 committed Aug 1, 2016
1 parent e4fc693 commit 1340b61
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/src/main/java/com/zulip/android/ZulipApp.java
Expand Up @@ -20,6 +20,7 @@
import com.crashlytics.android.Crashlytics;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.zulip.android.activities.ZulipActivity;
import com.zulip.android.database.DatabaseHelper;
import com.zulip.android.models.Message;
import com.zulip.android.models.Person;
Expand All @@ -45,6 +46,15 @@ public class ZulipApp extends Application {
DatabaseHelper databaseHelper;
Set<String> mutedTopics;
private static final String MUTED_TOPIC_KEY = "mutedTopics";
private ZulipActivity zulipActivity;

public ZulipActivity getZulipActivity() {
return zulipActivity;
}

public void setZulipActivity(ZulipActivity zulipActivity) {
this.zulipActivity = zulipActivity;
}

/**
* Handler to manage batching of unread messages
Expand Down
Expand Up @@ -244,6 +244,7 @@ protected void onCreate(Bundle savedInstanceState) {
messageEt = (EditText) findViewById(R.id.message_et);
textView = (TextView) findViewById(R.id.textView);
sendBtn = (ImageView) findViewById(R.id.send_btn);
app.setZulipActivity(this);
togglePrivateStreamBtn = (ImageView) findViewById(R.id.togglePrivateStream_btn);
mutedTopics = new ArrayList<>();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/zulip/android/models/Person.java
Expand Up @@ -126,7 +126,7 @@ public int hashCode() {
.toHashCode();
}

static Person getByEmail(ZulipApp app, String email) {
public static Person getByEmail(ZulipApp app, String email) {
try {
Dao<Person, Integer> dao = app.getDatabaseHelper().getDao(
Person.class);
Expand Down
Expand Up @@ -158,6 +158,9 @@ private void handleStartTag(String tag, Attributes attributes) {
start(mSpannableStringBuilder, new Monospace());
} else if (tag.equalsIgnoreCase("a")) {
startA(mSpannableStringBuilder, attributes, mBaseUri);
} else if (tag.equalsIgnoreCase("span")
&& attributes.getValue("class").equals("user-mention")) {
startSpan(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("u")) {
start(mSpannableStringBuilder, new Underline());
} else if (tag.equalsIgnoreCase("sup")) {
Expand Down Expand Up @@ -255,6 +258,8 @@ private void handleEndTag(String tag) {
MONOSPACE));
} else if (tag.equalsIgnoreCase("a")) {
endA(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("span")) {
endSpan(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("u")) {
end(mSpannableStringBuilder, Underline.class, new UnderlineSpan());
} else if (tag.equalsIgnoreCase("sup")) {
Expand Down Expand Up @@ -416,6 +421,26 @@ private static void endFont(SpannableStringBuilder text) {
}
}

private static void startSpan(SpannableStringBuilder text, Attributes attributes) {
String email = attributes.getValue("data-user-email");
int len = text.length();
text.setSpan(new Href(email), len, len, Spannable.SPAN_MARK_MARK);
}

private static void endSpan(SpannableStringBuilder text) {
int len = text.length();
Object obj = getLast(text, Href.class);
int where = text.getSpanStart(obj);
text.removeSpan(obj);
if (where != len) {
Href h = (Href) obj;
if (h != null && h.mHref != null) {
text.setSpan(new ProfileSpan(h.mHref, userMentionColor), where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}

private static void startA(SpannableStringBuilder text,
Attributes attributes, String baseUri) {
String href = attributes.getValue("", "href");
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/zulip/android/util/ProfileSpan.java
@@ -0,0 +1,48 @@
package com.zulip.android.util;

import android.content.Context;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import com.zulip.android.ZulipApp;
import com.zulip.android.filters.NarrowFilterPM;
import com.zulip.android.models.Person;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProfileSpan extends ClickableSpan {
private String email;
private int userMentionColor;

public ProfileSpan(String email, int color) {
this.email = email;
userMentionColor = color;
}

@Override
public void onClick(View widget) {
Context context = widget.getContext().getApplicationContext();
List<Person> people = new ArrayList<Person>();
if (email.equals("*")) { //This is for "@all"
try {
people = Person.getAllPeople(ZulipApp.get());
} catch (SQLException e) {
ZLog.logException(e);
return;
}
} else {
for (String email : this.email.split(",")) {
people.add(Person.getByEmail(ZulipApp.get(), email));
}
}
(((ZulipApp) context).getZulipActivity()).doNarrow(new NarrowFilterPM(people));
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(userMentionColor);
}
}

0 comments on commit 1340b61

Please sign in to comment.