add BML usd limits to dashboard and optmize login flow/session expirey
This commit is contained in:
@@ -82,6 +82,30 @@ class CredentialStore(context: Context) {
|
||||
.apply()
|
||||
}
|
||||
|
||||
fun saveBmlSession(accessToken: String, deviceId: String) {
|
||||
val key = getOrCreateKey()
|
||||
prefs.edit()
|
||||
.putString("bml_enc_token", encrypt(accessToken, key))
|
||||
.putString("bml_enc_device_id", encrypt(deviceId, key))
|
||||
.apply()
|
||||
}
|
||||
|
||||
fun loadBmlSession(): Pair<String, String>? {
|
||||
val key = getOrCreateKey()
|
||||
val encToken = prefs.getString("bml_enc_token", null) ?: return null
|
||||
val encDeviceId = prefs.getString("bml_enc_device_id", null) ?: return null
|
||||
return try {
|
||||
Pair(decrypt(encToken, key), decrypt(encDeviceId, key))
|
||||
} catch (_: Exception) { null }
|
||||
}
|
||||
|
||||
fun clearBmlSession() {
|
||||
prefs.edit()
|
||||
.remove("bml_enc_token")
|
||||
.remove("bml_enc_device_id")
|
||||
.apply()
|
||||
}
|
||||
|
||||
private fun getOrCreateKey(): SecretKey {
|
||||
val ks = KeyStore.getInstance("AndroidKeyStore").also { it.load(null) }
|
||||
ks.getKey(keyAlias, null)?.let { return it as SecretKey }
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package sh.sar.basedbank.util
|
||||
|
||||
import android.content.Context
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import sh.sar.basedbank.api.bml.BmlForeignLimit
|
||||
import sh.sar.basedbank.ui.home.HomeViewModel
|
||||
|
||||
object ForeignLimitsCache {
|
||||
|
||||
private const val PREFS = "foreign_limits_cache"
|
||||
private const val KEY = "bml_foreign_limits"
|
||||
|
||||
fun save(context: Context, entries: List<HomeViewModel.BmlLimitsData>) {
|
||||
val arr = JSONArray()
|
||||
for (entry in entries) {
|
||||
val limitsArr = JSONArray()
|
||||
for (l in entry.limits) {
|
||||
limitsArr.put(JSONObject().apply {
|
||||
put("type", l.type)
|
||||
put("used", l.used)
|
||||
put("totalLimit", l.totalLimit)
|
||||
put("generalCap", l.generalCap)
|
||||
put("generalRemaining", l.generalRemaining)
|
||||
put("medicalRemaining", l.medicalRemaining)
|
||||
put("isAtmEnabled", l.isAtmEnabled)
|
||||
put("isPosEnabled", l.isPosEnabled)
|
||||
put("atmRemaining", l.atmRemaining)
|
||||
put("atmLimit", l.atmLimit)
|
||||
put("ecomRemaining", l.ecomRemaining)
|
||||
put("ecomLimit", l.ecomLimit)
|
||||
put("posRemaining", l.posRemaining)
|
||||
put("posLimit", l.posLimit)
|
||||
})
|
||||
}
|
||||
arr.put(JSONObject().apply {
|
||||
put("userName", entry.userName)
|
||||
put("limits", limitsArr)
|
||||
})
|
||||
}
|
||||
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
.edit().putString(KEY, arr.toString()).apply()
|
||||
}
|
||||
|
||||
fun load(context: Context): List<HomeViewModel.BmlLimitsData> {
|
||||
val json = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
.getString(KEY, null) ?: return emptyList()
|
||||
return try {
|
||||
val arr = JSONArray(json)
|
||||
(0 until arr.length()).map { i ->
|
||||
val entry = arr.getJSONObject(i)
|
||||
val limitsArr = entry.optJSONArray("limits") ?: JSONArray()
|
||||
val limits = (0 until limitsArr.length()).map { j ->
|
||||
val l = limitsArr.getJSONObject(j)
|
||||
BmlForeignLimit(
|
||||
type = l.optString("type", "Debit"),
|
||||
used = l.optDouble("used", 0.0),
|
||||
totalLimit = l.optDouble("totalLimit", 0.0),
|
||||
generalCap = l.optDouble("generalCap", 0.0),
|
||||
generalRemaining = l.optDouble("generalRemaining", 0.0),
|
||||
medicalRemaining = l.optDouble("medicalRemaining", 0.0),
|
||||
isAtmEnabled = l.optBoolean("isAtmEnabled", false),
|
||||
isPosEnabled = l.optBoolean("isPosEnabled", false),
|
||||
atmRemaining = l.optDouble("atmRemaining", 0.0),
|
||||
atmLimit = l.optDouble("atmLimit", 0.0),
|
||||
ecomRemaining = l.optDouble("ecomRemaining", 0.0),
|
||||
ecomLimit = l.optDouble("ecomLimit", 0.0),
|
||||
posRemaining = l.optDouble("posRemaining", 0.0),
|
||||
posLimit = l.optDouble("posLimit", 0.0)
|
||||
)
|
||||
}
|
||||
HomeViewModel.BmlLimitsData(entry.optString("userName"), limits)
|
||||
}
|
||||
} catch (_: Exception) { emptyList() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user