feat(dm): 날짜 타임라인과 스크롤 상태를 추가한다
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.chat.dm.model
|
||||||
|
|
||||||
|
class DmChatScrollState {
|
||||||
|
data class Anchor(val identity: DmChatTimelineIdentity, val top: Int)
|
||||||
|
data class Position(val position: Int, val top: Int)
|
||||||
|
|
||||||
|
sealed interface Update {
|
||||||
|
data object None : Update
|
||||||
|
data object Bottom : Update
|
||||||
|
class Restore(val anchor: Anchor) : Update
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Pending(var anchor: Anchor?, var prepended: Boolean = false)
|
||||||
|
|
||||||
|
private var pending: Pending? = null
|
||||||
|
private var postedRestore: Update.Restore? = null
|
||||||
|
|
||||||
|
fun beginPrepend(content: DmChatRoomUiState.Content, captureAnchor: () -> Anchor?): Boolean {
|
||||||
|
if (pending != null || content.isLoadingOlder || !content.hasMore || content.nextCursor == null) return false
|
||||||
|
postedRestore = null
|
||||||
|
pending = Pending(captureAnchor())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPrependCount(count: Int) {
|
||||||
|
pending?.prepended = count > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onContentSubmitted(isLoadingOlder: Boolean, wasNearBottom: Boolean): Update {
|
||||||
|
if (isLoadingOlder) return Update.None
|
||||||
|
val request = pending
|
||||||
|
if (request != null) {
|
||||||
|
pending = null
|
||||||
|
val anchor = request.anchor
|
||||||
|
if (request.prepended && anchor != null) {
|
||||||
|
return Update.Restore(anchor).also { postedRestore = it }
|
||||||
|
}
|
||||||
|
return Update.None
|
||||||
|
}
|
||||||
|
return if (postedRestore == null && wasNearBottom) Update.Bottom else Update.None
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resolveRestore(restore: Update.Restore, items: List<DmChatTimelineItem>): Position? {
|
||||||
|
if (postedRestore !== restore) return null
|
||||||
|
postedRestore = null
|
||||||
|
return items.positionOf(restore.anchor.identity)?.let { Position(it, restore.anchor.top) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onUserScroll() {
|
||||||
|
postedRestore = null
|
||||||
|
pending?.anchor = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.chat.dm.model
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.GregorianCalendar
|
||||||
|
import java.util.Locale
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
|
sealed interface DmChatTimelineItem {
|
||||||
|
data class DateHeader(val dateKey: String, val text: String) : DmChatTimelineItem
|
||||||
|
|
||||||
|
data class Message(val item: DmChatMessageUiItem) : DmChatTimelineItem
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed interface DmChatTimelineIdentity {
|
||||||
|
data class DateHeader(val dateKey: String) : DmChatTimelineIdentity
|
||||||
|
data class LocalMessage(val localId: String) : DmChatTimelineIdentity
|
||||||
|
data class ServerMessage(val messageId: Long) : DmChatTimelineIdentity
|
||||||
|
}
|
||||||
|
|
||||||
|
fun DmChatTimelineItem.identity(): DmChatTimelineIdentity = when (this) {
|
||||||
|
is DmChatTimelineItem.DateHeader -> DmChatTimelineIdentity.DateHeader(dateKey)
|
||||||
|
is DmChatTimelineItem.Message -> item.localId?.let(DmChatTimelineIdentity::LocalMessage)
|
||||||
|
?: DmChatTimelineIdentity.ServerMessage(requireNotNull(item.messageId))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<DmChatTimelineItem>.positionOf(identity: DmChatTimelineIdentity): Int? =
|
||||||
|
indexOfFirst { it.identity() == identity }.takeIf { it >= 0 }
|
||||||
|
|
||||||
|
fun List<DmChatTimelineItem>.firstMessagePosition(firstVisiblePosition: Int, lastVisiblePosition: Int): Int? {
|
||||||
|
if (firstVisiblePosition < 0) return null
|
||||||
|
return (firstVisiblePosition..minOf(lastVisiblePosition, lastIndex)).firstOrNull {
|
||||||
|
this[it] is DmChatTimelineItem.Message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isDmChatNearBottom(items: List<DmChatTimelineItem>, lastVisiblePosition: Int): Boolean =
|
||||||
|
items.withIndex().count { (position, item) -> position > lastVisiblePosition && item is DmChatTimelineItem.Message } < 2
|
||||||
|
|
||||||
|
class DmChatTimelineStableIds {
|
||||||
|
private val ids = mutableMapOf<DmChatTimelineIdentity, Long>()
|
||||||
|
private var nextId = 0L
|
||||||
|
|
||||||
|
fun idFor(identity: DmChatTimelineIdentity): Long = ids.getOrPut(identity) { nextId++ }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<DmChatMessageUiItem>.toTimelineItems(locale: Locale, timeZone: TimeZone): List<DmChatTimelineItem> {
|
||||||
|
if (isEmpty()) return emptyList()
|
||||||
|
|
||||||
|
val dateKeyFormatter = SimpleDateFormat("yyyy-MM-dd", Locale.ROOT).apply {
|
||||||
|
calendar = GregorianCalendar(timeZone, Locale.ROOT)
|
||||||
|
}
|
||||||
|
val dateTextFormatter = createDmChatDateFormatter(locale, timeZone)
|
||||||
|
return buildList {
|
||||||
|
var previousDateKey: String? = null
|
||||||
|
for (message in this@toTimelineItems) {
|
||||||
|
val date = Date(message.createdAt)
|
||||||
|
val dateKey = dateKeyFormatter.format(date)
|
||||||
|
if (dateKey != previousDateKey) {
|
||||||
|
add(DmChatTimelineItem.DateHeader(dateKey, dateTextFormatter.format(date)))
|
||||||
|
previousDateKey = dateKey
|
||||||
|
}
|
||||||
|
add(DmChatTimelineItem.Message(message))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.chat.dm
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageUiItem
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatRoomUiState
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatScrollState
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatScrollState.Update
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatTimelineIdentity
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.toTimelineItems
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
import java.util.Locale
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
|
class DmChatScrollStateTest {
|
||||||
|
private val scroll = DmChatScrollState()
|
||||||
|
private val anchor = DmChatScrollState.Anchor(DmChatTimelineIdentity.ServerMessage(2L), -17)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `실제 요청 가능한 경우에만 anchor를 기억한다`() {
|
||||||
|
assertFalse(scroll.beginPrepend(content(hasMore = false)) { anchor })
|
||||||
|
assertFalse(scroll.beginPrepend(content(nextCursor = null)) { anchor })
|
||||||
|
assertFalse(scroll.beginPrepend(content(loading = true)) { anchor })
|
||||||
|
assertEquals(Update.Bottom, scroll.onContentSubmitted(false, true))
|
||||||
|
assertTrue(scroll.beginPrepend(content()) { anchor })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `로딩과 실시간 Content는 anchor를 소비하거나 하단으로 이동하지 않는다`() {
|
||||||
|
assertTrue(scroll.beginPrepend(content()) { anchor })
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(true, true))
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(true, true))
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(true, true))
|
||||||
|
val restore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
assertEquals(anchor, restore.anchor)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `count 신호 뒤 완료 제출에서만 복원하고 하단 이동보다 우선한다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onContentSubmitted(true, false)
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
val restore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
|
||||||
|
assertEquals(DmChatScrollState.Position(3, -17), scroll.resolveRestore(restore, timeline(1L, 2L)))
|
||||||
|
assertNull(scroll.resolveRestore(restore, timeline(1L, 2L)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `요청 중 반복 스크롤 콜백은 저장한 anchor를 덮어쓰지 않는다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
assertFalse(scroll.beginPrepend(content()) { anchor.copy(top = 99) })
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
|
||||||
|
val restore = scroll.onContentSubmitted(false, false) as Update.Restore
|
||||||
|
assertEquals(anchor, restore.anchor)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `실패와 빈 결과와 중복 결과의 완료는 대기를 해제한다`() {
|
||||||
|
listOf<Int?>(null, 0, 0).forEach { count ->
|
||||||
|
assertTrue(scroll.beginPrepend(content()) { anchor })
|
||||||
|
scroll.onContentSubmitted(true, true)
|
||||||
|
count?.let(scroll::onPrependCount)
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(false, true))
|
||||||
|
assertEquals(Update.Bottom, scroll.onContentSubmitted(false, true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `재생성으로 재전달된 이전 count는 현재 요청에 적용하지 않는다`() {
|
||||||
|
scroll.onPrependCount(10)
|
||||||
|
assertEquals(Update.Bottom, scroll.onContentSubmitted(false, true))
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onContentSubmitted(true, true)
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(false, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `지연 복원은 제출 당시가 아니라 실행 당시 최신 목록에서 identity를 찾는다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
val restore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(false, true))
|
||||||
|
|
||||||
|
assertEquals(DmChatScrollState.Position(5, -17), scroll.resolveRestore(restore, timeline(0L, 1L, 2L)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `이후 요청이 시작되면 이전 지연 복원이 새 anchor를 추월하지 않는다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
val oldRestore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
scroll.beginPrepend(content()) { anchor.copy(top = 25) }
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
val newRestore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
|
||||||
|
assertNull(scroll.resolveRestore(oldRestore, timeline(1L, 2L)))
|
||||||
|
assertEquals(DmChatScrollState.Position(3, 25), scroll.resolveRestore(newRestore, timeline(1L, 2L)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `사용자가 스크롤하면 게시된 복원은 사용자의 이동을 덮어쓰지 않는다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
val restore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
scroll.onUserScroll()
|
||||||
|
|
||||||
|
assertNull(scroll.resolveRestore(restore, timeline(1L, 2L)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `요청 도중 사용자 이동은 요청 종료까지 하단 이동을 억제한다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onUserScroll()
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(true, true))
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(false, true))
|
||||||
|
assertEquals(Update.Bottom, scroll.onContentSubmitted(false, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `사라진 메시지나 가시 메시지가 없는 요청은 임의 위치로 복원하지 않는다`() {
|
||||||
|
scroll.beginPrepend(content()) { anchor }
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
val restore = scroll.onContentSubmitted(false, true) as Update.Restore
|
||||||
|
assertNull(scroll.resolveRestore(restore, timeline(1L)))
|
||||||
|
assertTrue(scroll.beginPrepend(content()) { null })
|
||||||
|
scroll.onPrependCount(1)
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(false, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `일반 갱신은 기존 near bottom일 때만 하단을 따른다`() {
|
||||||
|
assertEquals(Update.None, scroll.onContentSubmitted(false, false))
|
||||||
|
assertEquals(Update.Bottom, scroll.onContentSubmitted(false, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `요청 불가와 요청 중 콜백은 가시 anchor를 다시 수집하지 않는다`() {
|
||||||
|
var captures = 0
|
||||||
|
val capture = {
|
||||||
|
captures += 1
|
||||||
|
anchor
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(scroll.beginPrepend(content(hasMore = false), capture))
|
||||||
|
assertFalse(scroll.beginPrepend(content(nextCursor = null), capture))
|
||||||
|
assertFalse(scroll.beginPrepend(content(loading = true), capture))
|
||||||
|
assertEquals(0, captures)
|
||||||
|
assertTrue(scroll.beginPrepend(content(), capture))
|
||||||
|
assertFalse(scroll.beginPrepend(content(), capture))
|
||||||
|
assertEquals(1, captures)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun content(hasMore: Boolean = true, nextCursor: Long? = 1L, loading: Boolean = false) =
|
||||||
|
DmChatRoomUiState.Content(10L, "상대", "", emptyList(), hasMore, nextCursor, loading)
|
||||||
|
|
||||||
|
private fun timeline(vararg ids: Long) = ids.map { id ->
|
||||||
|
DmChatMessageUiItem(id, null, null, false, "메시지", "상대", "", id * 86_400_000L, DmChatMessageStatus.SENT)
|
||||||
|
}.toTimelineItems(Locale.KOREAN, TimeZone.getTimeZone("UTC"))
|
||||||
|
}
|
||||||
@@ -0,0 +1,312 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.chat.dm
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageStatus
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageUiItem
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatTimelineItem
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatTimelineIdentity
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.firstMessagePosition
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.identity
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.isDmChatNearBottom
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.positionOf
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.toTimelineItems
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertSame
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
import java.time.Instant
|
||||||
|
import java.util.Locale
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
|
class DmChatTimelineTest {
|
||||||
|
|
||||||
|
private val seoulTimeZone = TimeZone.getTimeZone("Asia/Seoul")
|
||||||
|
private val losAngelesTimeZone = TimeZone.getTimeZone("America/Los_Angeles")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `첫 메시지와 현지 날짜 경계마다 header를 한 개 만든다`() {
|
||||||
|
val first = message(1L, "2026-10-16T03:00:00Z")
|
||||||
|
val second = message(2L, "2026-10-16T04:00:00Z")
|
||||||
|
val nextDay = message(3L, "2026-10-17T03:00:00Z")
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-16", "2026년 10월 16일 금요일"),
|
||||||
|
DmChatTimelineItem.Message(first),
|
||||||
|
DmChatTimelineItem.Message(second),
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-17", "2026년 10월 17일 토요일"),
|
||||||
|
DmChatTimelineItem.Message(nextDay)
|
||||||
|
),
|
||||||
|
listOf(first, second, nextDay).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `빈 목록에는 header를 만들지 않는다`() {
|
||||||
|
assertEquals(
|
||||||
|
emptyList<DmChatTimelineItem>(),
|
||||||
|
emptyList<DmChatMessageUiItem>().toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `기기 현지 자정 경계에서 날짜 그룹을 밀리초 단위로 나눈다`() {
|
||||||
|
val messages = listOf(
|
||||||
|
message(1L, "2026-10-15T14:59:59.999Z"),
|
||||||
|
message(2L, "2026-10-15T15:00:00Z")
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-15", "2026년 10월 15일 목요일"),
|
||||||
|
DmChatTimelineItem.Message(messages[0]),
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-16", "2026년 10월 16일 금요일"),
|
||||||
|
DmChatTimelineItem.Message(messages[1])
|
||||||
|
),
|
||||||
|
messages.toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `동일 epoch도 서울과 로스앤젤레스에서 다른 현지 날짜가 된다`() {
|
||||||
|
val messages = listOf(message(1L, "2026-10-15T15:30:00Z"))
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-16", "2026년 10월 16일 금요일"),
|
||||||
|
messages.toTimelineItems(Locale.KOREAN, seoulTimeZone).firstOrNull()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-15", "2026년 10월 15일 목요일"),
|
||||||
|
messages.toTimelineItems(Locale.KOREAN, losAngelesTimeZone).firstOrNull()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `일광절약시간 시작일에도 같은 현지 날짜를 중복 분리하지 않는다`() {
|
||||||
|
val messages = listOf(
|
||||||
|
message(1L, "2026-03-08T08:30:00Z"),
|
||||||
|
message(2L, "2026-03-08T10:30:00Z"),
|
||||||
|
message(3L, "2026-03-09T06:59:59Z"),
|
||||||
|
message(4L, "2026-03-09T07:00:00Z")
|
||||||
|
)
|
||||||
|
val timeline = messages.toTimelineItems(Locale.ENGLISH, losAngelesTimeZone)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-03-08", "Sunday, March 8, 2026"),
|
||||||
|
DmChatTimelineItem.Message(messages[0]),
|
||||||
|
DmChatTimelineItem.Message(messages[1]),
|
||||||
|
DmChatTimelineItem.Message(messages[2]),
|
||||||
|
DmChatTimelineItem.DateHeader("2026-03-09", "Monday, March 9, 2026"),
|
||||||
|
DmChatTimelineItem.Message(messages[3])
|
||||||
|
),
|
||||||
|
timeline
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `일광절약시간 종료일의 반복 시각과 긴 하루도 한 날짜로 유지한다`() {
|
||||||
|
val messages = listOf(
|
||||||
|
message(1L, "2026-11-01T07:30:00Z"),
|
||||||
|
message(2L, "2026-11-01T08:30:00Z"),
|
||||||
|
message(3L, "2026-11-01T09:30:00Z"),
|
||||||
|
message(4L, "2026-11-02T07:59:59Z"),
|
||||||
|
message(5L, "2026-11-02T08:00:00Z")
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-11-01", "Sunday, November 1, 2026"),
|
||||||
|
DmChatTimelineItem.Message(messages[0]),
|
||||||
|
DmChatTimelineItem.Message(messages[1]),
|
||||||
|
DmChatTimelineItem.Message(messages[2]),
|
||||||
|
DmChatTimelineItem.Message(messages[3]),
|
||||||
|
DmChatTimelineItem.DateHeader("2026-11-02", "Monday, November 2, 2026"),
|
||||||
|
DmChatTimelineItem.Message(messages[4])
|
||||||
|
),
|
||||||
|
messages.toTimelineItems(Locale.ENGLISH, losAngelesTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `기존 첫 메시지와 같은 날짜의 과거 prepend에는 중복 header가 없다`() {
|
||||||
|
val existing = listOf(message(2L, "2026-10-16T04:00:00Z"))
|
||||||
|
val older = message(1L, "2026-10-16T03:00:00Z")
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-16", "2026년 10월 16일 금요일"),
|
||||||
|
DmChatTimelineItem.Message(older),
|
||||||
|
DmChatTimelineItem.Message(existing.single())
|
||||||
|
),
|
||||||
|
(listOf(older) + existing).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `다른 날짜의 과거 prepend에는 각 날짜 첫 메시지 앞에 header가 있다`() {
|
||||||
|
val existing = message(2L, "2026-10-16T04:00:00Z")
|
||||||
|
val older = message(1L, "2026-10-15T03:00:00Z")
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-15", "2026년 10월 15일 목요일"),
|
||||||
|
DmChatTimelineItem.Message(older),
|
||||||
|
DmChatTimelineItem.DateHeader("2026-10-16", "2026년 10월 16일 금요일"),
|
||||||
|
DmChatTimelineItem.Message(existing)
|
||||||
|
),
|
||||||
|
listOf(older, existing).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `projection은 메시지 원본 객체와 입력 순서 및 전송 상태를 보존한다`() {
|
||||||
|
val first = message(9L, "2026-10-16T04:00:00Z").copy(status = DmChatMessageStatus.FAILED)
|
||||||
|
val second = message(1L, "2026-10-16T03:00:00Z").copy(
|
||||||
|
messageId = null,
|
||||||
|
localId = "pending-local",
|
||||||
|
requestId = "pending-request",
|
||||||
|
status = DmChatMessageStatus.SENDING
|
||||||
|
)
|
||||||
|
val messages = mutableListOf(first, second)
|
||||||
|
val snapshot = messages.toList()
|
||||||
|
|
||||||
|
val projected = messages.toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
.filterIsInstance<DmChatTimelineItem.Message>()
|
||||||
|
|
||||||
|
assertEquals(snapshot, messages)
|
||||||
|
assertEquals(snapshot, projected.map { it.item })
|
||||||
|
assertSame(first, projected[0].item)
|
||||||
|
assertSame(second, projected[1].item)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `현지 연도 경계에서 연도와 날짜 키를 함께 변경한다`() {
|
||||||
|
val messages = listOf(
|
||||||
|
message(1L, "2026-12-31T14:59:59.999Z"),
|
||||||
|
message(2L, "2026-12-31T15:00:00Z")
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
DmChatTimelineItem.DateHeader("2026-12-31", "2026年12月31日木曜日"),
|
||||||
|
DmChatTimelineItem.Message(messages[0]),
|
||||||
|
DmChatTimelineItem.DateHeader("2027-01-01", "2027年1月1日金曜日"),
|
||||||
|
DmChatTimelineItem.Message(messages[1])
|
||||||
|
),
|
||||||
|
messages.toTimelineItems(Locale.JAPANESE, seoulTimeZone)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `날짜 키는 표시 언어와 달력 설정에 관계없이 Gregorian 숫자 날짜다`() {
|
||||||
|
val messages = listOf(message(1L, "2026-10-16T03:00:00Z"))
|
||||||
|
val locales = listOf(Locale.KOREAN, Locale.ENGLISH, Locale.JAPANESE, Locale.forLanguageTag("ja-JP-u-ca-japanese"))
|
||||||
|
|
||||||
|
locales.forEach { locale ->
|
||||||
|
val header = messages.toTimelineItems(locale, seoulTimeZone).firstOrNull() as? DmChatTimelineItem.DateHeader
|
||||||
|
assertEquals("2026-10-16", header?.dateKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `header를 포함한 목록에서 메시지 identity의 위치를 찾는다`() {
|
||||||
|
val timeline = listOf(
|
||||||
|
message(1L, "2026-10-16T03:00:00Z"),
|
||||||
|
message(2L, "2026-10-17T03:00:00Z")
|
||||||
|
).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
|
||||||
|
assertEquals(1, timeline.positionOf(DmChatTimelineIdentity.ServerMessage(1L)))
|
||||||
|
assertEquals(3, timeline.positionOf(DmChatTimelineIdentity.ServerMessage(2L)))
|
||||||
|
assertEquals(2, timeline.positionOf(DmChatTimelineIdentity.DateHeader("2026-10-17")))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `같은 날짜와 다른 날짜 prepend 뒤에도 기존 메시지의 위치를 찾는다`() {
|
||||||
|
val current = message(3L, "2026-10-16T04:00:00Z")
|
||||||
|
val sameDay = message(2L, "2026-10-16T03:00:00Z")
|
||||||
|
val otherDay = message(1L, "2026-10-15T03:00:00Z")
|
||||||
|
val anchor = DmChatTimelineIdentity.ServerMessage(3L)
|
||||||
|
|
||||||
|
assertEquals(2, listOf(sameDay, current).toTimelineItems(Locale.KOREAN, seoulTimeZone).positionOf(anchor))
|
||||||
|
assertEquals(4, listOf(otherDay, sameDay, current).toTimelineItems(Locale.KOREAN, seoulTimeZone).positionOf(anchor))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `목록에서 사라진 anchor는 임의 위치 대신 null을 반환한다`() {
|
||||||
|
val timeline = listOf(message(1L, "2026-10-16T03:00:00Z")).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
|
||||||
|
assertNull(timeline.positionOf(DmChatTimelineIdentity.ServerMessage(2L)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `pending이 자정 이후 ACK되면 날짜는 바뀌어도 local identity를 유지한다`() {
|
||||||
|
val pending = message(1L, "2026-10-15T14:59:59Z").copy(
|
||||||
|
messageId = null,
|
||||||
|
localId = "local-1",
|
||||||
|
status = DmChatMessageStatus.SENDING
|
||||||
|
)
|
||||||
|
val ack = pending.copy(
|
||||||
|
messageId = 99L,
|
||||||
|
createdAt = Instant.parse("2026-10-15T15:00:00Z").toEpochMilli(),
|
||||||
|
status = DmChatMessageStatus.SENT
|
||||||
|
)
|
||||||
|
val before = listOf(pending).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
val after = listOf(ack).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
|
||||||
|
assertEquals(DmChatTimelineIdentity.LocalMessage("local-1"), before[1].identity())
|
||||||
|
assertEquals(before[1].identity(), after[1].identity())
|
||||||
|
assertEquals(DmChatTimelineIdentity.DateHeader("2026-10-15"), before[0].identity())
|
||||||
|
assertEquals(DmChatTimelineIdentity.DateHeader("2026-10-16"), after[0].identity())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `near bottom은 남은 날짜 행이 아니라 메시지 두 개 경계를 따른다`() {
|
||||||
|
val timeline = listOf(
|
||||||
|
message(1L, "2026-10-15T03:00:00Z"),
|
||||||
|
message(2L, "2026-10-16T03:00:00Z"),
|
||||||
|
message(3L, "2026-10-17T03:00:00Z")
|
||||||
|
).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
|
||||||
|
assertFalse(isDmChatNearBottom(timeline, 1))
|
||||||
|
assertTrue(isDmChatNearBottom(timeline, 3))
|
||||||
|
assertTrue(isDmChatNearBottom(timeline, 4))
|
||||||
|
assertTrue(isDmChatNearBottom(timeline, 5))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `빈 목록은 near bottom이고 가시 위치가 없는 긴 목록은 아니다`() {
|
||||||
|
assertTrue(isDmChatNearBottom(emptyList(), -1))
|
||||||
|
val timeline = listOf(
|
||||||
|
message(1L, "2026-10-16T03:00:00Z"),
|
||||||
|
message(2L, "2026-10-16T04:00:00Z")
|
||||||
|
).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
assertFalse(isDmChatNearBottom(timeline, -1))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `첫 가시 행이 날짜면 그 뒤 첫 가시 메시지를 anchor로 선택한다`() {
|
||||||
|
val timeline = listOf(
|
||||||
|
message(1L, "2026-10-16T03:00:00Z"),
|
||||||
|
message(2L, "2026-10-16T04:00:00Z")
|
||||||
|
).toTimelineItems(Locale.KOREAN, seoulTimeZone)
|
||||||
|
|
||||||
|
assertEquals(1, timeline.firstMessagePosition(0, 2))
|
||||||
|
assertEquals(2, timeline.firstMessagePosition(2, 2))
|
||||||
|
assertNull(timeline.firstMessagePosition(0, 0))
|
||||||
|
assertNull(timeline.firstMessagePosition(-1, -1))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun message(messageId: Long, instant: String) = DmChatMessageUiItem(
|
||||||
|
messageId = messageId,
|
||||||
|
localId = null,
|
||||||
|
requestId = null,
|
||||||
|
mine = messageId % 2L == 0L,
|
||||||
|
textMessage = "메시지 $messageId",
|
||||||
|
senderNickname = "크리에이터",
|
||||||
|
senderProfileImageUrl = "https://example.com/profile.png",
|
||||||
|
createdAt = Instant.parse(instant).toEpochMilli(),
|
||||||
|
status = DmChatMessageStatus.SENT
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user