ChatRoomsAdapter.kt 7.25 KB
Newer Older
1 2
package chat.rocket.android.chatrooms.ui

3
import DateTimeHelper
4
import DrawableHelper
5
import android.content.Context
6
import android.graphics.Color
7
import android.support.v4.content.ContextCompat
8
import android.support.v7.widget.RecyclerView
9 10
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
11 12 13 14
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import chat.rocket.android.R
15
import chat.rocket.android.helper.UrlHelper
16 17
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.checkIfMyself
18 19
import chat.rocket.android.server.domain.PublicSettings
import chat.rocket.android.server.domain.useRealName
20
import chat.rocket.android.util.extensions.content
21 22 23
import chat.rocket.android.util.extensions.inflate
import chat.rocket.android.util.extensions.setVisible
import chat.rocket.android.util.extensions.textContent
Lucio Maciel's avatar
Lucio Maciel committed
24
import chat.rocket.common.model.RoomType
25 26
import chat.rocket.core.model.ChatRoom
import com.facebook.drawee.view.SimpleDraweeView
27
import kotlinx.android.synthetic.main.avatar.view.*
28
import kotlinx.android.synthetic.main.item_chat.view.*
29
import kotlinx.android.synthetic.main.unread_messages_badge.view.*
30

31
class ChatRoomsAdapter(private val context: Context,
32
                       private val settings: PublicSettings,
33
                       private val localRepository: LocalRepository,
34
                       private val listener: (ChatRoom) -> Unit) : RecyclerView.Adapter<ChatRoomsAdapter.ViewHolder>() {
35
    var dataSet: MutableList<ChatRoom> = ArrayList()
36

37
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder(parent.inflate(R.layout.item_chat))
38

39
    override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(dataSet[position])
40

41 42
    override fun getItemCount(): Int = dataSet.size

43
    fun updateRooms(newRooms: List<ChatRoom>) {
44 45 46
        dataSet.clear()
        dataSet.addAll(newRooms)
    }
47

48 49 50
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind(chatRoom: ChatRoom) = with(itemView) {
51
            bindAvatar(chatRoom, image_avatar)
52 53 54 55
            bindName(chatRoom, text_chat_name)
            bindLastMessageDateTime(chatRoom, text_last_message_date_time)
            bindLastMessage(chatRoom, text_last_message)
            bindUnreadMessages(chatRoom, text_total_unread_messages)
56

57
            if (chatRoom.alert || chatRoom.unread > 0) {
58
                text_chat_name.setTextColor(ContextCompat.getColor(context,
Lucio Maciel's avatar
Lucio Maciel committed
59
                        R.color.colorPrimaryText))
60 61 62 63 64 65
                text_last_message_date_time.setTextColor(ContextCompat.getColor(context,
                        R.color.colorAccent))
                text_last_message.setTextColor(ContextCompat.getColor(context,
                        android.R.color.primary_text_light))
            } else {
                text_chat_name.setTextColor(ContextCompat.getColor(context,
Lucio Maciel's avatar
Lucio Maciel committed
66
                        R.color.colorSecondaryText))
67 68 69 70
                text_last_message_date_time.setTextColor(ContextCompat.getColor(context,
                        R.color.colorSecondaryText))
                text_last_message.setTextColor(ContextCompat.getColor(context,
                        R.color.colorSecondaryText))
71 72
            }

73
            setOnClickListener { listener(chatRoom) }
74
        }
75

76
        private fun bindAvatar(chatRoom: ChatRoom, drawee: SimpleDraweeView) {
Lucio Maciel's avatar
Lucio Maciel committed
77
            val avatarId = if (chatRoom.type is RoomType.DirectMessage) chatRoom.name else "@${chatRoom.name}"
78
            drawee.setImageURI(UrlHelper.getAvatarUrl(chatRoom.client.url, avatarId))
79 80 81
        }

        private fun bindName(chatRoom: ChatRoom, textView: TextView) {
82 83
            textView.textContent = chatRoom.name

Lucio Maciel's avatar
Lucio Maciel committed
84
            var drawable = when (chatRoom.type) {
85
                is RoomType.Channel -> {
Lucio Maciel's avatar
Lucio Maciel committed
86
                    DrawableHelper.getDrawableFromId(R.drawable.ic_room_channel, context)
87 88
                }
                is RoomType.PrivateGroup -> {
Lucio Maciel's avatar
Lucio Maciel committed
89
                    DrawableHelper.getDrawableFromId(R.drawable.ic_room_lock, context)
90 91
                }
                is RoomType.DirectMessage -> {
Lucio Maciel's avatar
Lucio Maciel committed
92
                    DrawableHelper.getDrawableFromId(R.drawable.ic_room_dm, context)
93
                }
Lucio Maciel's avatar
Lucio Maciel committed
94
                else -> null
95
            }
96 97

            drawable?.let {
98 99
                val wrappedDrawable = DrawableHelper.wrapDrawable(it)
                val mutableDrawable = wrappedDrawable.mutate()
Lucio Maciel's avatar
Lucio Maciel committed
100 101 102 103 104
                val color = when (chatRoom.alert || chatRoom.unread > 0) {
                    true -> R.color.colorPrimaryText
                    false -> R.color.colorSecondaryText
                }
                DrawableHelper.tintDrawable(mutableDrawable, context, color)
105
                DrawableHelper.compoundDrawable(textView, mutableDrawable)
106
            }
107 108 109 110 111 112
        }

        private fun bindLastMessageDateTime(chatRoom: ChatRoom, textView: TextView) {
            val lastMessage = chatRoom.lastMessage
            if (lastMessage != null) {
                val localDateTime = DateTimeHelper.getLocalDateTime(lastMessage.timestamp)
113
                textView.content = DateTimeHelper.getDate(localDateTime, context)
114
            } else {
115
                textView.content = ""
116 117 118
            }
        }

119 120 121 122 123
        private fun bindLastMessage(chatRoom: ChatRoom, textView: TextView) {
            val lastMessage = chatRoom.lastMessage
            val lastMessageSender = lastMessage?.sender
            if (lastMessage != null && lastMessageSender != null) {
                val message = lastMessage.message
124 125 126 127 128
                val senderUsername = if (settings.useRealName()) {
                    lastMessageSender.name ?: lastMessageSender.username
                } else {
                    lastMessageSender.username
                }
129 130
                when (senderUsername) {
                    chatRoom.name -> {
131
                        textView.content = message
132 133
                    }
                    else -> {
134 135 136 137 138 139 140 141 142 143
                        val user = if (localRepository.checkIfMyself(lastMessageSender.username!!)) {
                            "${context.getString(R.string.msg_you)}: "
                        } else {
                            "$senderUsername: "
                        }
                        val spannable = SpannableStringBuilder(user)
                        val len = spannable.length
                        spannable.setSpan(ForegroundColorSpan(Color.BLACK), 0, len - 1, 0)
                        spannable.append(message)
                        textView.content = spannable
144
                    }
145
                }
146
            } else {
147
                textView.content = context.getText(R.string.msg_no_messages_yet)
148 149 150
            }
        }

151 152 153 154 155
        private fun bindUnreadMessages(chatRoom: ChatRoom, textView: TextView) {
            val totalUnreadMessage = chatRoom.unread
            when {
                totalUnreadMessage in 1..99 -> {
                    textView.textContent = totalUnreadMessage.toString()
156
                    textView.setVisible(true)
157 158 159
                }
                totalUnreadMessage > 99 -> {
                    textView.textContent = context.getString(R.string.msg_more_than_ninety_nine_unread_messages)
160
                    textView.setVisible(true)
161
                }
162
                else -> textView.setVisible(false)
163 164
            }
        }
165 166
    }
}