add fallback image for mimetype if drawee request fails

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-04-29 14:24:14 +02:00
parent a64e22c820
commit 734f33caba
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
4 changed files with 45 additions and 12 deletions

View file

@ -1,18 +1,23 @@
package com.nextcloud.talk.adapters
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.drawee.controller.ControllerListener
import com.facebook.drawee.interfaces.DraweeController
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.common.RotationOptions
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.nextcloud.talk.databinding.SharedItemGridBinding
import com.nextcloud.talk.repositories.SharedItem
import com.nextcloud.talk.utils.DrawableUtils
import com.nextcloud.talk.utils.DrawableUtils.getDrawableResourceIdForMimeType
import com.nextcloud.talk.utils.FileViewerUtils
@ -44,16 +49,22 @@ class SharedItemsGridAdapter : RecyclerView.Adapter<SharedItemsGridAdapter.ViewH
.setHeaders(authHeader)
.build()
val listener: ControllerListener<ImageInfo?> = object : BaseControllerListener<ImageInfo?>() {
override fun onFailure(id: String, e: Throwable) {
Log.w(TAG, "Failed to load image. A static mimetype image will be used", e)
setStaticMimetypeImage(currentItem, holder)
}
}
val draweeController: DraweeController = Fresco.newDraweeControllerBuilder()
.setOldController(holder.binding.image.controller)
.setAutoPlayAnimations(true)
.setImageRequest(imageRequest)
.setControllerListener(listener)
.build()
holder.binding.image.controller = draweeController
} else {
val drawableResourceId = getDrawableResourceIdForMimeType(currentItem.mimeType)
val drawable = ContextCompat.getDrawable(holder.binding.image.context, drawableResourceId)
holder.binding.image.hierarchy.setPlaceholderImage(drawable)
setStaticMimetypeImage(currentItem, holder)
}
holder.binding.image.setOnClickListener {
val fileViewerUtils = FileViewerUtils(it.context, currentItem.userEntity)
@ -72,6 +83,15 @@ class SharedItemsGridAdapter : RecyclerView.Adapter<SharedItemsGridAdapter.ViewH
}
}
private fun setStaticMimetypeImage(
currentItem: SharedItem,
holder: SharedItemsGridAdapter.ViewHolder
) {
val drawableResourceId = DrawableUtils.getDrawableResourceIdForMimeType(currentItem.mimeType)
val drawable = ContextCompat.getDrawable(holder.binding.image.context, drawableResourceId)
holder.binding.image.hierarchy.setPlaceholderImage(drawable)
}
override fun getItemCount(): Int {
return items.size
}

View file

@ -2,14 +2,18 @@ package com.nextcloud.talk.adapters
import android.net.Uri
import android.text.format.Formatter
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.drawee.controller.ControllerListener
import com.facebook.drawee.interfaces.DraweeController
import com.facebook.imagepipeline.common.RotationOptions
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.nextcloud.talk.databinding.SharedItemListBinding
import com.nextcloud.talk.repositories.SharedItem
@ -55,16 +59,22 @@ class SharedItemsListAdapter : RecyclerView.Adapter<SharedItemsListAdapter.ViewH
.setHeaders(authHeader)
.build()
val listener: ControllerListener<ImageInfo?> = object : BaseControllerListener<ImageInfo?>() {
override fun onFailure(id: String, e: Throwable) {
Log.w(TAG, "Failed to load image. A static mimetype image will be used", e)
setStaticMimetypeImage(currentItem, holder)
}
}
val draweeController: DraweeController = Fresco.newDraweeControllerBuilder()
.setOldController(holder.binding.fileImage.controller)
.setAutoPlayAnimations(true)
.setImageRequest(imageRequest)
.setControllerListener(listener)
.build()
holder.binding.fileImage.controller = draweeController
} else {
val drawableResourceId = DrawableUtils.getDrawableResourceIdForMimeType(currentItem.mimeType)
val drawable = ContextCompat.getDrawable(holder.binding.fileImage.context, drawableResourceId)
holder.binding.fileImage.hierarchy.setPlaceholderImage(drawable)
setStaticMimetypeImage(currentItem, holder)
}
holder.binding.fileItem.setOnClickListener {
val fileViewerUtils = FileViewerUtils(it.context, currentItem.userEntity)
@ -79,6 +89,15 @@ class SharedItemsListAdapter : RecyclerView.Adapter<SharedItemsListAdapter.ViewH
}
}
private fun setStaticMimetypeImage(
currentItem: SharedItem,
holder: ViewHolder
) {
val drawableResourceId = DrawableUtils.getDrawableResourceIdForMimeType(currentItem.mimeType)
val drawable = ContextCompat.getDrawable(holder.binding.fileImage.context, drawableResourceId)
holder.binding.fileImage.hierarchy.setPlaceholderImage(drawable)
}
override fun getItemCount(): Int {
return items.size
}

View file

@ -45,11 +45,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:src="@drawable/ic_mimetype_file"
app:placeholderImageScaleType="fitCenter"
fresco:actualImageScaleType="centerCrop"
fresco:failureImage="@drawable/ic_mimetype_file"
fresco:placeholderImage="@drawable/ic_mimetype_file"
fresco:roundedCornerRadius="4dp" />
<ProgressBar

View file

@ -45,12 +45,9 @@
android:layout_width="@dimen/mediatab_file_icon_size"
android:layout_height="@dimen/mediatab_file_icon_size"
android:padding="4dp"
android:src="@drawable/ic_mimetype_file"
app:layout_constraintTop_toTopOf="parent"
app:placeholderImageScaleType="fitCenter"
fresco:actualImageScaleType="centerCrop"
fresco:failureImage="@drawable/ic_mimetype_file"
fresco:placeholderImage="@drawable/ic_mimetype_file"
fresco:roundedCornerRadius="4dp"
tools:src="@drawable/ic_call_black_24dp"/>