Speeds up some of image calculations
This commit is contained in:
@@ -53,7 +53,7 @@ class Amethyst : Application() {
|
|||||||
newCache
|
newCache
|
||||||
}
|
}
|
||||||
|
|
||||||
private val imageCache: DiskCache by lazy {
|
val coilCache: DiskCache by lazy {
|
||||||
DiskCache.Builder()
|
DiskCache.Builder()
|
||||||
.directory(applicationContext.safeCacheDir.resolve("image_cache"))
|
.directory(applicationContext.safeCacheDir.resolve("image_cache"))
|
||||||
.maxSizePercent(0.2)
|
.maxSizePercent(0.2)
|
||||||
@@ -85,7 +85,7 @@ class Amethyst : Application() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun imageLoaderBuilder(): ImageLoader.Builder {
|
fun imageLoaderBuilder(): ImageLoader.Builder {
|
||||||
return ImageLoader.Builder(applicationContext).diskCache { imageCache }
|
return ImageLoader.Builder(applicationContext).diskCache { coilCache }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun encryptedStorage(npub: String? = null): EncryptedSharedPreferences {
|
fun encryptedStorage(npub: String? = null): EncryptedSharedPreferences {
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ import androidx.core.view.ViewCompat
|
|||||||
import coil.annotation.ExperimentalCoilApi
|
import coil.annotation.ExperimentalCoilApi
|
||||||
import coil.compose.AsyncImage
|
import coil.compose.AsyncImage
|
||||||
import coil.compose.AsyncImagePainter
|
import coil.compose.AsyncImagePainter
|
||||||
import coil.imageLoader
|
import com.vitorpamplona.amethyst.Amethyst
|
||||||
import com.vitorpamplona.amethyst.R
|
import com.vitorpamplona.amethyst.R
|
||||||
import com.vitorpamplona.amethyst.commons.BaseMediaContent
|
import com.vitorpamplona.amethyst.commons.BaseMediaContent
|
||||||
import com.vitorpamplona.amethyst.commons.MediaLocalImage
|
import com.vitorpamplona.amethyst.commons.MediaLocalImage
|
||||||
@@ -305,7 +305,7 @@ private fun UrlImageView(
|
|||||||
BoxWithConstraints(contentAlignment = Alignment.Center) {
|
BoxWithConstraints(contentAlignment = Alignment.Center) {
|
||||||
val showImage =
|
val showImage =
|
||||||
remember {
|
remember {
|
||||||
mutableStateOf<Boolean>(
|
mutableStateOf(
|
||||||
if (alwayShowImage) true else accountViewModel.settings.showImages.value,
|
if (alwayShowImage) true else accountViewModel.settings.showImages.value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -313,19 +313,9 @@ private fun UrlImageView(
|
|||||||
val myModifier =
|
val myModifier =
|
||||||
remember {
|
remember {
|
||||||
mainImageModifier.widthIn(max = maxWidth).heightIn(max = maxHeight)
|
mainImageModifier.widthIn(max = maxWidth).heightIn(max = maxHeight)
|
||||||
/* Is this necessary? It makes images bleed into other pages
|
|
||||||
.run {
|
|
||||||
aspectRatio(content.dim)?.let { ratio ->
|
|
||||||
this.aspectRatio(ratio, false)
|
|
||||||
} ?: this
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val contentScale =
|
val contentScale = if (maxHeight.isFinite) ContentScale.Fit else ContentScale.FillWidth
|
||||||
remember {
|
|
||||||
if (maxHeight.isFinite) ContentScale.Fit else ContentScale.FillWidth
|
|
||||||
}
|
|
||||||
|
|
||||||
val verifierModifier =
|
val verifierModifier =
|
||||||
if (topPaddingForControllers.isSpecified) {
|
if (topPaddingForControllers.isSpecified) {
|
||||||
@@ -342,7 +332,9 @@ private fun UrlImageView(
|
|||||||
contentDescription = content.description,
|
contentDescription = content.description,
|
||||||
contentScale = contentScale,
|
contentScale = contentScale,
|
||||||
modifier = myModifier,
|
modifier = myModifier,
|
||||||
onState = { painterState.value = it },
|
onState = {
|
||||||
|
painterState.value = it
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +481,7 @@ private fun AddedImageFeatures(
|
|||||||
verifiedModifier: Modifier,
|
verifiedModifier: Modifier,
|
||||||
showImage: MutableState<Boolean>,
|
showImage: MutableState<Boolean>,
|
||||||
) {
|
) {
|
||||||
val ratio = remember { aspectRatio(content.dim) }
|
val ratio = remember(content.url) { aspectRatio(content.dim) }
|
||||||
|
|
||||||
if (!showImage.value) {
|
if (!showImage.value) {
|
||||||
if (content.blurhash != null && ratio != null) {
|
if (content.blurhash != null && ratio != null) {
|
||||||
@@ -509,7 +501,7 @@ private fun AddedImageFeatures(
|
|||||||
ImageUrlWithDownloadButton(content.url, showImage)
|
ImageUrlWithDownloadButton(content.url, showImage)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var verifiedHash by remember { mutableStateOf<Boolean?>(null) }
|
var verifiedHash by remember(content.url) { mutableStateOf<Boolean?>(null) }
|
||||||
|
|
||||||
when (painter.value) {
|
when (painter.value) {
|
||||||
null,
|
null,
|
||||||
@@ -537,10 +529,9 @@ private fun AddedImageFeatures(
|
|||||||
}
|
}
|
||||||
is AsyncImagePainter.State.Success -> {
|
is AsyncImagePainter.State.Success -> {
|
||||||
if (content.hash != null) {
|
if (content.hash != null) {
|
||||||
val context = LocalContext.current
|
|
||||||
LaunchedEffect(key1 = content.url) {
|
LaunchedEffect(key1 = content.url) {
|
||||||
launch(Dispatchers.IO) {
|
launch(Dispatchers.IO) {
|
||||||
val newVerifiedHash = verifyHash(content, context)
|
val newVerifiedHash = verifyHash(content)
|
||||||
if (newVerifiedHash != verifiedHash) {
|
if (newVerifiedHash != verifiedHash) {
|
||||||
verifiedHash = newVerifiedHash
|
verifiedHash = newVerifiedHash
|
||||||
}
|
}
|
||||||
@@ -1002,13 +993,10 @@ private fun RenderImageOrVideo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalCoilApi::class)
|
@OptIn(ExperimentalCoilApi::class)
|
||||||
private fun verifyHash(
|
private suspend fun verifyHash(content: MediaUrlContent): Boolean? {
|
||||||
content: MediaUrlContent,
|
|
||||||
context: Context,
|
|
||||||
): Boolean? {
|
|
||||||
if (content.hash == null) return null
|
if (content.hash == null) return null
|
||||||
|
|
||||||
context.imageLoader.diskCache?.get(content.url)?.use { snapshot ->
|
Amethyst.instance.coilCache.openSnapshot(content.url)?.use { snapshot ->
|
||||||
val hash = CryptoUtils.sha256(snapshot.data.toFile().readBytes()).toHexKey()
|
val hash = CryptoUtils.sha256(snapshot.data.toFile().readBytes()).toHexKey()
|
||||||
|
|
||||||
Log.d("Image Hash Verification", "$hash == ${content.hash}")
|
Log.d("Image Hash Verification", "$hash == ${content.hash}")
|
||||||
|
|||||||
Reference in New Issue
Block a user