Skip to content

Commit 3e96286

Browse files
committed
fixed Notification model
1 parent f1077a7 commit 3e96286

File tree

4 files changed

+83
-24
lines changed

4 files changed

+83
-24
lines changed

app/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ android {
3333

3434
dependencies {
3535

36-
implementation 'androidx.core:core-ktx:1.7.0'
37-
implementation 'androidx.appcompat:appcompat:1.4.1'
38-
implementation 'com.google.android.material:material:1.5.0'
39-
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
36+
implementation 'androidx.core:core-ktx:1.8.0'
37+
implementation 'androidx.appcompat:appcompat:1.4.2'
38+
implementation 'com.google.android.material:material:1.6.1'
39+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4040
testImplementation 'junit:junit:4.13.2'
4141
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4242
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
43+
4344
}

pam-android-kotlin/src/main/java/ai/pams/android/kotlin/api/NotificationAPI.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ class NotificationAPI {
6363
url = endpoint,
6464
queryString = queryString
6565
) { result, _ ->
66-
val model = createGson().fromJson(result, NotificationList::class.java)
67-
model.items?.forEach {
68-
it.parseFlex(context)
69-
}
66+
val model = NotificationList.parse(result, context);
7067
CoroutineScope(Dispatchers.Main).launch {
7168
callBack?.invoke(model.items ?: listOf())
7269
}
Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,79 @@
11
package ai.pams.android.kotlin.models.notification
22

3-
import com.google.gson.annotations.SerializedName
3+
import android.content.Context
4+
import android.os.Build
5+
import androidx.annotation.RequiresApi
6+
import org.json.JSONObject
7+
import java.text.ParseException
8+
import java.text.SimpleDateFormat
9+
import java.time.LocalDateTime
10+
import java.util.*
411

512
class NotificationList(
6-
@SerializedName("items") val items: List<NotificationItem>? = null
7-
)
13+
var items: List<NotificationItem>? = null
14+
){
15+
companion object{
16+
17+
private fun stringToDate(dateString: String): Date? {
18+
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
19+
try {
20+
return format.parse(dateString)
21+
} catch (e: ParseException) {
22+
e.printStackTrace()
23+
}
24+
return null
25+
}
26+
27+
fun parse(jsonString: String?, context: Context): NotificationList{
28+
if(jsonString == null){
29+
val noti = NotificationList()
30+
noti.items = listOf()
31+
return noti
32+
}
33+
34+
val noti = NotificationList();
35+
val list = mutableListOf<NotificationItem>()
36+
37+
val json = JSONObject(jsonString)
38+
val itemsArray = json.optJSONArray("items")
39+
val count = itemsArray?.length() ?: 0
40+
for(i in 0 until count){
41+
itemsArray?.getJSONObject(i)?.let{ jsonItem->
42+
43+
val createdDate = jsonItem.optString("created_date")
44+
45+
var jsonPayload = jsonItem.optJSONObject("json_data");
46+
jsonPayload = jsonPayload?.optJSONObject("pam")
47+
val payload = mutableMapOf<String, Any>()
48+
49+
val iterator = jsonPayload?.keys()
50+
while(iterator?.hasNext() == true){
51+
val key = iterator.next()
52+
jsonPayload?.get(key)?.let{ data ->
53+
payload[key] = data
54+
}
55+
}
56+
57+
val item = NotificationItem(
58+
date = stringToDate(createdDate),
59+
deliverId = jsonItem.optString("deliver_id"),
60+
description = jsonItem.optString("description"),
61+
flex = jsonItem.optString("flex"),
62+
isOpen = jsonItem.optBoolean("is_open"),
63+
payload = payload,
64+
pixel = jsonItem.optString("pixel"),
65+
thumbnailUrl = jsonItem.optString("thumbnail_url"),
66+
title = jsonItem.optString("title"),
67+
url = jsonItem.optString("title"),
68+
popupType = jsonPayload?.optString("popupType")
69+
)
70+
item.parseFlex(context)
71+
list.add(item)
72+
}
73+
}
74+
75+
noti.items = list
76+
return noti
77+
}
78+
}
79+
}

pam-android-kotlin/src/main/java/ai/pams/android/kotlin/models/notification/NotificationItem.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,16 @@ import com.google.gson.annotations.SerializedName
99
import java.util.*
1010

1111
data class NotificationItem(
12-
@SerializedName("created_date")
13-
val date: Date? = null,
14-
@SerializedName("deliver_id")
12+
var date: Date? = null,
1513
val deliverId: String? = null,
16-
@SerializedName("description")
1714
val description: String? = null,
18-
@SerializedName("flex")
1915
val flex: String? = null,
20-
@SerializedName("is_open")
2116
val isOpen: Boolean? = null,
22-
@SerializedName("json_data")
23-
val payload: Payload? = null,
24-
@SerializedName("pixel")
17+
val payload: Map<String, Any>? = null,
2518
val pixel: String? = null,
26-
@SerializedName("thumbnail_url")
2719
val thumbnailUrl: String? = null,
28-
@SerializedName("title")
2920
val title: String? = null,
30-
@SerializedName("url")
3121
val url: String? = null,
32-
@SerializedName("popupType")
3322
val popupType: String? = null,
3423
) {
3524
var bannerUrl: String? = null

0 commit comments

Comments
 (0)