Emojione shortcodes now converted to unicode in display names. closes #82, closes #129

This commit is contained in:
Vavassor 2017-04-25 18:31:27 -04:00
parent 1240a07ef4
commit d55b3ebcd7
7 changed files with 48 additions and 12 deletions

View File

@ -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()

View File

@ -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.

View File

@ -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()

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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<StringWithEmoji> {
@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("");
}
}
}

View File

@ -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();