mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-22 13:09:46 +01:00
Add some sugar for fetching conversation image
This commit is contained in:
parent
380667040a
commit
1f6d3a1a63
@ -22,11 +22,11 @@ package com.nextcloud.talk.adapters.items
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.LayerDrawable
|
||||
import android.text.TextUtils
|
||||
import android.text.format.DateUtils
|
||||
import android.view.View
|
||||
import coil.api.load
|
||||
import coil.request.CachePolicy
|
||||
import coil.transform.CircleCropTransformation
|
||||
import com.nextcloud.talk.R
|
||||
import com.nextcloud.talk.application.NextcloudTalkApplication
|
||||
@ -35,6 +35,7 @@ import com.nextcloud.talk.models.json.conversations.Conversation
|
||||
import com.nextcloud.talk.models.json.conversations.Conversation.ConversationType.ONE_TO_ONE_CONVERSATION
|
||||
import com.nextcloud.talk.newarch.local.models.UserNgEntity
|
||||
import com.nextcloud.talk.newarch.local.models.getCredentials
|
||||
import com.nextcloud.talk.newarch.utils.Images
|
||||
import com.nextcloud.talk.utils.ApiUtils
|
||||
import eu.davidea.flexibleadapter.FlexibleAdapter
|
||||
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
|
||||
@ -199,70 +200,20 @@ class ConversationItem(
|
||||
|
||||
holder.itemView.dialogAvatar.visibility = View.VISIBLE
|
||||
|
||||
var shouldLoadAvatar = true
|
||||
val objectType: String? = model.objectType
|
||||
if (!TextUtils.isEmpty(objectType)) {
|
||||
when (objectType) {
|
||||
"share:password" -> {
|
||||
shouldLoadAvatar = false
|
||||
holder.itemView.dialogAvatar.load(R.drawable.ic_file_password_request) {
|
||||
transformations(CircleCropTransformation())
|
||||
}
|
||||
}
|
||||
"file" -> {
|
||||
shouldLoadAvatar = false
|
||||
holder.itemView.dialogAvatar.load(R.drawable.ic_file_icon) {
|
||||
transformations(CircleCropTransformation())
|
||||
}
|
||||
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Conversation.ConversationType.SYSTEM_CONVERSATION == model.type) {
|
||||
val layers = arrayOfNulls<Drawable>(2)
|
||||
layers[0] = context.getDrawable(R.drawable.ic_launcher_background)
|
||||
layers[1] = context.getDrawable(R.drawable.ic_launcher_foreground)
|
||||
val layerDrawable = LayerDrawable(layers)
|
||||
|
||||
holder.itemView.dialogAvatar.load(layerDrawable) {
|
||||
val conversationDrawable: Drawable? = Images().getImageForConversation(context, model)
|
||||
if (conversationDrawable != null) {
|
||||
holder.itemView.dialogAvatar.setImageDrawable(conversationDrawable)
|
||||
} else {
|
||||
holder.itemView.dialogAvatar.load(
|
||||
ApiUtils.getUrlForAvatarWithName(
|
||||
user.baseUrl,
|
||||
model.name, R.dimen.avatar_size
|
||||
)
|
||||
) {
|
||||
addHeader("Authorization", user.getCredentials())
|
||||
transformations(CircleCropTransformation())
|
||||
}
|
||||
|
||||
shouldLoadAvatar = false
|
||||
}
|
||||
|
||||
if (shouldLoadAvatar) {
|
||||
when (model.type) {
|
||||
ONE_TO_ONE_CONVERSATION -> if (!TextUtils.isEmpty(
|
||||
model.name
|
||||
)
|
||||
) {
|
||||
holder.itemView.dialogAvatar.load(
|
||||
ApiUtils.getUrlForAvatarWithName(
|
||||
user.baseUrl,
|
||||
model.name, R.dimen.avatar_size
|
||||
)
|
||||
) {
|
||||
addHeader("Authorization", user.getCredentials())
|
||||
transformations(CircleCropTransformation())
|
||||
}
|
||||
|
||||
} else {
|
||||
holder.itemView.dialogAvatar.visibility = View.GONE
|
||||
}
|
||||
Conversation.ConversationType.GROUP_CONVERSATION ->
|
||||
holder.itemView.dialogAvatar.load(R.drawable.ic_people_group_white_24px) {
|
||||
transformations(CircleCropTransformation())
|
||||
}
|
||||
Conversation.ConversationType.PUBLIC_CONVERSATION ->
|
||||
holder.itemView.dialogAvatar.load(R.drawable.ic_link_white_24px) {
|
||||
transformations(CircleCropTransformation())
|
||||
}
|
||||
else -> holder.itemView.dialogAvatar.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,13 +21,18 @@
|
||||
package com.nextcloud.talk.newarch.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.LayerDrawable
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import coil.ImageLoader
|
||||
import coil.request.LoadRequest
|
||||
import coil.target.Target
|
||||
import coil.transform.Transformation
|
||||
import com.nextcloud.talk.R
|
||||
import com.nextcloud.talk.models.json.conversations.Conversation
|
||||
import com.nextcloud.talk.newarch.local.models.UserNgEntity
|
||||
import com.nextcloud.talk.newarch.local.models.getCredentials
|
||||
import com.nextcloud.talk.utils.DisplayUtils
|
||||
|
||||
class Images {
|
||||
fun getRequestForUrl(
|
||||
@ -58,4 +63,42 @@ class Images {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// returns null if it's one-to-one that you need to fetch yourself
|
||||
fun getImageForConversation(context: Context, conversation: Conversation): Drawable? {
|
||||
conversation.objectType?.let { objectType ->
|
||||
when (objectType) {
|
||||
"share:password" -> {
|
||||
return DisplayUtils.getRoundedDrawable(context.getDrawable(R.drawable.ic_file_password_request))
|
||||
}
|
||||
"file" -> {
|
||||
return DisplayUtils.getRoundedDrawable(context.getDrawable(R.drawable.ic_file_icon))
|
||||
}
|
||||
else -> {
|
||||
} // do nothing
|
||||
}
|
||||
}
|
||||
|
||||
when (conversation.type) {
|
||||
Conversation.ConversationType.ONE_TO_ONE_CONVERSATION -> {
|
||||
return null
|
||||
}
|
||||
Conversation.ConversationType.GROUP_CONVERSATION -> {
|
||||
return DisplayUtils.getRoundedDrawable(context.getDrawable(R.drawable.ic_people_group_white_24px))
|
||||
}
|
||||
Conversation.ConversationType.PUBLIC_CONVERSATION -> {
|
||||
return DisplayUtils.getRoundedDrawable(context.getDrawable(R.drawable.ic_link_white_24px))
|
||||
}
|
||||
//Conversation.ConversationType.SYSTEM_CONVERSATION
|
||||
else -> {
|
||||
// we handle else as SYSTEM_CONVERSATION
|
||||
val layers = arrayOfNulls<Drawable>(2)
|
||||
layers[0] = context.getDrawable(R.drawable.ic_launcher_background)
|
||||
layers[1] = context.getDrawable(R.drawable.ic_launcher_foreground)
|
||||
return DisplayUtils.getRoundedDrawable(LayerDrawable(layers))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -101,21 +101,22 @@ object DisplayUtils {
|
||||
}
|
||||
|
||||
private fun roundImage(pool: BitmapPool, input: Bitmap): Bitmap {
|
||||
val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
|
||||
val bitmapPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG).apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) }
|
||||
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
|
||||
|
||||
val minSize = min(input.width, input.height)
|
||||
val radius = minSize / 2f
|
||||
val output = pool.get(minSize, minSize, input.config)
|
||||
output.applyCanvas {
|
||||
drawCircle(radius, radius, radius, circlePaint)
|
||||
drawBitmap(input, 0f, 0f, bitmapPaint)
|
||||
drawCircle(radius, radius, radius, paint)
|
||||
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
|
||||
drawBitmap(input, radius - input.width / 2, radius - input.height / 2, paint)
|
||||
}
|
||||
pool.put(input)
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
|
||||
private fun getBitmap(drawable: Drawable): Bitmap {
|
||||
val bitmap = Bitmap.createBitmap(
|
||||
drawable.intrinsicWidth,
|
||||
|
Loading…
Reference in New Issue
Block a user