From d55b3ebcd7d506bbc90501ac2d769d95997167e0 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Tue, 25 Apr 2017 18:31:27 -0400 Subject: [PATCH] Emojione shortcodes now converted to unicode in display names. closes #82, closes #129 --- .../com/keylesspalace/tusky/BaseActivity.java | 2 ++ .../keylesspalace/tusky/LoginActivity.java | 1 - .../tusky/MyFirebaseMessagingService.java | 1 + .../keylesspalace/tusky/StatusViewHolder.java | 5 ---- .../keylesspalace/tusky/StringWithEmoji.java | 16 +++++++++++++ .../tusky/StringWithEmojiTypeAdapter.java | 23 +++++++++++++++++++ .../keylesspalace/tusky/entity/Account.java | 12 +++++----- 7 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/StringWithEmoji.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/StringWithEmojiTypeAdapter.java diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 2fa31b675..ce4cbdd72 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -32,6 +32,7 @@ import android.view.Menu; import com.google.firebase.iid.FirebaseInstanceId; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.keylesspalace.tusky.entity.Account; import java.io.IOException; @@ -120,6 +121,7 @@ public class BaseActivity extends AppCompatActivity { Gson gson = new GsonBuilder() .registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()) + .registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()) .create(); OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder() diff --git a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java index 3854a48e7..b85f1faaa 100644 --- a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java @@ -208,7 +208,6 @@ public class LoginActivity extends AppCompatActivity { } } - /** * Chain together the key-value pairs into a query string, for either appending to a URL or * as the content of an HTTP request. diff --git a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java index d278d611d..6ff4d212b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java +++ b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java @@ -122,6 +122,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { Gson gson = new GsonBuilder() .registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()) + .registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()) .create(); Retrofit retrofit = new Retrofit.Builder() diff --git a/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java index 5f260b6d4..6a2e431df 100644 --- a/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java @@ -16,14 +16,9 @@ package com.keylesspalace.tusky; import android.content.Context; -import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; -import android.text.SpannableStringBuilder; import android.text.Spanned; -import android.text.method.LinkMovementMethod; -import android.text.style.ClickableSpan; -import android.text.style.URLSpan; import android.view.View; import android.widget.CompoundButton; import android.widget.ImageButton; diff --git a/app/src/main/java/com/keylesspalace/tusky/StringWithEmoji.java b/app/src/main/java/com/keylesspalace/tusky/StringWithEmoji.java new file mode 100644 index 000000000..76d07e9c9 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/StringWithEmoji.java @@ -0,0 +1,16 @@ +package com.keylesspalace.tusky; + +/** + * This is just a wrapper class for a String. + * + * It was designed to get around the limitation of a Json deserializer which only allows custom + * deserializing based on types, when special handling for a specific field was what was actually + * desired (in this case, display names). So, it was most expedient to just make up a type. + */ +public class StringWithEmoji { + public String value; + + public StringWithEmoji(String value) { + this.value = value; + } +} diff --git a/app/src/main/java/com/keylesspalace/tusky/StringWithEmojiTypeAdapter.java b/app/src/main/java/com/keylesspalace/tusky/StringWithEmojiTypeAdapter.java new file mode 100644 index 000000000..e4923921a --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/StringWithEmojiTypeAdapter.java @@ -0,0 +1,23 @@ +package com.keylesspalace.tusky; + +import com.emojione.Emojione; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +/** This is a type-based workaround to allow for shortcode conversion when loading display names. */ +class StringWithEmojiTypeAdapter implements JsonDeserializer { + @Override + public StringWithEmoji deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + String value = json.getAsString(); + if (value != null) { + return new StringWithEmoji(Emojione.shortnameToUnicode(value, false)); + } else { + return new StringWithEmoji(""); + } + } +} diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Account.java b/app/src/main/java/com/keylesspalace/tusky/entity/Account.java index d76eb7c01..e7be8e810 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Account.java +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Account.java @@ -21,6 +21,7 @@ import android.text.Spanned; import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion; import com.google.gson.annotations.SerializedName; import com.keylesspalace.tusky.HtmlUtils; +import com.keylesspalace.tusky.StringWithEmoji; public class Account implements SearchSuggestion { public String id; @@ -32,7 +33,7 @@ public class Account implements SearchSuggestion { public String username; @SerializedName("display_name") - public String displayName; + public StringWithEmoji displayName; public Spanned note; @@ -70,11 +71,10 @@ public class Account implements SearchSuggestion { } public String getDisplayName() { - if (displayName.length() == 0) { + if (displayName.value.length() == 0) { return localUsername; } - - return displayName; + return displayName.value; } @Override @@ -92,7 +92,7 @@ public class Account implements SearchSuggestion { dest.writeString(id); dest.writeString(localUsername); dest.writeString(username); - dest.writeString(displayName); + dest.writeString(displayName.value); dest.writeString(HtmlUtils.toHtml(note)); dest.writeString(url); dest.writeString(avatar); @@ -111,7 +111,7 @@ public class Account implements SearchSuggestion { id = in.readString(); localUsername = in.readString(); username = in.readString(); - displayName = in.readString(); + displayName = new StringWithEmoji(in.readString()); note = HtmlUtils.fromHtml(in.readString()); url = in.readString(); avatar = in.readString();