perf: 优化图片显示

This commit is contained in:
lisonge 2024-01-01 02:10:05 +08:00
parent 6039f8cea5
commit bb8fbacec3
2 changed files with 26 additions and 13 deletions

View File

@ -31,15 +31,17 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph import com.ramcosta.composedestinations.annotation.RootNavGraph
import li.songe.gkd.data.RawSubscription import li.songe.gkd.data.RawSubscription
import li.songe.gkd.util.LocalNavController import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.appInfoCacheFlow import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.imageLoader import li.songe.gkd.util.imageLoader
import li.songe.gkd.util.subsIdToRawFlow import li.songe.gkd.util.subsIdToRawFlow
@RootNavGraph @RootNavGraph
@Destination @Destination(style = ProfileTransitions::class)
@Composable @Composable
fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) { fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) {
val context = LocalContext.current
val navController = LocalNavController.current val navController = LocalNavController.current
val subsIdToRaw by subsIdToRawFlow.collectAsState() val subsIdToRaw by subsIdToRawFlow.collectAsState()
val rawSubs = subsIdToRaw[subsInt] val rawSubs = subsIdToRaw[subsInt]
@ -72,8 +74,9 @@ fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) {
is RawSubscription.RawAppGroup -> { is RawSubscription.RawAppGroup -> {
Text( Text(
text = ((rawSubs?.name text = ((rawSubs?.name
?: subsInt.toString()) + (appInfoCache[appId]?.name ?: rawApp?.name ?: subsInt.toString()) + "/" + (appInfoCache[appId]?.name
?: appId) + "/" + (group.name)) ?: rawApp?.name
?: appId) + "/" + (group.name))
) )
} }
@ -103,7 +106,7 @@ fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) {
val url = allExampleUrls.getOrNull(p) val url = allExampleUrls.getOrNull(p)
if (url != null) { if (url != null) {
SubcomposeAsyncImage( SubcomposeAsyncImage(
model = ImageRequest.Builder(LocalContext.current).data(url) model = ImageRequest.Builder(context).data(url)
.crossfade(DefaultDurationMillis).build(), .crossfade(DefaultDurationMillis).build(),
contentDescription = null, contentDescription = null,
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),

View File

@ -14,6 +14,8 @@ import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import li.songe.gkd.app import li.songe.gkd.app
import okhttp3.OkHttpClient
import java.time.Duration
val kv by lazy { MMKV.mmkvWithID("kv")!! } val kv by lazy { MMKV.mmkvWithID("kv")!! }
@ -48,14 +50,22 @@ val client by lazy {
} }
val imageLoader by lazy { val imageLoader by lazy {
ImageLoader.Builder(app).components { ImageLoader.Builder(app)
if (Build.VERSION.SDK_INT >= 28) { .okHttpClient(
add(ImageDecoderDecoder.Factory()) OkHttpClient.Builder()
} else { .connectTimeout(Duration.ofSeconds(30))
add(GifDecoder.Factory()) .readTimeout(Duration.ofSeconds(30))
} .writeTimeout(Duration.ofSeconds(30))
}.diskCache { .build()
DiskCache.Builder().directory(imageCacheDir).build() )
}.build() .components {
if (Build.VERSION.SDK_INT >= 28) {
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
}.diskCache {
DiskCache.Builder().directory(imageCacheDir).build()
}.build()
} }