forked from shihaam/thijooree
138 lines
5.9 KiB
Kotlin
138 lines
5.9 KiB
Kotlin
package sh.sar.basedbank.util
|
|
|
|
import android.content.Context
|
|
import org.json.JSONArray
|
|
import org.json.JSONObject
|
|
import sh.sar.basedbank.ui.home.AppNotification
|
|
|
|
object NotificationsCache {
|
|
|
|
private const val PREFS = "notifications_cache"
|
|
private const val KEY_MIB_READ_IDS = "mib_read_ids"
|
|
|
|
private fun bmlKey(loginId: String) = "bml_notifs_$loginId"
|
|
private fun mibKey(loginId: String) = "mib_activities_$loginId"
|
|
|
|
// ── BML ─────────────────────────────────────────────────────────────────────
|
|
|
|
fun saveBml(ctx: Context, loginId: String, items: List<AppNotification>) {
|
|
val arr = JSONArray()
|
|
items.forEach { n ->
|
|
arr.put(JSONObject().apply {
|
|
put("id", n.id)
|
|
put("group", n.group)
|
|
put("title", n.title)
|
|
put("message", n.message)
|
|
put("timestampMs", n.timestampMs)
|
|
put("isRead", n.isRead)
|
|
val fields = JSONArray()
|
|
n.detailFields.forEach { (k, v) -> fields.put(JSONObject().put("k", k).put("v", v)) }
|
|
put("detailFields", fields)
|
|
})
|
|
}
|
|
ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.edit().putString(bmlKey(loginId), CacheEncryption.encrypt(arr.toString())).apply()
|
|
}
|
|
|
|
fun loadBml(ctx: Context, loginId: String): List<AppNotification> {
|
|
val raw = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.getString(bmlKey(loginId), null) ?: return emptyList()
|
|
return try {
|
|
val arr = JSONArray(CacheEncryption.decrypt(raw))
|
|
(0 until arr.length()).map { i ->
|
|
val obj = arr.getJSONObject(i)
|
|
val fields = obj.optJSONArray("detailFields")
|
|
val detailFields = if (fields != null) {
|
|
(0 until fields.length()).map { j ->
|
|
val f = fields.getJSONObject(j)
|
|
f.getString("k") to f.getString("v")
|
|
}
|
|
} else emptyList()
|
|
AppNotification(
|
|
id = obj.getString("id"),
|
|
bank = "BML",
|
|
loginId = loginId,
|
|
group = obj.getString("group"),
|
|
title = obj.getString("title"),
|
|
message = obj.getString("message"),
|
|
timestampMs = obj.getLong("timestampMs"),
|
|
isRead = obj.getBoolean("isRead"),
|
|
detailFields = detailFields
|
|
)
|
|
}
|
|
} catch (_: Exception) { emptyList() }
|
|
}
|
|
|
|
// ── MIB ─────────────────────────────────────────────────────────────────────
|
|
|
|
fun saveMib(ctx: Context, loginId: String, items: List<AppNotification>) {
|
|
val arr = JSONArray()
|
|
items.forEach { n ->
|
|
arr.put(JSONObject().apply {
|
|
put("id", n.id)
|
|
put("title", n.title)
|
|
put("message", n.message)
|
|
put("timestampMs", n.timestampMs)
|
|
val fields = JSONArray()
|
|
n.detailFields.forEach { (k, v) -> fields.put(JSONObject().put("k", k).put("v", v)) }
|
|
put("detailFields", fields)
|
|
})
|
|
}
|
|
ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.edit().putString(mibKey(loginId), CacheEncryption.encrypt(arr.toString())).apply()
|
|
}
|
|
|
|
fun loadMib(ctx: Context, loginId: String, readIds: Set<String>): List<AppNotification> {
|
|
val raw = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.getString(mibKey(loginId), null) ?: return emptyList()
|
|
return try {
|
|
val arr = JSONArray(CacheEncryption.decrypt(raw))
|
|
(0 until arr.length()).map { i ->
|
|
val obj = arr.getJSONObject(i)
|
|
val id = obj.getString("id")
|
|
val fields = obj.optJSONArray("detailFields")
|
|
val detailFields = if (fields != null) {
|
|
(0 until fields.length()).map { j ->
|
|
val f = fields.getJSONObject(j)
|
|
f.getString("k") to f.getString("v")
|
|
}
|
|
} else emptyList()
|
|
AppNotification(
|
|
id = id,
|
|
bank = "MIB",
|
|
loginId = loginId,
|
|
group = "ALERTS",
|
|
title = obj.getString("title"),
|
|
message = obj.getString("message"),
|
|
timestampMs = obj.getLong("timestampMs"),
|
|
isRead = id in readIds,
|
|
detailFields = detailFields
|
|
)
|
|
}
|
|
} catch (_: Exception) { emptyList() }
|
|
}
|
|
|
|
// ── MIB read IDs (in-app only) ───────────────────────────────────────────────
|
|
|
|
fun getMibReadIds(ctx: Context): Set<String> {
|
|
val raw = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.getString(KEY_MIB_READ_IDS, null) ?: return emptySet()
|
|
return try {
|
|
val arr = JSONArray(raw)
|
|
(0 until arr.length()).map { arr.getString(it) }.toSet()
|
|
} catch (_: Exception) { emptySet() }
|
|
}
|
|
|
|
fun addMibReadIds(ctx: Context, ids: Collection<String>) {
|
|
val current = getMibReadIds(ctx).toMutableSet()
|
|
current.addAll(ids)
|
|
val arr = JSONArray().apply { current.forEach { put(it) } }
|
|
ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.edit().putString(KEY_MIB_READ_IDS, arr.toString()).apply()
|
|
}
|
|
|
|
fun clearAll(ctx: Context) {
|
|
ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().clear().apply()
|
|
}
|
|
}
|