feat: switch to glide from coil

caching happens quite a bit more aggressively with glide and scrolling through a channel is a breeze now
This commit is contained in:
Infi 2023-01-07 01:42:09 +01:00
parent 1ef7d6610b
commit efdc035406
5 changed files with 21 additions and 46 deletions

View File

@ -92,11 +92,9 @@ dependencies {
implementation "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01" implementation "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01"
kapt "com.google.dagger:hilt-compiler:$hilt_version" kapt "com.google.dagger:hilt-compiler:$hilt_version"
// Coil - Image Loading // Glide - Image Loading
implementation "io.coil-kt:coil:$coil_version" implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "io.coil-kt:coil-compose:$coil_version" implementation "com.github.bumptech.glide:compose:1.0.0-alpha.1"
implementation "io.coil-kt:coil-svg:$coil_version"
implementation "io.coil-kt:coil-gif:$coil_version"
// AboutLibraries - automated OSS library attribution // AboutLibraries - automated OSS library attribution
implementation "com.mikepenz:aboutlibraries-compose:$aboutlibraries_version" implementation "com.mikepenz:aboutlibraries-compose:$aboutlibraries_version"

View File

@ -60,7 +60,6 @@ fun Message(
modifier = Modifier modifier = Modifier
.size(50.dp) .size(50.dp)
.clip(CircleShape), .clip(CircleShape),
crossfade = false,
description = "Avatar for ${author.username}" description = "Avatar for ${author.username}"
) )
} else { } else {
@ -69,7 +68,6 @@ fun Message(
modifier = Modifier modifier = Modifier
.size(50.dp) .size(50.dp)
.clip(CircleShape), .clip(CircleShape),
crossfade = false,
description = "Avatar for ${author.username}" description = "Avatar for ${author.username}"
) )
} }
@ -114,7 +112,6 @@ fun Message(
width = attachment.metadata.width?.toInt() ?: 0, width = attachment.metadata.width?.toInt() ?: 0,
height = attachment.metadata.height?.toInt() ?: 0, height = attachment.metadata.height?.toInt() ?: 0,
contentScale = ContentScale.Fit, contentScale = ContentScale.Fit,
crossfade = true,
description = "Attached image ${attachment.filename}" description = "Attached image ${attachment.filename}"
) )
} else { } else {

View File

@ -1,19 +1,19 @@
package chat.revolt.components.generic package chat.revolt.components.generic
import android.os.Build import android.util.DisplayMetrics
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import chat.revolt.BuildConfig import chat.revolt.BuildConfig
import coil.ImageLoader import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import coil.compose.AsyncImage import com.bumptech.glide.integration.compose.GlideImage
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.memory.MemoryCache
import coil.request.ImageRequest
@OptIn(ExperimentalGlideComposeApi::class)
@Composable @Composable
fun RemoteImage( fun RemoteImage(
url: String, url: String,
@ -22,42 +22,21 @@ fun RemoteImage(
contentScale: ContentScale = ContentScale.Crop, contentScale: ContentScale = ContentScale.Crop,
width: Int = 0, width: Int = 0,
height: Int = 0, height: Int = 0,
crossfade: Boolean = true,
) { ) {
val context = LocalContext.current val context = LocalContext.current
fun imageRequest() = run { fun pxAsDp(px: Int): Dp {
val builder = ImageRequest.Builder(context) return (px / (context.resources
.crossfade(crossfade) .displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).dp
.data(url)
if (width != 0 && height != 0) {
builder.size(width, height)
}
builder.build()
} }
AsyncImage( GlideImage(
model = imageRequest(), model = url,
imageLoader = ImageLoader.Builder(context)
.components {
if (Build.VERSION.SDK_INT >= 28) {
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
add(SvgDecoder.Factory())
}
.memoryCache {
MemoryCache.Builder(context)
.maxSizePercent(.25)
.build()
}
.build(),
contentDescription = description, contentDescription = description,
contentScale = contentScale, contentScale = contentScale,
modifier = modifier, modifier = modifier
.width(pxAsDp(width))
.height(pxAsDp(height)),
) )
} }

View File

@ -212,8 +212,8 @@ class ChannelScreenViewModel : ViewModel() {
} }
fun sendPendingMessage() { fun sendPendingMessage() {
setSendingMessage(true) setSendingMessage(true)
viewModelScope.launch { viewModelScope.launch {
val attachmentIds = arrayListOf<String>() val attachmentIds = arrayListOf<String>()

View File

@ -7,6 +7,7 @@ buildscript {
nav_version = '2.5.3' nav_version = '2.5.3'
hilt_version = '2.44' hilt_version = '2.44'
coil_version = '2.2.2' coil_version = '2.2.2'
glide_version = '4.14.2'
ktor_version = '2.1.3' ktor_version = '2.1.3'
aboutlibraries_version = '10.5.2' aboutlibraries_version = '10.5.2'
} }