added support for custom per-profile image for BML and Fahipay, MIB works pending
Auto Tag on Version Change / check-version (push) Successful in 7s
Auto Tag on Version Change / check-version (push) Successful in 7s
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package sh.sar.basedbank.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Stores and loads profile images locally for BML and Fahipay accounts.
|
||||
*
|
||||
* Key conventions:
|
||||
* BML profile: "bml_${profileId}"
|
||||
* Fahipay login: "fahipay_${loginId}"
|
||||
*/
|
||||
object ProfileImageStore {
|
||||
|
||||
private fun dir(context: Context): File =
|
||||
File(context.filesDir, "profile_images").also { it.mkdirs() }
|
||||
|
||||
private fun file(context: Context, key: String): File =
|
||||
File(dir(context), "${key.replace(Regex("[^A-Za-z0-9_\\-]"), "_")}.jpg")
|
||||
|
||||
fun save(context: Context, key: String, bitmap: Bitmap) {
|
||||
try {
|
||||
file(context, key).outputStream().use {
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it)
|
||||
}
|
||||
} catch (_: Exception) {}
|
||||
}
|
||||
|
||||
fun load(context: Context, key: String): Bitmap? = try {
|
||||
val f = file(context, key)
|
||||
if (f.exists()) BitmapFactory.decodeFile(f.absolutePath) else null
|
||||
} catch (_: Exception) { null }
|
||||
|
||||
fun delete(context: Context, key: String) {
|
||||
try { file(context, key).delete() } catch (_: Exception) {}
|
||||
}
|
||||
|
||||
fun exists(context: Context, key: String): Boolean =
|
||||
try { file(context, key).exists() } catch (_: Exception) { false }
|
||||
|
||||
fun clearAll(context: Context) {
|
||||
try { dir(context).listFiles()?.forEach { it.delete() } } catch (_: Exception) {}
|
||||
}
|
||||
|
||||
/** Key for a BML profile given its profileId. */
|
||||
fun bmlKey(profileId: String) = "bml_$profileId"
|
||||
|
||||
/** Key for a Fahipay login given its loginId. */
|
||||
fun fahipayKey(loginId: String) = "fahipay_$loginId"
|
||||
|
||||
/** Derives the loginId from a loginTag (e.g. "fahipay_abc" → "abc"). */
|
||||
fun loginIdFromTag(loginTag: String): String =
|
||||
loginTag.substringAfter('_', loginTag)
|
||||
}
|
||||
Reference in New Issue
Block a user