Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Glide] Add Gif support with GlideImage #173

Closed
jaredsburrows opened this issue Sep 18, 2022 · 6 comments
Closed

[Glide] Add Gif support with GlideImage #173

jaredsburrows opened this issue Sep 18, 2022 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@jaredsburrows
Copy link

The LocalGlideProvider only supports Drawable?:

public val LocalGlideRequestBuilder: ProvidableCompositionLocal<RequestBuilder<Drawable>?> =
  staticCompositionLocalOf { null }

By changing this to support GifDrawable?, we can support Glide's .asGif():


/**
 * Local containing the preferred [RequestOptions] for providing the same instance
 * in our composable hierarchy.
 */
public val LocalGlideGifRequestOptions: ProvidableCompositionLocal<RequestOptions?> =
  staticCompositionLocalOf { null }

/**
 * Local containing the preferred [RequestBuilder] for providing the same instance
 * in our composable hierarchy.staticCompositionLocalOf
 */
public val LocalGlideGifRequestBuilder: ProvidableCompositionLocal<RequestBuilder<GifDrawable>?> =
  staticCompositionLocalOf { null }

/**
 * Local containing the preferred [RequestManager] for providing the same instance
 * in our composable hierarchy.staticCompositionLocalOf
 */
public val LocalGlideGifRequestManager: ProvidableCompositionLocal<RequestManager?> =
  staticCompositionLocalOf { null }

/** A provider for taking the local instances related to the `GlideImage`. */
internal object LocalGlideGifProvider {

  /** Returns the current or default [RequestOptions] for the `GlideImage` parameter. */
  @Composable
  fun getGlideRequestOptions(): RequestOptions {
    return LocalGlideGifRequestOptions.current ?: RequestOptions()
  }

  /** Returns the current or default [RequestBuilder] for the `GlideImage` parameter. */
  @Composable
  fun getGlideRequestBuilder(): RequestBuilder<GifDrawable> {
    return LocalGlideGifRequestBuilder.current
           ?: getGlideRequestManager()
             .asGif()
  }

  /** Returns the current or default [RequestManager] for the `GlideImage` processor. */
  @Composable
  fun getGlideRequestManager(): RequestManager {
    // By default Glide tries to install lifecycle listeners to automatically re-trigger
    // requests when resumed. We don't want that with Compose, since we rely on composition
    // for our 'lifecycle'. We can stop Glide doing this by using the application context.
    return LocalGlideGifRequestManager.current
           ?: GlideApp.with(LocalContext.current.applicationContext)
  }
}

Can we provide a LocalGlideGifProvider, LocalGlideBitmapProvider and LocalGlideDrawableProvider providers?

@jaredsburrows jaredsburrows changed the title [Gif] Add Gif support with GlideImage [Glide] Add Gif support with GlideImage Sep 18, 2022
@skydoves skydoves added the enhancement New feature or request label Oct 3, 2022
@skydoves skydoves self-assigned this Oct 3, 2022
@skydoves
Copy link
Owner

Hey @jaredsburrows,
I'm currently working on this, and I have a question about this. I was thinking Glide automatically loads either a GifDrawable or a BitmapDrawable depending on the type of the underlying image. Do we have any problems loading gif images now? Thank you for reporting this issue!

@skydoves
Copy link
Owner

Since version 2.0.2, you can set glideRequestType parameter as the below:

GlideImage(
    imageModel = { poster.gif },
    glideRequestType = GlideRequestType.DRAWABLE,
    previewPlaceholder = R.drawable.poster
  )

GlideRequestType supports three different request types:

GlideRequestType.DRAWABLE // request image as Drawable type.
GlideRequestType.BITMAP // request image as Bitmap type.
GlideRequestType.GIF // request image as GifDrawable type.

Thanks!

@jaredsburrows
Copy link
Author

jaredsburrows commented Oct 22, 2022

@skydoves Thanks. I have tried the following but the gifs are not animating:

val glide = Glide.with(LocalView.current)
  .asGif()
  .override(135.dp.value.roundToInt())
  .signature(ObjectKey(item.tinyGifPreviewUrl))

CompositionLocalProvider(LocalGlideRequestBuilder provides glide) {
  GlideImage(
    imageModel = { item.tinyGifPreviewUrl },
    glideRequestType = GlideRequestType.GIF,
    modifier = Modifier
      .padding(1.dp)
      .size(135.dp),
    imageOptions = ImageOptions(contentScale = ContentScale.Crop),
    loading = {
      Box(modifier = Modifier.matchParentSize()) {
        CircularProgressIndicator(
          modifier = Modifier.align(Alignment.Center)
        )
      }
    },
  )
}

@skydoves
Copy link
Owner

@jaredsburrows
Thanks for the reporting with details! I'm not sure what I'm missing, but the codes below work well. Let me check out more details!

  GlideImage(
    imageModel = { "https://media.giphy.com/media/WNur0b03KntlK/giphy.gif" },
    modifier = Modifier
      .height(500.dp)
      .padding(8.dp)
      .clip(RoundedCornerShape(8.dp)),
    previewPlaceholder = R.drawable.poster
  )

@skydoves
Copy link
Owner

The example below works well for me 🤔

  val glide = Glide.with(LocalView.current)
    .asGif()
    .override(135.dp.value.roundToInt())
    .signature(ObjectKey("https://media.giphy.com/media/WNur0b03KntlK/giphy.gif"))

  CompositionLocalProvider(LocalGlideRequestBuilder provides glide) {

    GlideImage(
      imageModel = { "https://media.giphy.com/media/WNur0b03KntlK/giphy.gif" },
      glideRequestType = GlideRequestType.GIF,
      modifier = Modifier
        .padding(1.dp)
        .size(135.dp),
      imageOptions = ImageOptions(contentScale = ContentScale.Crop),
      loading = {
        Box(modifier = Modifier.matchParentSize()) {
          CircularProgressIndicator(
            modifier = Modifier.align(Alignment.Center)
          )
        }
      },
    )
  }

@jaredsburrows
Copy link
Author

@skydoves Thanks. I got it working. I appreciate the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants