Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,57 @@ import org.commcare.android.storage.framework.Persisted
import org.commcare.models.framework.Persisting
import org.commcare.modern.database.Table
import org.commcare.modern.models.MetaField
import org.javarosa.core.model.utils.DateUtils
import org.json.JSONArray
import java.io.Serializable
import java.util.Date

@Table(PushNotificationRecord.STORAGE_KEY)
class PushNotificationRecord : Persisted(), Serializable {

@Persisting(1)
@MetaField(META_NOTIFICATION_ID)
var notificationId: Int = 0
var notificationId: String = ""

@Persisting(2)
@MetaField(META_NOTIFICATION_TYPE)
var notificationType: String? = null
var notificationType: String = ""

@Persisting(3)
@MetaField(META_ACTION)
var action: String = ""

@Persisting(4)
@MetaField(META_TITLE)
var title: String? = null
var title: String = ""

@Persisting(5)
@MetaField(META_BODY)
var body: String? = null
var body: String = ""

@Persisting(6)
@MetaField(META_DATE_TIME)
var dateTime: String? = null
@MetaField(META_CREATED_DATE)
var createdDate: Date = Date()

@Persisting(7)
@MetaField(META_CONFIRMATION_STATUS)
var confirmationStatus: String? = null
var confirmationStatus: String = ""

@Persisting(8)
@MetaField(META_OPPORTUNITY_ID)
var opportunityId: Int? = null
var opportunityId: String = ""

@Persisting(9)
@MetaField(META_MESSAGE_ID)
var connectMessageId: Int? = null
var connectMessageId: String = ""

@Persisting(10)
@MetaField(META_CHANNEL)
var channel: Int? = null
var channel: String = ""

@Persisting(11)
@MetaField(META_PAYMENT_ID)
var paymentId: Int? = null
var paymentId: String = ""

@Persisting(12)
@MetaField(META_READ_STATUS)
Expand All @@ -65,12 +68,46 @@ class PushNotificationRecord : Persisted(), Serializable {
const val META_ACTION = "action"
const val META_TITLE = "title"
const val META_BODY = "body"
const val META_DATE_TIME = "date_time"
const val META_CREATED_DATE = "created_date"
const val META_CONFIRMATION_STATUS = "confirmation_status"
const val META_OPPORTUNITY_ID = "opportunity_id"
const val META_MESSAGE_ID = "message_id"
const val META_CHANNEL = "channel"
const val META_PAYMENT_ID = "payment_id"
const val META_READ_STATUS = "read_status"

const val META_TIME_STAMP = "timestamp"


fun fromJsonArray(jsonArray: JSONArray): List<PushNotificationRecord> {
val records = mutableListOf<PushNotificationRecord>()

for (i in 0 until jsonArray.length()) {
val obj = jsonArray.getJSONObject(i)
val record = PushNotificationRecord().apply {
notificationId = obj.optString(META_NOTIFICATION_ID,"")
title = obj.optString(META_TITLE,"")
body = obj.optString(META_BODY,"")
notificationType = obj.optString(META_NOTIFICATION_TYPE, "")
confirmationStatus = obj.optString(META_CONFIRMATION_STATUS, "")
paymentId = obj.optString(META_PAYMENT_ID, "")
readStatus = obj.optBoolean(META_READ_STATUS, false)

val dateString: String = obj.getString(META_TIME_STAMP)
createdDate = DateUtils.parseDateTime(dateString)

val dataObj = obj.optJSONObject("data")
dataObj?.let {
connectMessageId = it.optString(META_MESSAGE_ID, "")
channel = it.optString(META_CHANNEL, "")
action = it.optString(META_ACTION, "")
opportunityId = it.optString(META_OPPORTUNITY_ID, "")
}
}
records.add(record)
}

return records
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class NotificationRecordDatabaseHelper {
/**
* Fetch a notification from notification_id
*/
fun getNotificationById(context: Context, notificationId: Int): PushNotificationRecord? {
fun getNotificationById(context: Context, notificationId: String): PushNotificationRecord? {
val records = getStorage(context).getRecordsForValues(
arrayOf(PushNotificationRecord.META_NOTIFICATION_ID),
arrayOf(notificationId)
Expand All @@ -31,7 +31,7 @@ class NotificationRecordDatabaseHelper {
/**
* Update the read status for a notification using notification_id
*/
fun updateReadStatus(context: Context, notificationId: Int, isRead: Boolean) {
fun updateReadStatus(context: Context, notificationId: String, isRead: Boolean) {
val record = getNotificationById(context, notificationId) ?: return
record.readStatus = isRead
getStorage(context).write(record)
Expand All @@ -40,12 +40,14 @@ class NotificationRecordDatabaseHelper {
/**
* Append notification(s) to DB (insert or update)
*/
fun storeNotifications(context: Context, notifications: List<PushNotificationRecord>) {
fun storeNotifications(
context: Context,
notifications: List<PushNotificationRecord>,
) {
val storage = getStorage(context)

for (incoming in notifications) {
val existing = getNotificationById(context, incoming.notificationId)
if (existing != null) {
getNotificationById(context, incoming.notificationId)?.let { existing ->
incoming.id = existing.id
}
storage.write(incoming)
Expand Down
Loading