Glide: GlideExtension in kotlin use of object or companion object ?

Created on 14 Jul 2017  路  9Comments  路  Source: bumptech/glide

use kotlin can't implement GlideExtension

documentation

Most helpful comment

user JvmStatic

sample

@GlideExtension
object MyAppExtension {

  private val MINI_THUMB_SIZE = 100

  @GlideOption
  @JvmStatic
  fun miniThumb(options: RequestOptions) {
    options
        .fitCenter()
        .override(MINI_THUMB_SIZE)
  }
}

All 9 comments

Please provide a bit more info, neither of us are active Kotlin devs. Your code, the error message, versiobs., Grade setup.

From the documentation, glide extensions are annotated classes with annotated static methods. Since kotlin doesn't really have static methods, the generated api may not work.

I haven't tried the extension annotations, but in the case of kotlin, you can already make extension functions directly yourself.

Consider trying something along the lines of

fun RequestOptions.newFun() {
    //action here
}

Another example of an extension function is

fun <T> RequestBuilder<T>.withRoundIcon() = apply(RequestOptions().transform(CircleCrop()))

@AllanWang Thank you ,This is a good way to achieve

That does exist but it needs to be wrapped in an object so I'm not sure how glide would deal with it. But again since kotlin supports extensions already you my as well just use that as opposed to generating the extension code

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

Inspired by @AllanWang . I wrote a sample to present how to use Kotlin Extension to achieve Glide v4 Extension.

fun <T> GlideRequest<T>.removePlaceholders(): GlideRequest<T> {
    val options = RequestOptions()
            .placeholder(android.R.color.transparent)
            .fallback(android.R.color.transparent)
            .error(android.R.color.transparent)
    return apply(options)
}

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

user JvmStatic

sample

@GlideExtension
object MyAppExtension {

  private val MINI_THUMB_SIZE = 100

  @GlideOption
  @JvmStatic
  fun miniThumb(options: RequestOptions) {
    options
        .fitCenter()
        .override(MINI_THUMB_SIZE)
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

billy2271 picture billy2271  路  3Comments

StefMa picture StefMa  路  3Comments

mttmllns picture mttmllns  路  3Comments

sant527 picture sant527  路  3Comments

MrFuFuFu picture MrFuFuFu  路  3Comments