Skip to content

Commit 0713788

Browse files
committed
chore(fc): attempt to group separators together between items
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 1fd1aa7 commit 0713788

3 files changed

Lines changed: 51 additions & 14 deletions

File tree

flipchatApp/src/main/kotlin/xyz/flipchat/app/features/chat/conversation/ConversationViewModel.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,7 @@ class ConversationViewModel @Inject constructor(
760760
var unreadSeparatorInserted = false
761761

762762
data.insertSeparators { before: ChatItem.Message?, after: ChatItem.Message? ->
763-
val beforeDate = before?.relativeDate
764-
val afterDate = after?.relativeDate
765-
766-
// if the date changes between two items, add a date separator
767-
if (beforeDate != afterDate) {
768-
return@insertSeparators beforeDate?.let { ChatItem.Date(before.date) }
769-
}
763+
val separators = mutableListOf<ChatItem.Separator>()
770764

771765
if (
772766
stateFlow.value.startAtUnread &&
@@ -776,11 +770,22 @@ class ConversationViewModel @Inject constructor(
776770
) {
777771
unreadSeparatorInserted = true
778772
val unreadCount = stateFlow.value.unreadCount ?: return@insertSeparators null
779-
return@insertSeparators ChatItem.UnreadSeparator(unreadCount)
773+
separators.add(ChatItem.UnreadSeparator(unreadCount))
774+
}
775+
776+
val beforeDate = before?.relativeDate
777+
val afterDate = after?.relativeDate
778+
779+
// if the date changes between two items, add a date separator
780+
if (beforeDate != afterDate) {
781+
beforeDate?.let { separators.add(ChatItem.Date(before.date)) }
780782
}
781783

782-
// No separator in other cases
783-
null
784+
if (separators.isNotEmpty()) {
785+
ChatItem.Separators(separators)
786+
} else {
787+
null
788+
}
784789
}
785790
}.cachedIn(viewModelScope)
786791

ui/components/src/main/kotlin/com/getcode/ui/components/chat/MessageList.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.runtime.snapshots.Snapshot
1919
import androidx.compose.ui.Modifier
2020
import androidx.compose.ui.text.TextStyle
2121
import androidx.compose.ui.unit.dp
22+
import androidx.compose.ui.util.fastForEach
2223
import androidx.paging.LoadState
2324
import androidx.paging.compose.LazyPagingItems
2425
import androidx.paging.compose.itemContentType
@@ -92,9 +93,10 @@ fun MessageList(
9293
key = messages.itemKey { item -> item.key },
9394
contentType = messages.itemContentType { item ->
9495
when (item) {
95-
is ChatItem.Date -> "separators"
96+
is ChatItem.Date -> "date"
9697
is ChatItem.Message -> "messages"
9798
is ChatItem.UnreadSeparator -> "unread_divider"
99+
is ChatItem.Separators -> "separators"
98100
}
99101
}
100102
) { index ->
@@ -179,6 +181,31 @@ fun MessageList(
179181
}
180182
}
181183

184+
is ChatItem.Separators -> {
185+
item.separators.fastForEach { separator ->
186+
when (separator) {
187+
is ChatItem.Date -> {
188+
DateBubble(
189+
modifier = Modifier
190+
.padding(vertical = CodeTheme.dimens.grid.x2),
191+
date = separator.dateString
192+
)
193+
}
194+
195+
is ChatItem.UnreadSeparator -> {
196+
if (separator.count > 0) {
197+
UnreadSeparator(
198+
modifier = Modifier
199+
.fillParentMaxWidth()
200+
.padding(vertical = CodeTheme.dimens.grid.x2),
201+
count = separator.count
202+
)
203+
}
204+
}
205+
}
206+
}
207+
}
208+
182209
else -> Unit
183210
}
184211
}
@@ -277,7 +304,7 @@ private fun HandleStartAtUnread(
277304
.collect { loadState ->
278305
if (loadState.refresh is LoadState.NotLoading && messages.itemCount > 0) {
279306
val separatorIndex = messages.itemSnapshotList
280-
.indexOfFirst { it is ChatItem.UnreadSeparator }
307+
.indexOfFirst { it is ChatItem.UnreadSeparator || (it is ChatItem.Separators && it.separators.any { it is ChatItem.UnreadSeparator }) }
281308

282309
if (separatorIndex > 0 && !hasSetAtUnread) {
283310
val previousItemIndex = separatorIndex - 1

ui/components/src/main/kotlin/com/getcode/ui/components/chat/utils/ChatItem.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ sealed class ChatItem(open val key: Any) {
4545
val relativeDate: String = date.formatDateRelatively()
4646
}
4747

48+
sealed interface Separator
49+
4850
@Stable
49-
data class UnreadSeparator(val count: Int) : ChatItem("unread")
51+
data class UnreadSeparator(val count: Int) : ChatItem("unread"), Separator
5052

5153
@Stable
52-
data class Date(val date: Instant) : ChatItem(date) {
54+
data class Date(val date: Instant) : ChatItem(date), Separator {
5355
val dateString: String = date.formatDateRelatively()
5456

5557
override val key: Any = date.toEpochMilliseconds()
5658
}
59+
60+
@Stable
61+
data class Separators(val separators: List<Separator>): ChatItem(separators.hashCode())
5762
}

0 commit comments

Comments
 (0)