forked from thijooree/android
121 lines
5.2 KiB
Kotlin
121 lines
5.2 KiB
Kotlin
package sh.sar.basedbank.util
|
|
|
|
import android.content.Context
|
|
import org.json.JSONArray
|
|
import org.json.JSONObject
|
|
import sh.sar.basedbank.api.mib.MibAccount
|
|
|
|
object AccountCache {
|
|
|
|
private const val PREFS = "account_cache"
|
|
private const val KEY_MIB = "mib_accounts"
|
|
private const val KEY_BML = "bml_accounts"
|
|
|
|
fun save(context: Context, accounts: List<MibAccount>) {
|
|
val arr = JSONArray()
|
|
for (acc in accounts) {
|
|
arr.put(JSONObject().apply {
|
|
put("profileName", acc.profileName)
|
|
put("profileType", acc.profileType)
|
|
put("accountNumber", acc.accountNumber)
|
|
put("accountBriefName", acc.accountBriefName)
|
|
put("currencyName", acc.currencyName)
|
|
put("accountTypeName", acc.accountTypeName)
|
|
put("availableBalance", acc.availableBalance)
|
|
put("currentBalance", acc.currentBalance)
|
|
put("blockedAmount", acc.blockedAmount)
|
|
put("mvrBalance", acc.mvrBalance)
|
|
put("statusDesc", acc.statusDesc)
|
|
put("loginTag", acc.loginTag)
|
|
put("profileId", acc.profileId)
|
|
if (acc.profileImageHash != null) put("profileImageHash", acc.profileImageHash)
|
|
})
|
|
}
|
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.edit().putString(KEY_MIB, arr.toString()).apply()
|
|
}
|
|
|
|
fun saveBml(context: Context, accounts: List<MibAccount>) {
|
|
val arr = JSONArray()
|
|
for (acc in accounts) {
|
|
arr.put(JSONObject().apply {
|
|
put("profileName", acc.profileName)
|
|
put("profileType", acc.profileType)
|
|
put("accountNumber", acc.accountNumber)
|
|
put("accountBriefName", acc.accountBriefName)
|
|
put("currencyName", acc.currencyName)
|
|
put("accountTypeName", acc.accountTypeName)
|
|
put("availableBalance", acc.availableBalance)
|
|
put("currentBalance", acc.currentBalance)
|
|
put("blockedAmount", acc.blockedAmount)
|
|
put("mvrBalance", acc.mvrBalance)
|
|
put("statusDesc", acc.statusDesc)
|
|
put("loginTag", acc.loginTag)
|
|
put("internalId", acc.internalId)
|
|
})
|
|
}
|
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.edit().putString(KEY_BML, arr.toString()).apply()
|
|
}
|
|
|
|
fun loadBml(context: Context): List<MibAccount> {
|
|
val json = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.getString(KEY_BML, null) ?: return emptyList()
|
|
return try {
|
|
val arr = JSONArray(json)
|
|
(0 until arr.length()).map { i ->
|
|
val o = arr.getJSONObject(i)
|
|
MibAccount(
|
|
profileName = o.optString("profileName"),
|
|
profileType = o.optString("profileType"),
|
|
accountNumber = o.optString("accountNumber"),
|
|
accountBriefName = o.optString("accountBriefName"),
|
|
currencyName = o.optString("currencyName"),
|
|
accountTypeName = o.optString("accountTypeName"),
|
|
availableBalance = o.optString("availableBalance"),
|
|
currentBalance = o.optString("currentBalance"),
|
|
blockedAmount = o.optString("blockedAmount"),
|
|
mvrBalance = o.optString("mvrBalance"),
|
|
statusDesc = o.optString("statusDesc"),
|
|
profileImageHash = null,
|
|
loginTag = o.optString("loginTag"),
|
|
internalId = o.optString("internalId", "")
|
|
)
|
|
}
|
|
} catch (e: Exception) { emptyList() }
|
|
}
|
|
|
|
fun clear(context: Context) {
|
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().clear().apply()
|
|
}
|
|
|
|
fun load(context: Context): List<MibAccount> {
|
|
val json = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.getString(KEY_MIB, null) ?: return emptyList()
|
|
return try {
|
|
val arr = JSONArray(json)
|
|
(0 until arr.length()).map { i ->
|
|
val o = arr.getJSONObject(i)
|
|
MibAccount(
|
|
profileName = o.optString("profileName"),
|
|
profileType = o.optString("profileType"),
|
|
accountNumber = o.optString("accountNumber"),
|
|
accountBriefName = o.optString("accountBriefName"),
|
|
currencyName = o.optString("currencyName"),
|
|
accountTypeName = o.optString("accountTypeName"),
|
|
availableBalance = o.optString("availableBalance"),
|
|
currentBalance = o.optString("currentBalance"),
|
|
blockedAmount = o.optString("blockedAmount"),
|
|
mvrBalance = o.optString("mvrBalance"),
|
|
statusDesc = o.optString("statusDesc"),
|
|
profileImageHash = o.optString("profileImageHash").takeIf { it.isNotBlank() },
|
|
loginTag = o.optString("loginTag"),
|
|
profileId = o.optString("profileId", "")
|
|
)
|
|
}
|
|
} catch (e: Exception) {
|
|
emptyList()
|
|
}
|
|
}
|
|
}
|