mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-10-31 16:22:49 +00:00
Use reverse ordering for FilterEmptyDayPostProcessor (#6927)
It turns out the previous ordering was right: timestamp ordering is ascending, not descending
This commit is contained in:
committed by
GitHub
parent
abb1b2ad50
commit
1df94a563c
+5
-3
@@ -18,10 +18,10 @@ class FilterEmptyDayPostProcessor {
|
||||
* Filters out day separators from [items] for days that don't contain any events.
|
||||
*/
|
||||
fun process(items: List<MatrixTimelineItem>): List<MatrixTimelineItem> = buildList {
|
||||
// The timeline is ordered by descending timestamp, so events that happened during a day appear before the day separator for that day.
|
||||
// We can use this to determine if a day separator should be kept or not.
|
||||
// The timeline is ordered by ascending timestamp, so events that happened during a day appear after the day separator for that day.
|
||||
// We can use this to determine if a day separator should be kept or not by traversing the list in reverse.
|
||||
var hasEvent = false
|
||||
for (item in items) {
|
||||
for (item in items.asReversed()) {
|
||||
if (item is MatrixTimelineItem.Event) {
|
||||
hasEvent = true
|
||||
add(item)
|
||||
@@ -35,4 +35,6 @@ class FilterEmptyDayPostProcessor {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Then reverse the result to restore the original order, minus the empty day separators.
|
||||
.asReversed()
|
||||
}
|
||||
|
||||
+14
-14
@@ -32,36 +32,36 @@ class FilterEmptyDayPostProcessorTest {
|
||||
@Test
|
||||
fun `filterEmptyDaySeparators keeps day separator with events after it`() {
|
||||
val items = listOf(
|
||||
anEvent,
|
||||
aDaySeparator(TODAY),
|
||||
anEvent,
|
||||
)
|
||||
val result = FilterEmptyDayPostProcessor().process(items)
|
||||
assertThat(result).hasSize(2)
|
||||
assertThat(result[0]).isEqualTo(anEvent)
|
||||
assertThat(result[1]).isEqualTo(aDaySeparator(TODAY))
|
||||
assertThat(result[0]).isEqualTo(aDaySeparator(TODAY))
|
||||
assertThat(result[1]).isEqualTo(anEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `filterEmptyDaySeparators removes day separator with no events after it`() {
|
||||
val items = listOf(
|
||||
aDaySeparator(TODAY),
|
||||
aDaySeparator(YESTERDAY),
|
||||
aDaySeparator(TODAY),
|
||||
)
|
||||
val result = FilterEmptyDayPostProcessor().process(items)
|
||||
assertThat(result).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `filterEmptyDaySeparators removes first day separator and keeps second when only second has events`() {
|
||||
fun `filterEmptyDaySeparators removes second day separator and keeps first when only first has events`() {
|
||||
val items = listOf(
|
||||
aDaySeparator(TODAY),
|
||||
anEvent,
|
||||
aDaySeparator(YESTERDAY),
|
||||
anEvent,
|
||||
aDaySeparator(TODAY),
|
||||
)
|
||||
val result = FilterEmptyDayPostProcessor().process(items)
|
||||
assertThat(result).hasSize(2)
|
||||
assertThat(result[0]).isEqualTo(anEvent)
|
||||
assertThat(result[1]).isEqualTo(aDaySeparator(YESTERDAY))
|
||||
assertThat(result[0]).isEqualTo(aDaySeparator(YESTERDAY))
|
||||
assertThat(result[1]).isEqualTo(anEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,15 +78,15 @@ class FilterEmptyDayPostProcessorTest {
|
||||
@Test
|
||||
fun `filterEmptyDaySeparators keeps all items when no day separators`() {
|
||||
val items = listOf(
|
||||
anEvent,
|
||||
anEvent.copy(uniqueId = UniqueId("event2")),
|
||||
anEvent,
|
||||
)
|
||||
val result = FilterEmptyDayPostProcessor().process(items)
|
||||
assertThat(result).hasSize(2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `filterEmptyDaySeparators removes day separator followed by non-event virtual item`() {
|
||||
fun `filterEmptyDaySeparators removes day separator preceded by non-event virtual item`() {
|
||||
val readMarker = MatrixTimelineItem.Virtual(
|
||||
uniqueId = UniqueId("readMarker"),
|
||||
virtual = VirtualTimelineItem.ReadMarker
|
||||
@@ -107,12 +107,12 @@ class FilterEmptyDayPostProcessorTest {
|
||||
virtual = VirtualTimelineItem.ReadMarker
|
||||
)
|
||||
val items = listOf(
|
||||
anEvent,
|
||||
readMarker,
|
||||
aDaySeparator(TODAY),
|
||||
readMarker,
|
||||
anEvent,
|
||||
)
|
||||
val result = FilterEmptyDayPostProcessor().process(items)
|
||||
assertThat(result).hasSize(3)
|
||||
assertThat(result[2]).isEqualTo(aDaySeparator(TODAY))
|
||||
assertThat(result[0]).isEqualTo(aDaySeparator(TODAY))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user