Skip to content

Commit

Permalink
perf: 优化图片显示
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 31, 2023
1 parent 6039f8c commit bb8fbac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
11 changes: 7 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/GroupItemPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import li.songe.gkd.data.RawSubscription
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.imageLoader
import li.songe.gkd.util.subsIdToRawFlow


@RootNavGraph
@Destination
@Destination(style = ProfileTransitions::class)
@Composable
fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) {
val context = LocalContext.current
val navController = LocalNavController.current
val subsIdToRaw by subsIdToRawFlow.collectAsState()
val rawSubs = subsIdToRaw[subsInt]
Expand Down Expand Up @@ -72,8 +74,9 @@ fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) {
is RawSubscription.RawAppGroup -> {
Text(
text = ((rawSubs?.name
?: subsInt.toString()) + (appInfoCache[appId]?.name ?: rawApp?.name
?: appId) + "/" + (group.name))
?: subsInt.toString()) + "/" + (appInfoCache[appId]?.name
?: rawApp?.name
?: appId) + "/" + (group.name))
)
}

Expand Down Expand Up @@ -103,7 +106,7 @@ fun GroupItemPage(subsInt: Long, groupKey: Int, appId: String? = null) {
val url = allExampleUrls.getOrNull(p)
if (url != null) {
SubcomposeAsyncImage(
model = ImageRequest.Builder(LocalContext.current).data(url)
model = ImageRequest.Builder(context).data(url)
.crossfade(DefaultDurationMillis).build(),
contentDescription = null,
modifier = Modifier.fillMaxWidth(),
Expand Down
28 changes: 19 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/util/Singleton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import li.songe.gkd.app
import okhttp3.OkHttpClient
import java.time.Duration


val kv by lazy { MMKV.mmkvWithID("kv")!! }
Expand Down Expand Up @@ -48,14 +50,22 @@ val client by lazy {
}

val imageLoader by lazy {
ImageLoader.Builder(app).components {
if (Build.VERSION.SDK_INT >= 28) {
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
}.diskCache {
DiskCache.Builder().directory(imageCacheDir).build()
}.build()
ImageLoader.Builder(app)
.okHttpClient(
OkHttpClient.Builder()
.connectTimeout(Duration.ofSeconds(30))
.readTimeout(Duration.ofSeconds(30))
.writeTimeout(Duration.ofSeconds(30))
.build()
)
.components {
if (Build.VERSION.SDK_INT >= 28) {
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
}.diskCache {
DiskCache.Builder().directory(imageCacheDir).build()
}.build()
}

0 comments on commit bb8fbac

Please sign in to comment.