Add support for deserializing NEW_STUFF and UNALLOCATED_STATS_POINTS notifications

This commit is contained in:
Carl Vuorinen 2019-04-09 22:07:33 +03:00
parent 090ab372d0
commit 3361a8e45f
5 changed files with 48 additions and 14 deletions

View file

@ -2,9 +2,12 @@ package com.habitrpg.android.habitica.models.notifications
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.lang.reflect.Type
enum class NotificationType(val type: String) {
NEW_CHAT_MESSAGE("NEW_CHAT_MESSAGE");
NEW_STUFF("NEW_STUFF"),
NEW_CHAT_MESSAGE("NEW_CHAT_MESSAGE"),
UNALLOCATED_STATS_POINTS("UNALLOCATED_STATS_POINTS");
companion object {
fun contains(type: String?): Boolean {
@ -28,12 +31,33 @@ open class GlobalNotification : RealmObject() {
var type: String? = null
var seen: Boolean? = null
var newChatMessageData: ChatNotificationData? = null
var newStuffData: NewStuffData? = null
var newChatMessageData: NewChatMessageData? = null
var unallocatedPointsData: UnallocatedPointsData? = null
// Workaround for Realms lack of polymorphism
fun getData(): GlobalNotificationData? {
return when(type) {
return when (type) {
NotificationType.NEW_STUFF.type -> newStuffData
NotificationType.NEW_CHAT_MESSAGE.type -> newChatMessageData
NotificationType.UNALLOCATED_STATS_POINTS.type -> unallocatedPointsData
else -> null
}
}
fun setData(data: GlobalNotificationData?) {
when (type) {
NotificationType.NEW_STUFF.type -> newStuffData = data as NewStuffData?
NotificationType.NEW_CHAT_MESSAGE.type -> newChatMessageData = data as NewChatMessageData?
NotificationType.UNALLOCATED_STATS_POINTS.type -> unallocatedPointsData = data as UnallocatedPointsData?
}
}
fun getDataType(): Type? {
return when (type) {
NotificationType.NEW_STUFF.type -> NewStuffData::class.java
NotificationType.NEW_CHAT_MESSAGE.type -> NewChatMessageData::class.java
NotificationType.UNALLOCATED_STATS_POINTS.type -> UnallocatedPointsData::class.java
else -> null
}
}

View file

@ -2,6 +2,6 @@ package com.habitrpg.android.habitica.models.notifications
import io.realm.RealmObject
open class ChatNotificationData : RealmObject(), GlobalNotificationData {
open class NewChatMessageData : RealmObject(), GlobalNotificationData {
var group: NotificationGroup? = null
}

View file

@ -0,0 +1,7 @@
package com.habitrpg.android.habitica.models.notifications
import io.realm.RealmObject
open class NewStuffData : RealmObject(), GlobalNotificationData {
var title: String? = null
}

View file

@ -0,0 +1,7 @@
package com.habitrpg.android.habitica.models.notifications
import io.realm.RealmObject
open class UnallocatedPointsData : RealmObject(), GlobalNotificationData {
var points: Int? = null
}

View file

@ -4,9 +4,7 @@ import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import com.habitrpg.android.habitica.models.notifications.ChatNotificationData
import com.habitrpg.android.habitica.models.notifications.GlobalNotification
import com.habitrpg.android.habitica.models.notifications.NotificationType
import com.habitrpg.android.habitica.models.notifications.*
import java.lang.reflect.Type
class NotificationDeserializer : JsonDeserializer<GlobalNotification> {
@ -27,13 +25,11 @@ class NotificationDeserializer : JsonDeserializer<GlobalNotification> {
notification.seen = obj.get("seen").asBoolean
}
if (obj.has("data")) {
when (notification.type) {
NotificationType.NEW_CHAT_MESSAGE.type -> notification.newChatMessageData = context.deserialize<ChatNotificationData>(
obj.getAsJsonObject("data"),
ChatNotificationData::class.java
)
}
val dataType = notification.getDataType()
if (obj.has("data") && dataType != null) {
notification.setData(
context.deserialize(obj.getAsJsonObject("data"), dataType)
)
}
return notification