Skip to content

Instantly share code, notes, and snippets.

@5AbhishekSaxena
Created April 19, 2022 18:47
Show Gist options
  • Save 5AbhishekSaxena/33df7096adc59ca624f30ecee9d634e0 to your computer and use it in GitHub Desktop.
Save 5AbhishekSaxena/33df7096adc59ca624f30ecee9d634e0 to your computer and use it in GitHub Desktop.
sealed class UiImage<out T>(val resource: T) {
data class LocalImage(private val imageRes: Int) : UiImage<Int>(resource = imageRes)
data class RemoteImage(private val imageUrl: String) : UiImage<String>(resource = imageUrl)
}
@5AbhishekSaxena
Copy link
Author

Implementation example

data class Person(
    var profileImage: UiImage<*>
)

fun sample() {
    val person =
        Person(
            profileImage = UiImage.RemoteImage(
                imageUrl = "https://google.ccom/images/sample_image.png"
            )
        )

    person.profileImage = UiImage.LocalImage(R.drawable.placeholder)

}

// How to use it with Glide
fun ImageView.loadImage(
    image: UiImage<*>
) {
    Glide.with(this.context)
        .load(image.resource)
        .into(this)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment