This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package sh.sar.basedbank.util
|
||||
|
||||
import android.content.Context
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import sh.sar.basedbank.api.mib.MibCard
|
||||
|
||||
object CardsCache {
|
||||
|
||||
private const val PREFS = "cards_cache"
|
||||
private const val KEY_MIB_CARDS = "mib_cards"
|
||||
|
||||
fun save(context: Context, cards: List<MibCard>) {
|
||||
val arr = JSONArray()
|
||||
for (c in cards) {
|
||||
arr.put(JSONObject().apply {
|
||||
put("cardId", c.cardId)
|
||||
put("maskedCardNumber", c.maskedCardNumber)
|
||||
put("cardStatus", c.cardStatus)
|
||||
put("cardType", c.cardType)
|
||||
put("cardTypeDesc", c.cardTypeDesc)
|
||||
put("customerId", c.customerId)
|
||||
put("phoneNumber", c.phoneNumber)
|
||||
put("cardHolderName", c.cardHolderName)
|
||||
put("loginTag", c.loginTag)
|
||||
})
|
||||
}
|
||||
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
.edit().putString(KEY_MIB_CARDS, CacheEncryption.encrypt(arr.toString())).apply()
|
||||
}
|
||||
|
||||
fun load(context: Context): List<MibCard> {
|
||||
val raw = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
.getString(KEY_MIB_CARDS, null) ?: return emptyList()
|
||||
return try {
|
||||
val arr = JSONArray(CacheEncryption.decrypt(raw))
|
||||
(0 until arr.length()).map { i ->
|
||||
val o = arr.getJSONObject(i)
|
||||
MibCard(
|
||||
cardId = o.optString("cardId"),
|
||||
maskedCardNumber = o.optString("maskedCardNumber"),
|
||||
cardStatus = o.optString("cardStatus"),
|
||||
cardType = o.optString("cardType"),
|
||||
cardTypeDesc = o.optString("cardTypeDesc"),
|
||||
customerId = o.optString("customerId"),
|
||||
phoneNumber = o.optString("phoneNumber"),
|
||||
cardHolderName = o.optString("cardHolderName"),
|
||||
loginTag = o.optString("loginTag")
|
||||
)
|
||||
}
|
||||
} catch (_: Exception) { emptyList() }
|
||||
}
|
||||
|
||||
fun clear(context: Context) {
|
||||
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().clear().apply()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user