Skip to content

Commit

Permalink
Merge pull request #21286 from wordpress-mobile/issue/21285-coil-imag…
Browse files Browse the repository at this point in the history
…e-loader

Coil image loader singleton
  • Loading branch information
nbradbury authored Oct 11, 2024
2 parents 2f5b371 + 416341c commit 03b02d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 14 additions & 1 deletion WordPress/src/main/java/org/wordpress/android/WordPress.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.wordpress.android

import android.app.Application
import android.content.Context
import coil.decode.VideoFrameDecoder
import com.android.volley.RequestQueue
import dagger.hilt.EntryPoints
import org.wordpress.android.fluxc.tools.FluxCImageLoader
Expand All @@ -11,7 +12,7 @@ import org.wordpress.android.modules.AppComponent
* An abstract class to be extended by {@link WordPressApp} for real application and WordPressTest for UI test
* application. Containing public static variables and methods to be accessed by other classes.
*/
abstract class WordPress : Application() {
abstract class WordPress : Application(), coil.ImageLoaderFactory {
abstract fun initializer(): AppInitializer

fun component(): AppComponent = EntryPoints.get(this, AppComponent::class.java)
Expand All @@ -20,6 +21,18 @@ abstract class WordPress : Application() {
initializer().wordPressComSignOut()
}

/**
* This returns a singleton Coil ImageLoader that's accessed with context.imageLoader
*/
override fun newImageLoader(): coil.ImageLoader {
return coil.ImageLoader.Builder(this)
.crossfade(true)
.components {
add(VideoFrameDecoder.Factory())
}
.build()
}

@Suppress("TooManyFunctions")
companion object {
const val SITE = "SITE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.ImageLoader
import coil.compose.AsyncImage
import coil.decode.VideoFrameDecoder
import coil.imageLoader
import coil.request.ImageRequest
import org.wordpress.android.R

Expand Down Expand Up @@ -81,18 +80,13 @@ private fun MediaUriImage(uri: Uri) {
val context = LocalContext.current
val mimeType = context.contentResolver.getType(uri)
if (mimeType?.startsWith("video/") == true) {
val imageLoader = ImageLoader.Builder(LocalContext.current)
.components {
add(VideoFrameDecoder.Factory())
}
.build()
Box {
AsyncImage(
model = ImageRequest.Builder(context)
.data(uri)
.crossfade(true)
.build(),
imageLoader = imageLoader,
imageLoader = context.imageLoader,
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier
Expand Down

0 comments on commit 03b02d1

Please sign in to comment.