feat(chat): 채팅방 목록 Adapter를 추가한다
This commit is contained in:
@@ -0,0 +1,86 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.chat.ui
|
||||||
|
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import coil.transform.CircleCropTransformation
|
||||||
|
import coil.transform.Transformation
|
||||||
|
import kr.co.vividnext.sodalive.R
|
||||||
|
import kr.co.vividnext.sodalive.extensions.loadUrl
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.model.ChatRoomListUiItem
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.model.formatChatRoomLastMessageTime
|
||||||
|
|
||||||
|
class ChatRoomListAdapter(
|
||||||
|
private val onItemClick: (ChatRoomListUiItem) -> Unit
|
||||||
|
) : RecyclerView.Adapter<ChatRoomListAdapter.ChatRoomViewHolder>() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun profileImageTransformations(): List<Transformation> = listOf(CircleCropTransformation())
|
||||||
|
}
|
||||||
|
|
||||||
|
private var items: List<ChatRoomListUiItem> = emptyList()
|
||||||
|
|
||||||
|
fun submitItems(newItems: List<ChatRoomListUiItem>) {
|
||||||
|
val diffResult = DiffUtil.calculateDiff(ChatRoomDiffCallback(items, newItems))
|
||||||
|
items = newItems
|
||||||
|
diffResult.dispatchUpdatesTo(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatRoomViewHolder {
|
||||||
|
val view = LayoutInflater.from(parent.context)
|
||||||
|
.inflate(R.layout.item_v2_chat_room, parent, false)
|
||||||
|
return ChatRoomViewHolder(view, onItemClick)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ChatRoomViewHolder, position: Int) {
|
||||||
|
holder.bind(items[position])
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = items.size
|
||||||
|
|
||||||
|
private class ChatRoomDiffCallback(
|
||||||
|
private val oldList: List<ChatRoomListUiItem>,
|
||||||
|
private val newList: List<ChatRoomListUiItem>
|
||||||
|
) : DiffUtil.Callback() {
|
||||||
|
override fun getOldListSize(): Int = oldList.size
|
||||||
|
override fun getNewListSize(): Int = newList.size
|
||||||
|
override fun areItemsTheSame(oldPos: Int, newPos: Int): Boolean =
|
||||||
|
oldList[oldPos].roomId == newList[newPos].roomId
|
||||||
|
override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean =
|
||||||
|
oldList[oldPos] == newList[newPos]
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChatRoomViewHolder(
|
||||||
|
itemView: View,
|
||||||
|
private val onItemClick: (ChatRoomListUiItem) -> Unit
|
||||||
|
) : RecyclerView.ViewHolder(itemView) {
|
||||||
|
|
||||||
|
private val ivProfile = itemView.findViewById<ImageView>(R.id.iv_profile)
|
||||||
|
private val tvName = itemView.findViewById<TextView>(R.id.tv_name)
|
||||||
|
private val tvDirectBadge = itemView.findViewById<TextView>(R.id.tv_direct_badge)
|
||||||
|
private val tvTime = itemView.findViewById<TextView>(R.id.tv_time)
|
||||||
|
private val tvLastMessage = itemView.findViewById<TextView>(R.id.tv_last_message)
|
||||||
|
|
||||||
|
fun bind(item: ChatRoomListUiItem) {
|
||||||
|
ivProfile.loadUrl(item.targetImageUrl) {
|
||||||
|
transformations(profileImageTransformations())
|
||||||
|
}
|
||||||
|
tvName.text = item.targetName
|
||||||
|
tvLastMessage.text = item.lastMessage
|
||||||
|
tvTime.text = formatChatRoomLastMessageTime(
|
||||||
|
itemView.context,
|
||||||
|
item.lastMessageAt
|
||||||
|
)
|
||||||
|
tvDirectBadge.visibility = if (item.showDirectBadge) {
|
||||||
|
View.VISIBLE
|
||||||
|
} else {
|
||||||
|
View.GONE
|
||||||
|
}
|
||||||
|
itemView.setOnClickListener { onItemClick(item) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.chat
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.FrameLayout
|
||||||
|
import android.widget.TextView
|
||||||
|
import coil.transform.CircleCropTransformation
|
||||||
|
import kr.co.vividnext.sodalive.R
|
||||||
|
import kr.co.vividnext.sodalive.common.ImageLoaderProvider
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.model.ChatRoomListUiItem
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.model.ChatRoomType
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.model.formatChatRoomLastMessageTime
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.ui.ChatRoomListAdapter
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.RuntimeEnvironment
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(sdk = [28], application = Application::class)
|
||||||
|
class ChatRoomListAdapterTest {
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
if (!ImageLoaderProvider.isInitialized) {
|
||||||
|
ImageLoaderProvider.init(RuntimeEnvironment.getApplication())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createItem(
|
||||||
|
chatType: ChatRoomType = ChatRoomType.AI,
|
||||||
|
roomId: Long = 1L,
|
||||||
|
targetName: String = "테스트",
|
||||||
|
targetImageUrl: String = "https://example.com/img.png",
|
||||||
|
lastMessage: String = "안녕하세요",
|
||||||
|
lastMessageAt: String = "2026-06-09T12:00:00+09:00",
|
||||||
|
showDirectBadge: Boolean = chatType == ChatRoomType.DM
|
||||||
|
) = ChatRoomListUiItem(
|
||||||
|
roomId = roomId,
|
||||||
|
chatType = chatType,
|
||||||
|
targetName = targetName,
|
||||||
|
targetImageUrl = targetImageUrl,
|
||||||
|
lastMessage = lastMessage,
|
||||||
|
lastMessageAt = lastMessageAt,
|
||||||
|
showDirectBadge = showDirectBadge
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun bindHolder(
|
||||||
|
adapter: ChatRoomListAdapter,
|
||||||
|
position: Int
|
||||||
|
): View {
|
||||||
|
val context = RuntimeEnvironment.getApplication()
|
||||||
|
val parent = FrameLayout(context)
|
||||||
|
val holder = adapter.onCreateViewHolder(parent, 0)
|
||||||
|
adapter.onBindViewHolder(holder, position)
|
||||||
|
return holder.itemView
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `DM item bind 시 Direct badge가 VISIBLE이다`() {
|
||||||
|
val adapter = ChatRoomListAdapter {}
|
||||||
|
adapter.submitItems(listOf(createItem(chatType = ChatRoomType.DM)))
|
||||||
|
|
||||||
|
val itemView = bindHolder(adapter, 0)
|
||||||
|
val badge = itemView.findViewById<TextView>(R.id.tv_direct_badge)
|
||||||
|
assertEquals(View.VISIBLE, badge.visibility)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `AI item bind 시 Direct badge가 GONE이다`() {
|
||||||
|
val adapter = ChatRoomListAdapter {}
|
||||||
|
adapter.submitItems(listOf(createItem(chatType = ChatRoomType.AI)))
|
||||||
|
|
||||||
|
val itemView = bindHolder(adapter, 0)
|
||||||
|
val badge = itemView.findViewById<TextView>(R.id.tv_direct_badge)
|
||||||
|
assertEquals(View.GONE, badge.visibility)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `name과 lastMessage가 TextView에 표시된다`() {
|
||||||
|
val item = createItem(
|
||||||
|
targetName = "캐릭터A",
|
||||||
|
lastMessage = "반갑습니다"
|
||||||
|
)
|
||||||
|
val adapter = ChatRoomListAdapter {}
|
||||||
|
adapter.submitItems(listOf(item))
|
||||||
|
|
||||||
|
val itemView = bindHolder(adapter, 0)
|
||||||
|
val tvName = itemView.findViewById<TextView>(R.id.tv_name)
|
||||||
|
val tvLastMessage = itemView.findViewById<TextView>(R.id.tv_last_message)
|
||||||
|
assertEquals("캐릭터A", tvName.text.toString())
|
||||||
|
assertEquals("반갑습니다", tvLastMessage.text.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `lastMessageAt이 시간 포맷 문자열로 변환되어 표시된다`() {
|
||||||
|
val item = createItem(lastMessageAt = "2026-06-09T12:00:00+09:00")
|
||||||
|
val context = RuntimeEnvironment.getApplication()
|
||||||
|
val adapter = ChatRoomListAdapter {}
|
||||||
|
adapter.submitItems(listOf(item))
|
||||||
|
|
||||||
|
val itemView = bindHolder(adapter, 0)
|
||||||
|
val tvTime = itemView.findViewById<TextView>(R.id.tv_time)
|
||||||
|
val expected = formatChatRoomLastMessageTime(context, item.lastMessageAt)
|
||||||
|
assertEquals(expected, tvTime.text.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `profile image는 원형 변환을 사용한다`() {
|
||||||
|
val transformations = ChatRoomListAdapter.profileImageTransformations()
|
||||||
|
|
||||||
|
assertEquals(1, transformations.size)
|
||||||
|
assertEquals(CircleCropTransformation::class, transformations.first()::class)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `item 클릭 시 bound ChatRoomListUiItem을 listener로 전달한다`() {
|
||||||
|
var clickedItem: ChatRoomListUiItem? = null
|
||||||
|
val item = createItem(roomId = 42L)
|
||||||
|
val adapter = ChatRoomListAdapter { clickedItem = it }
|
||||||
|
adapter.submitItems(listOf(item))
|
||||||
|
|
||||||
|
val itemView = bindHolder(adapter, 0)
|
||||||
|
itemView.performClick()
|
||||||
|
assertEquals(42L, clickedItem?.roomId)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `layout에 unread dot id가 존재하지 않는다`() {
|
||||||
|
val adapter = ChatRoomListAdapter {}
|
||||||
|
adapter.submitItems(listOf(createItem()))
|
||||||
|
|
||||||
|
val itemView = bindHolder(adapter, 0)
|
||||||
|
val unreadDot = itemView.findViewById<View>(
|
||||||
|
itemView.resources.getIdentifier(
|
||||||
|
"iv_unread_dot",
|
||||||
|
"id",
|
||||||
|
itemView.context.packageName
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertNull(unreadDot)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user