From 4a912c70faceede9d39451178eb1deff1b4cb6c6 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 22 Oct 2022 14:02:13 +0100 Subject: [PATCH] reusing image request build base instance to avoid redundant creations --- .../kotlin/app/dapk/st/messenger/MessengerActivity.kt | 9 ++++----- .../main/kotlin/app/dapk/st/messenger/MessengerScreen.kt | 4 +--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerActivity.kt b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerActivity.kt index 6849d35..045e6dc 100644 --- a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerActivity.kt +++ b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerActivity.kt @@ -5,7 +5,6 @@ import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Parcelable -import android.widget.Toast import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Surface import androidx.compose.runtime.CompositionLocalProvider @@ -16,9 +15,10 @@ import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.matrix.common.RoomId import app.dapk.st.messenger.gallery.GetImageFromGallery import app.dapk.st.navigator.MessageAttachment +import coil.request.ImageRequest import kotlinx.parcelize.Parcelize -val LocalDecyptingFetcherFactory = staticCompositionLocalOf { throw IllegalAccessError() } +val LocalImageRequestFactory = staticCompositionLocalOf { throw IllegalAccessError() } class MessengerActivity : DapkActivity() { @@ -50,7 +50,7 @@ class MessengerActivity : DapkActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val payload = readPayload() - val factory = module.decryptingFetcherFactory(RoomId(payload.roomId)) + val factory = ImageRequest.Builder(applicationContext).fetcherFactory(module.decryptingFetcherFactory(RoomId(payload.roomId))) val galleryLauncher = registerForActivityResult(GetImageFromGallery()) { it?.let { uri -> @@ -65,10 +65,9 @@ class MessengerActivity : DapkActivity() { } } - setContent { Surface(Modifier.fillMaxSize()) { - CompositionLocalProvider(LocalDecyptingFetcherFactory provides factory) { + CompositionLocalProvider(LocalImageRequestFactory provides factory) { MessengerScreen(RoomId(payload.roomId), payload.attachments, viewModel, navigator, galleryLauncher) } } diff --git a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt index a1fa188..06bb911 100644 --- a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt +++ b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt @@ -205,9 +205,7 @@ private fun RoomEvent.toModel(event: BubbleModel.Event): BubbleModel = when (thi is RoomEvent.Message -> BubbleModel.Text(this.content, event) is RoomEvent.Encrypted -> BubbleModel.Encrypted(event) is RoomEvent.Image -> { - val context = LocalContext.current - val imageRequest = ImageRequest.Builder(context) - .fetcherFactory(LocalDecyptingFetcherFactory.current) + val imageRequest = LocalImageRequestFactory.current .memoryCacheKey(this.imageMeta.url) .data(this) .build()