From d0f7f6f83c8eab54212bd81194255c35fda38a66 Mon Sep 17 00:00:00 2001 From: kyori19 Date: Sat, 16 Mar 2019 22:33:16 +0900 Subject: [PATCH] Fix crash with unknown notification type (#1123) * Fix crash with unknown notification type * Add NotificationTypeAdapter to handle unknown type * Remove unneeded SerializedName --- .../tusky/adapter/NotificationsAdapter.java | 1 + .../tusky/entity/Notification.kt | 26 ++++++++++++++----- .../tusky/json/NotificationTypeAdapter.kt | 18 +++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/json/NotificationTypeAdapter.kt diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java index 528d7d84..d5649f4c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java @@ -136,6 +136,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { (NotificationViewData.Concrete) notification; Notification.Type type = concreteNotificaton.getType(); switch (type) { + default: case MENTION: { StatusViewHolder holder = (StatusViewHolder) viewHolder; StatusViewData.Concrete status = concreteNotificaton.getStatusViewData(); diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Notification.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Notification.kt index 32f5af4c..78f728a7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Notification.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Notification.kt @@ -15,7 +15,8 @@ package com.keylesspalace.tusky.entity -import com.google.gson.annotations.SerializedName +import com.google.gson.annotations.JsonAdapter +import com.keylesspalace.tusky.json.NotificationTypeAdapter data class Notification( val type: Type, @@ -23,15 +24,28 @@ data class Notification( val account: Account, val status: Status?) { + @JsonAdapter(NotificationTypeAdapter::class) enum class Type { - @SerializedName("mention") + UNKNOWN, MENTION, - @SerializedName("reblog") REBLOG, - @SerializedName("favourite") FAVOURITE, - @SerializedName("follow") - FOLLOW + FOLLOW; + + companion object { + + @JvmStatic + fun byString(s: String): Type { + return when (s) { + "mention" -> MENTION + "reblog" -> REBLOG + "favourite" -> FAVOURITE + "follow" -> FOLLOW + else -> UNKNOWN + } + } + + } } override fun hashCode(): Int { diff --git a/app/src/main/java/com/keylesspalace/tusky/json/NotificationTypeAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/json/NotificationTypeAdapter.kt new file mode 100644 index 00000000..44e47143 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/json/NotificationTypeAdapter.kt @@ -0,0 +1,18 @@ +package com.keylesspalace.tusky.json + +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import com.google.gson.JsonParseException +import com.keylesspalace.tusky.entity.Notification + +import java.lang.reflect.Type + +class NotificationTypeAdapter : JsonDeserializer { + + @Throws(JsonParseException::class) + override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Notification.Type { + return Notification.Type.byString(json.asString) + } + +}