From 5f045ca5c33f020932db592c2fe38feb2fd7a889 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Tue, 20 Jun 2017 18:41:57 -0400 Subject: [PATCH] Fixes a bug where mentioning users of the same username links them all to the same profile. Closes #312 Also, removes the title on the search page and fixes an intermittent crash on thread pages when elements load in a paritcular order. --- .../keylesspalace/tusky/SearchActivity.java | 1 + .../tusky/adapter/StatusViewHolder.java | 2 -- .../tusky/adapter/ThreadAdapter.java | 4 ++- .../keylesspalace/tusky/entity/Status.java | 2 -- .../keylesspalace/tusky/util/LinkHelper.java | 26 ++++++++++++++++++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/SearchActivity.java b/app/src/main/java/com/keylesspalace/tusky/SearchActivity.java index 2b8a9be7..141e18df 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SearchActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/SearchActivity.java @@ -66,6 +66,7 @@ public class SearchActivity extends BaseActivity implements SearchView.OnQueryTe if (bar != null) { bar.setDisplayHomeAsUpEnabled(true); bar.setDisplayShowHomeEnabled(true); + bar.setDisplayShowTitleEnabled(false); } RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java index abfecd8e..0ee4bcc6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java @@ -231,8 +231,6 @@ class StatusViewHolder extends RecyclerView.ViewHolder { } }); } - - } if (sensitive) { diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/ThreadAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/ThreadAdapter.java index 0bfccbf5..9c885280 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/ThreadAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/ThreadAdapter.java @@ -82,7 +82,9 @@ public class ThreadAdapter extends RecyclerView.Adapter implements AdapterItemRe } public int setStatus(Status status) { - if (statuses.size() > 0 && statuses.get(statusIndex).equals(status)) { + if (statuses.size() > 0 + && statusIndex < statuses.size() + && statuses.get(statusIndex).equals(status)) { // Do not add this status on refresh, it's already in there. statuses.set(statusIndex, status); return statusIndex; diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Status.java b/app/src/main/java/com/keylesspalace/tusky/entity/Status.java index c98ebfa5..8fe10681 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Status.java +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Status.java @@ -172,6 +172,4 @@ public class Status { @SerializedName("username") public String localUsername; } - - } diff --git a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.java b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.java index 46ddd5f1..55ff6b7c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.java @@ -27,7 +27,25 @@ import android.widget.TextView; import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.interfaces.LinkListener; +import java.net.URI; +import java.net.URISyntaxException; + public class LinkHelper { + private static String getDomain(String urlString) { + URI uri; + try { + uri = new URI(urlString); + } catch (URISyntaxException e) { + return ""; + } + String host = uri.getHost(); + if (host.startsWith("www.")) { + return host.substring(4); + } else { + return host; + } + } + public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions, boolean useCustomTabs, final LinkListener listener) { @@ -49,11 +67,17 @@ public class LinkHelper { builder.removeSpan(span); builder.setSpan(newSpan, start, end, flags); } else if (text.charAt(0) == '@' && mentions != null) { - final String accountUsername = text.subSequence(1, text.length()).toString(); + String accountUsername = text.subSequence(1, text.length()).toString(); + /* There may be multiple matches for users on different instances with the same + * username. If a match has the same domain we know it's for sure the same, but if + * that can't be found then just go with whichever one matched last. */ String id = null; for (Status.Mention mention : mentions) { if (mention.localUsername.equals(accountUsername)) { id = mention.id; + if (mention.url.contains(getDomain(span.getURL()))) { + break; + } } } if (id != null) {