package sh.sar.basedbank.util import android.content.Context import org.json.JSONArray import org.json.JSONObject data class RecentPick( val accountNumber: String, val displayName: String, val subtitle: String, val colorHex: String, val imageHash: String?, val isProfileImage: Boolean, /** Source bank tag for the recent — e.g. "MFAISA". Used by the picker to decide * per-bank selectability. Null for legacy entries; treated as unspecified. */ val bank: String? = null, /** The contact category the pick was paid as — set for Fahipay payouts (e.g. * "FAHIPAY_RAASTAS") so picking the recent again applies the same service. */ val contactCategory: String? = null ) object RecentsCache { private const val PREFS = "recents_cache" private const val KEY = "contact_recents" private const val MAX = 10 fun save(context: Context, pick: RecentPick) { val existing = load(context).toMutableList() existing.removeAll { it.accountNumber == pick.accountNumber } existing.add(0, pick) if (existing.size > MAX) existing.subList(MAX, existing.size).clear() val arr = JSONArray() for (r in existing) { arr.put(JSONObject().apply { put("accountNumber", r.accountNumber) put("displayName", r.displayName) put("subtitle", r.subtitle) put("colorHex", r.colorHex) if (r.imageHash != null) put("imageHash", r.imageHash) put("isProfileImage", r.isProfileImage) if (r.bank != null) put("bank", r.bank) if (r.contactCategory != null) put("contactCategory", r.contactCategory) }) } context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) .edit().putString(KEY, CacheEncryption.encrypt(arr.toString())).apply() } fun remove(context: Context, accountNumber: String) { val updated = load(context).filter { it.accountNumber != accountNumber } val arr = JSONArray() for (r in updated) { arr.put(JSONObject().apply { put("accountNumber", r.accountNumber) put("displayName", r.displayName) put("subtitle", r.subtitle) put("colorHex", r.colorHex) if (r.imageHash != null) put("imageHash", r.imageHash) put("isProfileImage", r.isProfileImage) if (r.bank != null) put("bank", r.bank) if (r.contactCategory != null) put("contactCategory", r.contactCategory) }) } context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) .edit().putString(KEY, CacheEncryption.encrypt(arr.toString())).apply() } fun clear(context: Context) { context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().clear().apply() } fun load(context: Context): List { val raw = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) .getString(KEY, null) ?: return emptyList() return try { val json = CacheEncryption.decrypt(raw) val arr = JSONArray(json) (0 until arr.length()).map { i -> val o = arr.getJSONObject(i) RecentPick( accountNumber = o.getString("accountNumber"), displayName = o.getString("displayName"), subtitle = o.getString("subtitle"), colorHex = o.getString("colorHex"), imageHash = o.optString("imageHash").takeIf { it.isNotBlank() }, isProfileImage = o.optBoolean("isProfileImage", false), bank = o.optString("bank").takeIf { it.isNotBlank() }, contactCategory = o.optString("contactCategory").takeIf { it.isNotBlank() } ) } } catch (_: Exception) { emptyList() } } }