Fix crash with unknown notification type (#1123)

* Fix crash with unknown notification type

* Add NotificationTypeAdapter to handle unknown type

* Remove unneeded SerializedName
This commit is contained in:
kyori19 2019-03-16 22:33:16 +09:00 committed by Konrad Pozniak
parent 4f7c989b2d
commit d0f7f6f83c
3 changed files with 39 additions and 6 deletions

View File

@ -136,6 +136,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
(NotificationViewData.Concrete) notification; (NotificationViewData.Concrete) notification;
Notification.Type type = concreteNotificaton.getType(); Notification.Type type = concreteNotificaton.getType();
switch (type) { switch (type) {
default:
case MENTION: { case MENTION: {
StatusViewHolder holder = (StatusViewHolder) viewHolder; StatusViewHolder holder = (StatusViewHolder) viewHolder;
StatusViewData.Concrete status = concreteNotificaton.getStatusViewData(); StatusViewData.Concrete status = concreteNotificaton.getStatusViewData();

View File

@ -15,7 +15,8 @@
package com.keylesspalace.tusky.entity 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( data class Notification(
val type: Type, val type: Type,
@ -23,15 +24,28 @@ data class Notification(
val account: Account, val account: Account,
val status: Status?) { val status: Status?) {
@JsonAdapter(NotificationTypeAdapter::class)
enum class Type { enum class Type {
@SerializedName("mention") UNKNOWN,
MENTION, MENTION,
@SerializedName("reblog")
REBLOG, REBLOG,
@SerializedName("favourite")
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 { override fun hashCode(): Int {

View File

@ -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<Notification.Type> {
@Throws(JsonParseException::class)
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Notification.Type {
return Notification.Type.byString(json.asString)
}
}