@@ -4,9 +4,15 @@ package ai.pams.android.kotlin.models.notification
4
4
import ai.pams.android.kotlin.flex.parser.FlexParser
5
5
import ai.pams.android.kotlin.flex.parser.PImage
6
6
import ai.pams.android.kotlin.http.Http
7
+ import ai.pams.android.kotlin.utils.DateUtils
7
8
import android.content.Context
9
+ import android.os.Parcel
10
+ import android.os.Parcelable
11
+ import org.json.JSONObject
8
12
import org.threeten.bp.LocalDateTime
9
13
14
+
15
+
10
16
data class NotificationItem (
11
17
var date : LocalDateTime ? = null ,
12
18
val deliverId : String? = null ,
@@ -19,7 +25,7 @@ data class NotificationItem(
19
25
val title : String? = null ,
20
26
val url : String? = null ,
21
27
val popupType : String? = null ,
22
- ) {
28
+ ): Parcelable {
23
29
var bannerUrl: String? = null
24
30
25
31
@Deprecated(" createdDate is deprecated use date instead." , ReplaceWith (" date" ))
@@ -46,4 +52,100 @@ data class NotificationItem(
46
52
Http .getInstance().get(it)
47
53
}
48
54
}
55
+
56
+
57
+ // PARCEL IMPLEMENTATION
58
+ constructor (parcel: Parcel ) : this (
59
+ // date
60
+ convertStringToDate(parcel.readString() ),
61
+ // deliverId
62
+ parcel.readString(),
63
+ // description
64
+ parcel.readString(),
65
+ // flex
66
+ parcel.readString(),
67
+ // isOpen
68
+ parcel.readValue(Boolean ::class .java.classLoader) as ? Boolean ,
69
+ // payload
70
+ jsonToMap( parcel.readString()),
71
+ // pixel
72
+ parcel.readString(),
73
+ // thumbnailUrl
74
+ parcel.readString(),
75
+ // title
76
+ parcel.readString(),
77
+ // url
78
+ parcel.readString(),
79
+ // popupType
80
+ parcel.readString()
81
+ ) {
82
+ bannerUrl = parcel.readString()
83
+ }
84
+
85
+ override fun writeToParcel (parcel : Parcel , flags : Int ) {
86
+ // date
87
+ if ( date != null ){
88
+ var dateStr = " "
89
+ date?.let {
90
+ dateStr = DateUtils .localDateToServerFormat(it)
91
+ }
92
+ parcel.writeString(dateStr)
93
+ }else {
94
+ parcel.writeString(" -" )
95
+ }
96
+
97
+ parcel.writeString(deliverId)
98
+ parcel.writeString(description)
99
+ parcel.writeString(flex)
100
+ parcel.writeValue(isOpen)
101
+ parcel.writeValue(payload)
102
+ parcel.writeString(pixel)
103
+ parcel.writeString(thumbnailUrl)
104
+ parcel.writeString(title)
105
+ parcel.writeString(url)
106
+ parcel.writeString(popupType)
107
+ parcel.writeString(bannerUrl)
108
+ }
109
+
110
+ override fun describeContents (): Int {
111
+ return 0
112
+ }
113
+
114
+ companion object CREATOR : Parcelable.Creator<NotificationItem> {
115
+ override fun createFromParcel (parcel : Parcel ): NotificationItem {
116
+ return NotificationItem (parcel)
117
+ }
118
+
119
+ override fun newArray (size : Int ): Array <NotificationItem ?> {
120
+ return arrayOfNulls(size)
121
+ }
122
+
123
+ fun jsonToMap (jsonStr : String? ): Map <String , Any >? {
124
+ if (jsonStr == null ){
125
+ return null
126
+ }
127
+ val json = JSONObject (jsonStr)
128
+ val m = mutableMapOf<String , Any >()
129
+ json.keys().forEach {
130
+ if (json.get(it) is Int ){
131
+ m[it] = json.optInt(it)
132
+ }else if (json.get(it) is String ){
133
+ m[it] = json.optString(it)
134
+ }else if (json.get(it) is Boolean ){
135
+ m[it] = json.optBoolean(it)
136
+ }else if (json.get(it) is Double ){
137
+ m[it] = json.optDouble(it)
138
+ }else {
139
+ m[it] = json.get(it)
140
+ }
141
+ }
142
+ return m.toMap()
143
+ }
144
+ fun convertStringToDate (str : String? ): LocalDateTime ? {
145
+ str?.let {
146
+ return @convertStringToDate DateUtils .localDateTimeFromString(str)
147
+ }
148
+ return null
149
+ }
150
+ }
49
151
}
0 commit comments