tmp
This commit is contained in:
parent
7898a51252
commit
8fbe3c0b7b
21 changed files with 359 additions and 35 deletions
|
|
@ -1,6 +1,6 @@
|
|||
package com.pixelized.biblib.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.app.Application
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.util.Log
|
||||
|
|
@ -12,12 +12,11 @@ import java.io.File
|
|||
import java.io.IOException
|
||||
import java.net.URL
|
||||
|
||||
object BitmapCache {
|
||||
private val scope = CoroutineScope(Dispatchers.IO)
|
||||
class BitmapCache(application: Application) {
|
||||
private var cache: File? = null
|
||||
|
||||
fun init(context: Context) {
|
||||
cache = context.cacheDir
|
||||
init {
|
||||
cache = application.cacheDir
|
||||
}
|
||||
|
||||
fun writeToDisk(url: URL, bitmap: Bitmap) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package com.pixelized.biblib.utils.exception
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class InjectionException(clazz: KClass<*>) : RuntimeException("InjectionException occur for class:${clazz}")
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package com.pixelized.biblib.utils.exception
|
||||
|
||||
class NoBearerException : RuntimeException("Bearer token is null")
|
||||
|
|
@ -5,18 +5,19 @@ import androidx.compose.runtime.mutableStateOf
|
|||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.painter.BitmapPainter
|
||||
import androidx.compose.ui.graphics.painter.Painter
|
||||
import com.pixelized.biblib.injection.get
|
||||
import com.pixelized.biblib.utils.BitmapCache
|
||||
import java.net.URL
|
||||
|
||||
|
||||
fun URL.toImage(placeHolder: Painter): State<Painter> {
|
||||
val cached = BitmapCache.readFromDisk(this)?.let { BitmapPainter(it.asImageBitmap()) }
|
||||
val state = mutableStateOf(cached ?: placeHolder)
|
||||
val cache: BitmapCache = get()
|
||||
val resource = cache.readFromDisk(this)?.let { BitmapPainter(it.asImageBitmap()) }
|
||||
val state = mutableStateOf(resource ?: placeHolder)
|
||||
|
||||
if (cached == null) {
|
||||
BitmapCache.download(url = this) { downloaded ->
|
||||
if (resource == null) {
|
||||
cache.download(url = this) { downloaded ->
|
||||
if (downloaded != null) {
|
||||
BitmapCache.writeToDisk(this, downloaded)
|
||||
cache.writeToDisk(this, downloaded)
|
||||
state.value = BitmapPainter(downloaded.asImageBitmap())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue