forked from thijooree/android
71 lines
2.9 KiB
Kotlin
71 lines
2.9 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.MibFinanceDeal
|
|
|
|
object FinancingCache {
|
|
|
|
private const val PREFS = "financing_cache"
|
|
private const val KEY_MIB = "mib_financing"
|
|
|
|
fun save(context: Context, deals: List<MibFinanceDeal>) {
|
|
val arr = JSONArray()
|
|
for (d in deals) {
|
|
arr.put(JSONObject().apply {
|
|
put("dealNo", d.dealNo)
|
|
put("productDesc", d.productDesc)
|
|
put("dealStatus", d.dealStatus)
|
|
put("statusDesc", d.statusDesc)
|
|
put("dealAmount", d.dealAmount)
|
|
put("paidAmount", d.paidAmount)
|
|
put("outstandingAmount", d.outstandingAmount)
|
|
put("dealDate", d.dealDate)
|
|
put("overdueAmount", d.overdueAmount)
|
|
put("installmentAmount", d.installmentAmount)
|
|
put("noOfInstallments", d.noOfInstallments)
|
|
put("lastPaidDate", d.lastPaidDate)
|
|
put("lastPayAmount", d.lastPayAmount)
|
|
put("currency", d.currency)
|
|
})
|
|
}
|
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.edit().putString(KEY_MIB, CacheEncryption.encrypt(arr.toString())).apply()
|
|
}
|
|
|
|
fun clear(context: Context) {
|
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().clear().apply()
|
|
}
|
|
|
|
fun load(context: Context): List<MibFinanceDeal> {
|
|
val raw = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
.getString(KEY_MIB, 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)
|
|
MibFinanceDeal(
|
|
dealNo = o.optString("dealNo"),
|
|
productDesc = o.optString("productDesc"),
|
|
dealStatus = o.optString("dealStatus"),
|
|
statusDesc = o.optString("statusDesc"),
|
|
dealAmount = o.optDouble("dealAmount", 0.0),
|
|
paidAmount = o.optDouble("paidAmount", 0.0),
|
|
outstandingAmount = o.optDouble("outstandingAmount", 0.0),
|
|
dealDate = o.optString("dealDate"),
|
|
overdueAmount = o.optDouble("overdueAmount", 0.0),
|
|
installmentAmount = o.optDouble("installmentAmount", 0.0),
|
|
noOfInstallments = o.optInt("noOfInstallments", 0),
|
|
lastPaidDate = o.optString("lastPaidDate"),
|
|
lastPayAmount = o.optDouble("lastPayAmount", 0.0),
|
|
currency = o.optString("currency", "MVR")
|
|
)
|
|
}
|
|
} catch (e: Exception) {
|
|
emptyList()
|
|
}
|
|
}
|
|
}
|