RoomViewHolder.kt 3.75 KB
Newer Older
1 2 3 4 5 6
package chat.rocket.android.chatrooms.adapter

import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.view.isGone
7
import androidx.core.view.isInvisible
8 9
import androidx.core.view.isVisible
import chat.rocket.android.R
10
import chat.rocket.android.chatrooms.adapter.model.RoomUiModel
11 12 13 14 15
import chat.rocket.common.model.RoomType
import chat.rocket.common.model.UserStatus
import kotlinx.android.synthetic.main.item_chat.view.*
import kotlinx.android.synthetic.main.unread_messages_badge.view.*

16 17
class RoomViewHolder(itemView: View, private val listener: (RoomUiModel) -> Unit) :
    ViewHolder<RoomItemHolder>(itemView) {
18
    private val resources: Resources = itemView.resources
19 20 21 22 23 24
    private val channelIcon: Drawable = resources.getDrawable(R.drawable.ic_hashtag_12dp)
    private val groupIcon: Drawable = resources.getDrawable(R.drawable.ic_lock_12_dp)
    private val onlineIcon: Drawable = resources.getDrawable(R.drawable.ic_status_online_12dp)
    private val awayIcon: Drawable = resources.getDrawable(R.drawable.ic_status_away_12dp)
    private val busyIcon: Drawable = resources.getDrawable(R.drawable.ic_status_busy_12dp)
    private val offlineIcon: Drawable = resources.getDrawable(R.drawable.ic_status_invisible_12dp)
25 26 27 28 29 30 31

    override fun bindViews(data: RoomItemHolder) {
        val room = data.data
        with(itemView) {
            image_avatar.setImageURI(room.avatar)
            text_chat_name.text = room.name

32 33 34 35 36 37
            if (room.status != null && room.type is RoomType.DirectMessage) {
                image_chat_icon.setImageDrawable(getStatusDrawable(room.status))
            } else {
                image_chat_icon.setImageDrawable(getRoomDrawable(room.type))
            }

38
            if (room.lastMessage != null) {
39
                text_last_message.text = room.lastMessage
40
                text_last_message.isVisible = true
41 42 43 44 45
            } else {
                text_last_message.isGone = true
            }

            if (room.date != null) {
46 47
                text_timestamp.text = room.date
                text_timestamp.isVisible = true
48
            } else {
49
                text_timestamp.isInvisible = true
50 51
            }

52 53 54 55
            if (room.alert) {
                if (room.unread == null) text_total_unread_messages.text = "!"
                if (room.unread != null) text_total_unread_messages.text = room.unread
                if (room.mentions) text_total_unread_messages.text = "@${room.unread}"
56
                text_chat_name.setTextAppearance(context, R.style.ChatList_ChatName_Unread_TextView)
57 58
                text_timestamp.setTextAppearance(context, R.style.ChatList_Timestamp_Unread_TextView)
                text_last_message.setTextAppearance(context, R.style.ChatList_LastMessage_Unread_TextView)
59
                text_total_unread_messages.isVisible = true
60
            } else {
61
                text_chat_name.setTextAppearance(context, R.style.ChatList_ChatName_TextView)
62 63
                text_timestamp.setTextAppearance(context, R.style.ChatList_Timestamp_TextView)
                text_last_message.setTextAppearance(context, R.style.ChatList_LastMessage_TextView)
64
                text_total_unread_messages.isInvisible = true
65 66
            }

67
            setOnClickListener { listener(room) }
68 69 70
        }
    }

71 72 73 74
    private fun getRoomDrawable(type: RoomType): Drawable? {
        return when (type) {
            is RoomType.Channel -> channelIcon
            is RoomType.PrivateGroup -> groupIcon
75 76 77 78 79
            else -> null
        }
    }

    private fun getStatusDrawable(status: UserStatus): Drawable {
80 81 82 83 84
        return when (status) {
            is UserStatus.Online -> onlineIcon
            is UserStatus.Away -> awayIcon
            is UserStatus.Busy -> busyIcon
            else -> offlineIcon
85 86 87
        }
    }
}