Last active
February 8, 2021 09:55
-
-
Save KryptKode/951ca52559f0a36b4728dc96aaeef940 to your computer and use it in GitHub Desktop.
ImageLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GlideModule | |
class GlideAppModule : AppGlideModule() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ImageLoader { | |
fun load( | |
imageSource: String, | |
target: ImageView, | |
@DrawableRes errorResId: Int = R.drawable.ic_photo | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dagger module to bind the ImageLoader to it's implementation | |
*/ | |
@Module | |
interface ImageLoaderModule { | |
@Binds | |
@ActivityScoped | |
fun provideImageLoader(dispatchers: ImageLoaderImpl): ImageLoader | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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