add blurhash to discover

This commit is contained in:
Matthieu 2021-02-09 23:28:08 +01:00
parent 6f9bbf5100
commit 70f0e59d66
4 changed files with 24 additions and 9 deletions

View File

@ -152,7 +152,15 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
if(status?.media_attachments?.size == 1) {
request.placeholder(
BlurHashDecoder.blurHashBitmap(binding.root.context.resources, status?.media_attachments?.get(0))
status?.media_attachments?.get(0).let {
it?.blurhash?.let { hash ->
BlurHashDecoder.blurHashBitmap(binding.root.resources,
hash,
it.meta?.original?.width,
it.meta?.original?.height
)
}
}
).load(status?.getPostUrl()).into(binding.postPicture)
val imgDescription = status?.media_attachments?.get(0)?.description.orEmpty().ifEmpty { binding.root.context.getString(
R.string.no_description) }
@ -702,8 +710,13 @@ class AlbumViewPagerAdapter(private val media_attachments: List<Attachment>) :
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Glide.with(holder.binding.root)
.asDrawable().fitCenter().placeholder(
BlurHashDecoder.blurHashBitmap(
holder.binding.root.context.resources, media_attachments[position])
media_attachments[position].blurhash?.let {
BlurHashDecoder.blurHashBitmap(
holder.binding.root.resources,
it,
media_attachments[position].meta?.original?.width,
media_attachments[position].meta?.original?.height)
}
)
.load(media_attachments[position].url).into(holder.image)

View File

@ -135,7 +135,7 @@ class SearchDiscoverFragment : BaseFragment() {
} else {
holder.albumIcon.visibility = View.GONE
}
ImageConverter.setSquareImageFromURL(holder.postView, post.media_attachments?.firstOrNull()?.preview_url, holder.postPreview)
ImageConverter.setSquareImageFromURL(holder.postView, post.media_attachments?.firstOrNull()?.preview_url, holder.postPreview, post.media_attachments?.firstOrNull()?.blurhash)
holder.postPreview.setOnClickListener {
val intent = Intent(holder.postView.context, PostActivity::class.java)
intent.putExtra(Status.POST_TAG, post)

View File

@ -19,12 +19,12 @@ import kotlin.math.withSign
object BlurHashDecoder {
fun blurHashBitmap(resources: Resources, attachment: Attachment?): BitmapDrawable {
val ratioOr0 = (attachment?.meta?.original?.width?.toFloat() ?: 1f) / (attachment?.meta?.original?.height?.toFloat() ?: 1f)
fun blurHashBitmap(resources: Resources, blurHash: String, width: Int?, height: Int?): BitmapDrawable {
val ratioOr0 = (width?.toFloat() ?: 1f) / (height?.toFloat() ?: 1f)
val ratio = if (ratioOr0 == 0f) 1f else ratioOr0
return BitmapDrawable(resources,
decode(attachment?.blurhash,
decode(blurHash,
(32f * ratio).toInt().coerceAtLeast(32),
(32f / ratio).toInt().coerceAtLeast(32))
)

View File

@ -69,8 +69,10 @@ class ImageConverter {
* @param url, the url of the image that will be loaded
* @param image, the imageView into which we will load the image
*/
fun setSquareImageFromURL(view : View, url : String?, image : ImageView) {
Glide.with(view).load(url).apply(RequestOptions().centerCrop()).into(image)
fun setSquareImageFromURL(view : View, url : String?, image : ImageView, blurhash: String? = null) {
Glide.with(view).load(url).placeholder(
blurhash?.let { BlurHashDecoder.blurHashBitmap(view.resources, it, 32,32) }
).apply(RequestOptions().centerCrop()).into(image)
}