Skip to content
Permalink
Browse files

Finally fix images not always loading

  • Loading branch information...
chrisbanes committed Jul 31, 2019
1 parent 2d48caf commit 6ee2425e9c491ed8214eb344b88449ead22033c3
@@ -17,10 +17,68 @@
package app.tivi.extensions

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.view.doOnLayout
import app.tivi.common.ui.R
import app.tivi.data.entities.ImageType
import app.tivi.data.entities.TmdbImageEntity
import app.tivi.tmdb.TmdbImageUrlProvider
import app.tivi.ui.glide.GlideApp
import app.tivi.ui.glide.GlideRequest
import app.tivi.ui.glide.GlideRequests
import java.util.Objects

fun GlideRequests.optSaturateOnLoad(saturateOnLoad: Boolean): GlideRequest<Drawable> = when {
saturateOnLoad -> saturateOnLoad()
else -> asDrawable()
}

fun ImageView.loadTmdbImage(
image: TmdbImageEntity,
urlProvider: TmdbImageUrlProvider,
saturateOnLoad: Boolean = true
) {
fun invokeGlide() {
GlideApp.with(this)
.optSaturateOnLoad(saturateOnLoad)
.load(image.toUrl(urlProvider, width))
.thumbnail(GlideApp.with(this)
.optSaturateOnLoad(saturateOnLoad)
.load(image.toUrl(urlProvider, 0)))
.into(this)
}

val requestKey = Objects.hash(image, urlProvider, saturateOnLoad)

if (getTag(R.id.image_key) == requestKey) {
// Image is already in progress
return
}

GlideApp.with(this).clear(this)
setTag(R.id.image_key, requestKey)

if (isLaidOut && isAttachedToWindow) {
invokeGlide()
} else {
doOnAttach {
if (getTag(R.id.image_key) == requestKey) {
doOnLayout {
if (getTag(R.id.image_key) == requestKey) {
invokeGlide()
}
}
}
}
}
}

fun ImageView.clearRequest() {
GlideApp.with(this).clear(this)
setTag(R.id.image_key, null)
}

fun TmdbImageEntity.toUrl(urlProvider: TmdbImageUrlProvider, width: Int) = when (type) {
ImageType.BACKDROP -> urlProvider.getBackdropUrl(path, width)
ImageType.POSTER -> urlProvider.getPosterUrl(path, width)
}
@@ -17,15 +17,13 @@
package app.tivi.ui.glide

import android.widget.ImageView
import androidx.core.view.doOnLayout
import androidx.databinding.BindingAdapter
import app.tivi.common.ui.R
import app.tivi.data.entities.ImageType
import app.tivi.data.entities.ShowTmdbImage
import app.tivi.data.entities.TmdbImageEntity
import app.tivi.extensions.optSaturateOnLoad
import app.tivi.extensions.clearRequest
import app.tivi.extensions.loadTmdbImage
import app.tivi.tmdb.TmdbImageUrlProvider
import java.util.Objects

@BindingAdapter(
"tmdbBackdropPath",
@@ -40,10 +38,9 @@ fun loadBackdrop(
saturateOnLoad: Boolean?
) {
val image = if (path != null) ShowTmdbImage(path = path, type = ImageType.BACKDROP, showId = 0) else null
loadImage(view, null, urlProvider, saturateOnLoad, image, urlProvider, saturateOnLoad)
loadImage(view, image, urlProvider, saturateOnLoad)
}

@Suppress("UNUSED_PARAMETER")
@BindingAdapter(
"image",
"tmdbImageUrlProvider",
@@ -52,47 +49,14 @@ fun loadBackdrop(
)
fun loadImage(
view: ImageView,
previousImage: TmdbImageEntity?,
previousUrlProvider: TmdbImageUrlProvider?,
previousSaturateOnLoad: Boolean?,
image: TmdbImageEntity?,
urlProvider: TmdbImageUrlProvider?,
saturateOnLoad: Boolean?
) {
val requestKey = Objects.hash(image, urlProvider)
view.setTag(R.id.image_key, requestKey)

if (urlProvider != null && image != null) {
if (previousUrlProvider == urlProvider && previousImage == image) {
return
}

view.setImageDrawable(null)

view.doOnLayout {
if (it.getTag(R.id.image_key) != requestKey) {
// The request key is different, exit now since there's we've probably be rebound to a different
// item
return@doOnLayout
}

fun toUrl(image: TmdbImageEntity, width: Int) = when (image.type) {
ImageType.BACKDROP -> urlProvider.getBackdropUrl(image.path, width)
ImageType.POSTER -> urlProvider.getPosterUrl(image.path, width)
}

GlideApp.with(view).clear(view)

GlideApp.with(it)
.optSaturateOnLoad(saturateOnLoad == null || saturateOnLoad)
.load(toUrl(image, view.width))
.thumbnail(GlideApp.with(view)
.optSaturateOnLoad(saturateOnLoad == null || saturateOnLoad)
.load(toUrl(image, 0)))
.into(view)
}
view.loadTmdbImage(image, urlProvider, saturateOnLoad == null || saturateOnLoad)
} else {
GlideApp.with(view).clear(view)
view.clearRequest()
view.setImageDrawable(null)
}
}
@@ -47,13 +47,17 @@ fun View.doOnApplyWindowInsets(f: (View, WindowInsetsCompat, ViewPaddingState) -
* If not it sets an [View.OnAttachStateChangeListener] and waits to be attached before calling
* [View.requestApplyInsets].
*/
fun View.requestApplyInsetsWhenAttached() {
fun View.requestApplyInsetsWhenAttached() = doOnAttach {
it.requestApplyInsets()
}

fun View.doOnAttach(f: (View) -> Unit) {
if (isAttachedToWindow) {
requestApplyInsets()
f(this)
} else {
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) {
v.requestApplyInsets()
f(v)
}

override fun onViewDetachedFromWindow(v: View) = Unit

0 comments on commit 6ee2425

Please sign in to comment.
You can’t perform that action at this time.