AIRA-android/app/src/main/java/sushi/hardcore/aira/adapters/ChatAdapter.kt

246 lines
11 KiB
Kotlin
Raw Normal View History

2021-01-26 19:45:18 +01:00
package sushi.hardcore.aira.adapters
2021-06-16 20:57:11 +02:00
import android.annotation.SuppressLint
2021-01-26 19:45:18 +01:00
import android.content.Context
2021-06-22 15:45:31 +02:00
import android.graphics.drawable.GradientDrawable
import android.os.Handler
import android.os.Looper
2021-01-26 19:45:18 +01:00
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.updateMargins
2021-05-05 20:54:25 +02:00
import androidx.core.view.updatePadding
2021-01-26 19:45:18 +01:00
import androidx.recyclerview.widget.RecyclerView
import sushi.hardcore.aira.ChatItem
import sushi.hardcore.aira.R
2021-06-16 20:57:11 +02:00
import sushi.hardcore.aira.utils.StringUtils
import java.util.*
2021-01-26 19:45:18 +01:00
class ChatAdapter(
private val context: Context,
private val onSavingFile: (filename: String, rawUuid: ByteArray) -> Unit
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val BUBBLE_MARGIN = 150
const val CONTAINER_PADDING = 40
const val BUBBLE_VERTICAL_MARGIN = 40
2021-06-22 15:45:31 +02:00
const val BUBBLE_CORNER_NORMAL = 50f
const val BUBBLE_CORNER_ARROW = 20f
2021-01-26 19:45:18 +01:00
}
private val inflater: LayoutInflater = LayoutInflater.from(context)
private val chatItems = mutableListOf<ChatItem>()
fun newMessage(chatItem: ChatItem) {
chatItems.add(chatItem)
2021-06-22 15:45:31 +02:00
Handler(Looper.getMainLooper()).postDelayed({
notifyItemChanged(chatItems.size-2)
}, 100)
2021-01-26 19:45:18 +01:00
notifyItemInserted(chatItems.size-1)
}
fun newLoadedMessage(chatItem: ChatItem) {
chatItems.add(0, chatItem)
notifyItemInserted(0)
}
fun clear() {
chatItems.clear()
notifyDataSetChanged()
}
internal open class BubbleViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
2021-06-22 15:45:31 +02:00
private fun generateCorners(topLeft: Float, topRight: Float, bottomRight: Float, bottomLeft: Float): FloatArray {
return floatArrayOf(topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft)
}
protected fun configureContainer(outgoing: Boolean, previousOutgoing: Boolean?, isLast: Boolean) {
val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
if (previousOutgoing != null && previousOutgoing != outgoing) {
layoutParams.updateMargins(top = BUBBLE_VERTICAL_MARGIN)
}
2021-06-22 15:45:31 +02:00
if (isLast) {
layoutParams.updateMargins(bottom = BUBBLE_VERTICAL_MARGIN)
}
itemView.layoutParams = layoutParams //set layoutParams anyway to reset margins if the view was recycled
2021-01-26 19:45:18 +01:00
if (outgoing) {
itemView.updatePadding(right = CONTAINER_PADDING)
2021-01-26 19:45:18 +01:00
} else {
itemView.updatePadding(left = CONTAINER_PADDING)
}
}
2021-06-22 15:45:31 +02:00
protected fun configureBubble(context: Context, outgoing: Boolean, previousOutgoing: Boolean?, nextOutgoing: Boolean?) {
2021-06-16 20:57:11 +02:00
val bubble = itemView.findViewById<LinearLayout>(R.id.bubble_content)
bubble.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT).apply {
gravity = if (outgoing) {
marginStart = BUBBLE_MARGIN
Gravity.END
} else {
marginEnd = BUBBLE_MARGIN
Gravity.START
}
}
2021-06-22 15:45:31 +02:00
val backgroundDrawable = GradientDrawable()
backgroundDrawable.setColor(ContextCompat.getColor(context, if (outgoing) {
R.color.bubbleBackground
} else {
R.color.incomingBubbleBackground
}))
var topLeft = BUBBLE_CORNER_NORMAL
var topRight = BUBBLE_CORNER_NORMAL
var bottomRight = BUBBLE_CORNER_NORMAL
var bottomLeft = BUBBLE_CORNER_NORMAL
if (nextOutgoing == outgoing) {
if (outgoing) {
bottomRight = BUBBLE_CORNER_ARROW
} else {
bottomLeft = BUBBLE_CORNER_ARROW
}
2021-01-26 19:45:18 +01:00
}
2021-06-22 15:45:31 +02:00
if (previousOutgoing == outgoing) {
if (outgoing) {
topRight = BUBBLE_CORNER_ARROW
} else {
topLeft = BUBBLE_CORNER_ARROW
}
}
backgroundDrawable.cornerRadii = generateCorners(topLeft, topRight, bottomRight, bottomLeft)
bubble.background = backgroundDrawable
2021-01-26 19:45:18 +01:00
}
2021-06-16 20:57:11 +02:00
protected fun setTimestamp(chatItem: ChatItem): TextView {
val calendar = Calendar.getInstance().apply {
time = Date(chatItem.timestamp * 1000)
}
val textView = itemView.findViewById<TextView>(R.id.timestamp)
@SuppressLint("SetTextI18n")
textView.text = StringUtils.toTwoDigits(calendar.get(Calendar.HOUR_OF_DAY))+":"+StringUtils.toTwoDigits(calendar.get(Calendar.MINUTE))
return textView
}
2021-01-26 19:45:18 +01:00
}
internal open class MessageViewHolder(itemView: View): BubbleViewHolder(itemView) {
protected fun bindMessage(chatItem: ChatItem, outgoing: Boolean): TextView {
2021-01-26 19:45:18 +01:00
itemView.findViewById<TextView>(R.id.text_message).apply {
text = chatItem.data.sliceArray(1 until chatItem.data.size).decodeToString()
if (!outgoing) {
highlightColor = ContextCompat.getColor(context, R.color.incomingHighlight)
}
return this
2021-01-26 19:45:18 +01:00
}
}
}
internal class OutgoingMessageViewHolder(private val context: Context, itemView: View): MessageViewHolder(itemView) {
2021-06-22 15:45:31 +02:00
fun bind(chatItem: ChatItem, previousOutgoing: Boolean?, nextOutgoing: Boolean?) {
2021-06-16 20:57:11 +02:00
setTimestamp(chatItem).apply {
setTextColor(ContextCompat.getColor(context, R.color.outgoingTimestamp))
}
2021-06-22 15:45:31 +02:00
configureBubble(context, true, previousOutgoing, nextOutgoing)
bindMessage(chatItem, true).apply {
2021-06-16 20:57:11 +02:00
setLinkTextColor(ContextCompat.getColor(context, R.color.outgoingTextLink))
}
2021-06-22 15:45:31 +02:00
configureContainer(true, previousOutgoing, nextOutgoing == null)
}
}
internal class IncomingMessageViewHolder(private val context: Context, itemView: View): MessageViewHolder(itemView) {
2021-06-22 15:45:31 +02:00
fun bind(chatItem: ChatItem, previousOutgoing: Boolean?, nextOutgoing: Boolean?) {
2021-06-16 20:57:11 +02:00
setTimestamp(chatItem).apply {
setTextColor(ContextCompat.getColor(context, R.color.incomingTimestamp))
}
2021-06-22 15:45:31 +02:00
configureBubble(context, false, previousOutgoing, nextOutgoing)
bindMessage(chatItem, false).apply {
setLinkTextColor(ContextCompat.getColor(context, R.color.incomingTextLink))
2021-06-16 20:57:11 +02:00
}
2021-06-22 15:45:31 +02:00
configureContainer(false, previousOutgoing, nextOutgoing == null)
}
}
internal open class FileViewHolder(itemView: View, private val onSavingFile: (filename: String, rawUuid: ByteArray) -> Unit): BubbleViewHolder(itemView) {
protected fun bindFile(chatItem: ChatItem, outgoing: Boolean) {
2021-01-26 19:45:18 +01:00
val filename = chatItem.data.sliceArray(17 until chatItem.data.size).decodeToString()
itemView.findViewById<TextView>(R.id.text_filename).apply {
text = filename
if (!outgoing) {
highlightColor = ContextCompat.getColor(context, R.color.incomingHighlight)
}
}
2021-01-26 19:45:18 +01:00
itemView.findViewById<ImageButton>(R.id.button_save).setOnClickListener {
onSavingFile(filename, chatItem.data.sliceArray(1 until 17))
}
}
}
internal class OutgoingFileViewHolder(private val context: Context, itemView: View, onSavingFile: (filename: String, rawUuid: ByteArray) -> Unit): FileViewHolder(itemView, onSavingFile) {
2021-06-22 15:45:31 +02:00
fun bind(chatItem: ChatItem, previousOutgoing: Boolean?, nextOutgoing: Boolean?) {
2021-06-16 20:57:11 +02:00
setTimestamp(chatItem).apply {
setTextColor(ContextCompat.getColor(context, R.color.outgoingTimestamp))
}
bindFile(chatItem, true)
2021-06-22 15:45:31 +02:00
configureBubble(context, true, previousOutgoing, nextOutgoing)
configureContainer(true, previousOutgoing, nextOutgoing == null)
}
}
internal class IncomingFileViewHolder(private val context: Context, itemView: View, onSavingFile: (filename: String, rawUuid: ByteArray) -> Unit): FileViewHolder(itemView, onSavingFile) {
2021-06-22 15:45:31 +02:00
fun bind(chatItem: ChatItem, previousOutgoing: Boolean?, nextOutgoing: Boolean?) {
2021-06-16 20:57:11 +02:00
setTimestamp(chatItem).apply {
setTextColor(ContextCompat.getColor(context, R.color.incomingTimestamp))
}
bindFile(chatItem, false)
2021-06-22 15:45:31 +02:00
configureBubble(context, false, previousOutgoing, nextOutgoing)
configureContainer(false, previousOutgoing, nextOutgoing == null)
2021-01-26 19:45:18 +01:00
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == ChatItem.OUTGOING_MESSAGE || viewType == ChatItem.INCOMING_MESSAGE) {
val view = inflater.inflate(R.layout.adapter_chat_message, parent, false)
if (viewType == ChatItem.OUTGOING_MESSAGE) {
OutgoingMessageViewHolder(context, view)
} else {
IncomingMessageViewHolder(context, view)
2021-01-26 19:45:18 +01:00
}
} else {
val view = inflater.inflate(R.layout.adapter_chat_file, parent, false)
if (viewType == ChatItem.OUTGOING_FILE) {
OutgoingFileViewHolder(context, view, onSavingFile)
} else {
IncomingFileViewHolder(context, view, onSavingFile)
2021-01-26 19:45:18 +01:00
}
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val chatItem = chatItems[position]
val previousOutgoing = if (position == 0) {
null
} else {
chatItems[position-1].outgoing
}
2021-06-22 15:45:31 +02:00
val nextOutgoing = if (position == chatItems.size - 1) {
null
} else {
chatItems[position+1].outgoing
}
2021-01-26 19:45:18 +01:00
when (chatItem.itemType) {
2021-06-22 15:45:31 +02:00
ChatItem.OUTGOING_MESSAGE -> (holder as OutgoingMessageViewHolder).bind(chatItem, previousOutgoing, nextOutgoing)
ChatItem.INCOMING_MESSAGE -> (holder as IncomingMessageViewHolder).bind(chatItem, previousOutgoing, nextOutgoing)
ChatItem.OUTGOING_FILE -> (holder as OutgoingFileViewHolder).bind(chatItem, previousOutgoing, nextOutgoing)
ChatItem.INCOMING_FILE -> (holder as IncomingFileViewHolder).bind(chatItem, previousOutgoing, nextOutgoing)
2021-01-26 19:45:18 +01:00
}
}
override fun getItemCount(): Int {
return chatItems.size
}
override fun getItemViewType(position: Int): Int {
return chatItems[position].itemType
}
}