diff --git a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift index c58d1b92c..5fd108715 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift @@ -141,8 +141,13 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { fileprivate func updateRoomsWithDiffs(_ diffs: [SlidingSyncViewRoomsListDiff]) { rooms = diffs - .compactMap(buildDiff) - .reduce(rooms) { $0.applying($1) ?? $0 } + .reduce(rooms) { partialResult, diff in + 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 { @@ -186,7 +191,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { } } - private func buildDiff(from diff: SlidingSyncViewRoomsListDiff) -> CollectionDifference? { + private func buildDiff(from diff: SlidingSyncViewRoomsListDiff, on rooms: [RoomSummary]) -> CollectionDifference? { // Invalidations are a no-op for the moment if diff.isInvalidation { return nil @@ -217,12 +222,9 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { changes.append(.remove(offset: index, element: summary, associatedWith: nil)) } - values - .reversed() - .map { buildSummaryForRoomListEntry($0) } - .forEach { summary in - changes.append(.insert(offset: 0, element: summary, associatedWith: nil)) - } + for (index, value) in values.enumerated() { + changes.append(.insert(offset: index, element: buildSummaryForRoomListEntry(value), associatedWith: nil)) + } } return CollectionDifference(changes) diff --git a/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift b/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift index f139f9673..d4da0d741 100644 --- a/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift +++ b/ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift @@ -105,15 +105,20 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol { } // MARK: - Private - + private func updateItemsWithDiffs(_ diffs: [TimelineDiff]) { itemProxies = diffs - .compactMap(buildDiff) - .reduce(itemProxies) { $0.applying($1) ?? $0 } + .reduce(itemProxies) { partialResult, diff in + guard let collectionDiff = buildDiff(from: diff, on: partialResult) else { + return partialResult + } + + return partialResult.applying(collectionDiff) ?? partialResult + } } // swiftlint:disable:next cyclomatic_complexity - private func buildDiff(from diff: TimelineDiff) -> CollectionDifference? { + private func buildDiff(from diff: TimelineDiff, on itemProxies: [TimelineItemProxy]) -> CollectionDifference? { var changes = [CollectionDifference.Change]() switch diff.change() { @@ -150,12 +155,9 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol { changes.append(.remove(offset: index, element: itemProxy, associatedWith: nil)) } - items - .reversed() - .map { TimelineItemProxy(item: $0) } - .forEach { itemProxy in - changes.append(.insert(offset: 0, element: itemProxy, associatedWith: nil)) - } + for (index, item) in items.enumerated() { + changes.append(.insert(offset: index, element: TimelineItemProxy(item: item), associatedWith: nil)) + } } case .clear: for (index, itemProxy) in itemProxies.enumerated() {