From fe413d52d6f2f2dddfb4e340ed1ed7ef19a8b54f Mon Sep 17 00:00:00 2001 From: len Date: Sat, 4 Feb 2017 19:07:06 +0100 Subject: [PATCH] Show loading/error for images in catalogue grid view. Update support lib --- app/build.gradle | 2 +- .../ui/catalogue/CatalogueFragment.kt | 10 ++-- .../ui/catalogue/CatalogueGridHolder.kt | 5 +- .../kanade/tachiyomi/util/ViewExtensions.kt | 16 +++++- .../tachiyomi/widget/StateImageViewTarget.kt | 49 +++++++++++++++++++ .../drawable/ic_broken_image_grey_24dp.xml | 10 ++++ .../main/res/layout/item_catalogue_grid.xml | 8 +++ 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/StateImageViewTarget.kt create mode 100644 app/src/main/res/drawable/ic_broken_image_grey_24dp.xml diff --git a/app/build.gradle b/app/build.gradle index d5747a2ffb..00aab893e7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -101,7 +101,7 @@ dependencies { compile 'com.github.inorichi:subsampling-scale-image-view:44aa442' // Android support library - final support_library_version = '25.1.0' + final support_library_version = '25.1.1' compile "com.android.support:support-v4:$support_library_version" compile "com.android.support:appcompat-v7:$support_library_version" compile "com.android.support:cardview-v7:$support_library_version" diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt index b81836af1f..15af1ad94f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt @@ -486,14 +486,10 @@ open class CatalogueFragment : BaseRxFragment(), * @return the holder of the manga or null if it's not bound. */ private fun getHolder(manga: Manga): CatalogueHolder? { - val layoutManager = recycler.layoutManager as LinearLayoutManager - val firstVisiblePos = layoutManager.findFirstVisibleItemPosition() - val lastVisiblePos = layoutManager.findLastVisibleItemPosition() - - (firstVisiblePos..lastVisiblePos-1).forEach { i -> - val item = adapter.getItem(i) as? CatalogueItem + adapter.allBoundViewHolders.forEach { holder -> + val item = adapter.getItem(holder.adapterPosition) as? CatalogueItem if (item != null && item.manga.id!! == manga.id!!) { - return recycler.findViewHolderForLayoutPosition(i) as? CatalogueHolder + return holder as CatalogueHolder } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueGridHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueGridHolder.kt index 60cec7d01e..034f8d7f50 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueGridHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueGridHolder.kt @@ -4,7 +4,9 @@ import android.view.View import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import eu.davidea.flexibleadapter.FlexibleAdapter +import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.widget.StateImageViewTarget import kotlinx.android.synthetic.main.item_catalogue_grid.view.* /** @@ -42,8 +44,9 @@ class CatalogueGridHolder(private val view: View, private val adapter: FlexibleA .diskCacheStrategy(DiskCacheStrategy.SOURCE) .centerCrop() .skipMemoryCache(true) + .error(R.drawable.ic_broken_image_grey_24dp) .placeholder(android.R.color.transparent) - .into(view.thumbnail) + .into(StateImageViewTarget(view.thumbnail, view.progress)) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt index 150b1e4b1d..912f05e806 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt @@ -1,3 +1,5 @@ +@file:Suppress("NOTHING_TO_INLINE") + package eu.kanade.tachiyomi.util import android.graphics.Color @@ -28,4 +30,16 @@ inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Sn snack.f() snack.show() return snack -} \ No newline at end of file +} + +inline fun View.visible() { + visibility = View.VISIBLE +} + +inline fun View.invisible() { + visibility = View.INVISIBLE +} + +inline fun View.gone() { + visibility = View.GONE +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/StateImageViewTarget.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/StateImageViewTarget.kt new file mode 100644 index 0000000000..8792b88cc8 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/StateImageViewTarget.kt @@ -0,0 +1,49 @@ +package eu.kanade.tachiyomi.widget + +import android.graphics.drawable.Drawable +import android.widget.ImageView +import android.widget.ImageView.ScaleType +import android.widget.ProgressBar +import com.bumptech.glide.load.resource.drawable.GlideDrawable +import com.bumptech.glide.request.animation.GlideAnimation +import com.bumptech.glide.request.target.GlideDrawableImageViewTarget +import eu.kanade.tachiyomi.util.gone +import eu.kanade.tachiyomi.util.visible + +/** + * A glide target to display an image with an optional progress bar and a configurable scale type + * for the error drawable. + * + * @param view the view where the image will be loaded + * @param progress an optional progress bar to show when the image is loading. + * @param errorScaleType the scale type for the error drawable, [ScaleType.CENTER] by default. + */ +class StateImageViewTarget(view: ImageView, + val progress: ProgressBar? = null, + val errorScaleType: ScaleType = ScaleType.CENTER) : + GlideDrawableImageViewTarget(view) { + + private val imageScaleType = view.scaleType + + override fun onLoadStarted(placeholder: Drawable?) { + progress?.visible() + super.onLoadStarted(placeholder) + } + + override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) { + progress?.gone() + view.scaleType = errorScaleType + super.onLoadFailed(e, errorDrawable) + } + + override fun onLoadCleared(placeholder: Drawable?) { + progress?.gone() + super.onLoadCleared(placeholder) + } + + override fun onResourceReady(resource: GlideDrawable?, animation: GlideAnimation?) { + progress?.gone() + view.scaleType = imageScaleType + super.onResourceReady(resource, animation) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_broken_image_grey_24dp.xml b/app/src/main/res/drawable/ic_broken_image_grey_24dp.xml new file mode 100644 index 0000000000..35506713be --- /dev/null +++ b/app/src/main/res/drawable/ic_broken_image_grey_24dp.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/item_catalogue_grid.xml b/app/src/main/res/layout/item_catalogue_grid.xml index 6e7fae66db..ee76f80b13 100644 --- a/app/src/main/res/layout/item_catalogue_grid.xml +++ b/app/src/main/res/layout/item_catalogue_grid.xml @@ -58,6 +58,14 @@ android:shadowRadius="4" tools:text="Sample name"/> + + \ No newline at end of file