Fix incorrect diffing behavior on replacement in both the timelineProvider and the roomSummaryProvider

This commit is contained in:
Stefan Ceriu
2022-11-22 18:41:03 +02:00
committed by Stefan Ceriu
parent cee628e651
commit 1db49a4600
2 changed files with 23 additions and 19 deletions
@@ -141,8 +141,13 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
fileprivate func updateRoomsWithDiffs(_ diffs: [SlidingSyncViewRoomsListDiff]) { fileprivate func updateRoomsWithDiffs(_ diffs: [SlidingSyncViewRoomsListDiff]) {
rooms = diffs rooms = diffs
.compactMap(buildDiff) .reduce(rooms) { partialResult, diff in
.reduce(rooms) { $0.applying($1) ?? $0 } guard let collectionDiff = buildDiff(from: diff, on: partialResult) else {
return partialResult
}
return partialResult.applying(collectionDiff) ?? partialResult
}
} }
private func buildEmptyRoomSummary(forIdentifier identifier: String = UUID().uuidString) -> RoomSummary { private func buildEmptyRoomSummary(forIdentifier identifier: String = UUID().uuidString) -> RoomSummary {
@@ -186,7 +191,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
} }
} }
private func buildDiff(from diff: SlidingSyncViewRoomsListDiff) -> CollectionDifference<RoomSummary>? { private func buildDiff(from diff: SlidingSyncViewRoomsListDiff, on rooms: [RoomSummary]) -> CollectionDifference<RoomSummary>? {
// Invalidations are a no-op for the moment // Invalidations are a no-op for the moment
if diff.isInvalidation { if diff.isInvalidation {
return nil return nil
@@ -217,11 +222,8 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
changes.append(.remove(offset: index, element: summary, associatedWith: nil)) changes.append(.remove(offset: index, element: summary, associatedWith: nil))
} }
values for (index, value) in values.enumerated() {
.reversed() changes.append(.insert(offset: index, element: buildSummaryForRoomListEntry(value), associatedWith: nil))
.map { buildSummaryForRoomListEntry($0) }
.forEach { summary in
changes.append(.insert(offset: 0, element: summary, associatedWith: nil))
} }
} }
@@ -108,12 +108,17 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
private func updateItemsWithDiffs(_ diffs: [TimelineDiff]) { private func updateItemsWithDiffs(_ diffs: [TimelineDiff]) {
itemProxies = diffs itemProxies = diffs
.compactMap(buildDiff) .reduce(itemProxies) { partialResult, diff in
.reduce(itemProxies) { $0.applying($1) ?? $0 } guard let collectionDiff = buildDiff(from: diff, on: partialResult) else {
return partialResult
}
return partialResult.applying(collectionDiff) ?? partialResult
}
} }
// swiftlint:disable:next cyclomatic_complexity // swiftlint:disable:next cyclomatic_complexity
private func buildDiff(from diff: TimelineDiff) -> CollectionDifference<TimelineItemProxy>? { private func buildDiff(from diff: TimelineDiff, on itemProxies: [TimelineItemProxy]) -> CollectionDifference<TimelineItemProxy>? {
var changes = [CollectionDifference<TimelineItemProxy>.Change]() var changes = [CollectionDifference<TimelineItemProxy>.Change]()
switch diff.change() { switch diff.change() {
@@ -150,11 +155,8 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
changes.append(.remove(offset: index, element: itemProxy, associatedWith: nil)) changes.append(.remove(offset: index, element: itemProxy, associatedWith: nil))
} }
items for (index, item) in items.enumerated() {
.reversed() changes.append(.insert(offset: index, element: TimelineItemProxy(item: item), associatedWith: nil))
.map { TimelineItemProxy(item: $0) }
.forEach { itemProxy in
changes.append(.insert(offset: 0, element: itemProxy, associatedWith: nil))
} }
} }
case .clear: case .clear: