Skip to content

Instantly share code, notes, and snippets.

@KryptKode
Last active February 8, 2021 09:55
Show Gist options
  • Save KryptKode/951ca52559f0a36b4728dc96aaeef940 to your computer and use it in GitHub Desktop.
Save KryptKode/951ca52559f0a36b4728dc96aaeef940 to your computer and use it in GitHub Desktop.
ImageLoader
@GlideModule
class GlideAppModule : AppGlideModule()
interface ImageLoader {
fun load(
imageSource: String,
target: ImageView,
@DrawableRes errorResId: Int = R.drawable.ic_photo
)
}
class ImageLoaderImpl @Inject constructor(
@ActivityContext private val context: Context
) : ImageLoader {
override fun load(
imageSource: String,
target: ImageView,
errorResId: Int
) {
GlideApp.with(context)
.load(imageSource)
.error(errorResId)
.into(target)
}
}
/*
Dagger module to bind the ImageLoader to it's implementation
*/
@Module
interface ImageLoaderModule {
@Binds
@ActivityScoped
fun provideImageLoader(dispatchers: ImageLoaderImpl): ImageLoader
}
class SampleActivity : AppCompatActivity() {
@Inject
lateinit var imageLoader: ImageLoader
private val binding by viewBinding(ActivitySampleBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
val adapter = SampleAdapter(imageLoader)
binding.recyclerView.adapter = adapter
}
}
class SampleAdapter (
private val imageLoader:ImageLoader
) : RecyclerView.Adapter<SampleViewHolder>() {
override fun onBindViewHolder(holder:SampleViewHolder, position:Int) {
holder.bind(getItem(position))
}
inner class SampleViewHolder (val binding:SampleItemBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(item:SampleItem){
imageloader.load("image_url", binding.image)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment