mirror of
https://github.com/nextcloud/talk-android.git
synced 2024-11-27 08:55:54 +03:00
add fallback image for mimetype if drawee request fails
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
a64e22c820
commit
734f33caba
4 changed files with 45 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"/>
|
||||
|
||||
|
|
Loading…
Reference in a new issue