Должен ли я использовать инъекции в классе BindAdapter? - PullRequest
0 голосов
/ 09 июля 2019

У меня есть этот метод:

/**
 * Uses the Glide library to load an image by URL into an [ImageView]
 */
@BindingAdapter("imageUrl")
fun bindImage(imgView: ImageView, imgUrl: String?) {
    imgUrl?.let {
        val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
        Glide.with(imgView.context)
            .load(imgUri)
            .apply(
                RequestOptions()
                    .placeholder(R.drawable.loading_animation)
                    .error(R.drawable.ic_broken_image)
            )
            .into(imgView)
    }
}

внутри файла BindAdapter.

Должен ли я использовать кинжал для введения кинжала в этот метод?

Если это так, как мне это сделать, потому что BindAdapter - это файл, а не класс?

1 Ответ

1 голос
/ 09 июля 2019
 You can try this way
  @Singleton
  @Provides
  static RequestOptions provideRequestOptions(){
    return RequestOptions
            .placeholderOf(R.drawable.loading_animation)
            .error(R.drawable.ic_broken_image);
  }

  @Singleton
  @Provides
  static RequestManager provideGlideInstance(Application application, 
  RequestOptions requestOptions){
    return Glide.with(application)
            .setDefaultRequestOptions(requestOptions);
  }


  In DataBinding Class
   @Inject
   RequestManager requestManager;

   @BindingAdapter("imageUrl")
  fun bindImage(imgView: ImageView, imgUrl: String?) {
  imgUrl?.let {
    val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
    requestManager
        .load(imgUri)
        .into(imgView)
   }
}
...