mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
SDK Bump + Back pagination state listener (#1274)
* backPagination improved * sdk bump * removed unused line * Update ElementX/Sources/Services/Timeline/TimelineController/RoomTimelineController.swift Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> * cancelling the TaskHandle on deallocation --------- Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import MatrixRustSDK
|
||||
|
||||
struct MockRoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
var itemsPublisher = CurrentValuePublisher<[TimelineItemProxy], Never>([])
|
||||
|
||||
private var itemProxies = [TimelineItemProxy]()
|
||||
}
|
||||
@@ -21,31 +21,41 @@ import MatrixRustSDK
|
||||
class RoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private let serialDispatchQueue: DispatchQueue
|
||||
|
||||
private let itemsSubject = CurrentValueSubject<[TimelineItemProxy], Never>([])
|
||||
|
||||
var itemsPublisher: CurrentValuePublisher<[TimelineItemProxy], Never> {
|
||||
itemsSubject.asCurrentValuePublisher()
|
||||
}
|
||||
|
||||
private var itemProxies: [TimelineItemProxy] {
|
||||
didSet {
|
||||
itemsSubject.send(itemProxies)
|
||||
}
|
||||
}
|
||||
|
||||
init(currentItems: [TimelineItem], updatePublisher: AnyPublisher<TimelineDiff, Never>) {
|
||||
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomtimelineprovider", qos: .utility)
|
||||
|
||||
itemProxies = currentItems.map(TimelineItemProxy.init)
|
||||
private let backPaginationStateSubject = CurrentValueSubject<BackPaginationStatus, Never>(.idle)
|
||||
var backPaginationState: BackPaginationStatus {
|
||||
backPaginationStateSubject.value
|
||||
}
|
||||
|
||||
private let itemProxiesSubject: CurrentValueSubject<[TimelineItemProxy], Never>
|
||||
var itemProxies: [TimelineItemProxy] {
|
||||
itemProxiesSubject.value
|
||||
}
|
||||
|
||||
var updatePublisher: AnyPublisher<TimelineProviderUpdate, Never> {
|
||||
itemProxiesSubject
|
||||
.combineLatest(backPaginationStateSubject)
|
||||
.map(TimelineProviderUpdate.init)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(currentItems: [TimelineItem],
|
||||
updatePublisher: AnyPublisher<TimelineDiff, Never>,
|
||||
backPaginationStatePublisher: AnyPublisher<BackPaginationStatus, Never>) {
|
||||
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomtimelineprovider", qos: .utility)
|
||||
itemProxiesSubject = CurrentValueSubject<[TimelineItemProxy], Never>(currentItems.map(TimelineItemProxy.init))
|
||||
|
||||
// Manually call it here as the didSet doesn't work from constructors
|
||||
itemsSubject.send(itemProxies)
|
||||
|
||||
itemProxiesSubject.send(itemProxies)
|
||||
|
||||
updatePublisher
|
||||
.collect(.byTime(serialDispatchQueue, 0.1))
|
||||
.sink { [weak self] in self?.updateItemsWithDiffs($0) }
|
||||
.store(in: &cancellables)
|
||||
|
||||
backPaginationStatePublisher
|
||||
.sink { [weak self] in self?.backPaginationStateSubject.send($0) }
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
@@ -59,7 +69,7 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
|
||||
MXLog.verbose("Received timeline diff")
|
||||
|
||||
itemProxies = diffs
|
||||
let items = diffs
|
||||
.reduce(itemProxies) { currentItems, diff in
|
||||
guard let collectionDiff = buildDiff(from: diff, on: currentItems) else {
|
||||
MXLog.error("Failed building CollectionDifference from \(diff)")
|
||||
@@ -73,6 +83,8 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
|
||||
|
||||
return updatedItems
|
||||
}
|
||||
|
||||
itemProxiesSubject.send(items)
|
||||
|
||||
MXLog.verbose("Finished applying diffs, current items (\(itemProxies.count)) : \(itemProxies.map(\.debugIdentifier))")
|
||||
}
|
||||
@@ -186,12 +198,8 @@ private extension VirtualTimelineItem {
|
||||
switch self {
|
||||
case .dayDivider(let timestamp):
|
||||
return "DayDiviver(\(timestamp))"
|
||||
case .loadingIndicator:
|
||||
return "LoadingIndicator"
|
||||
case .readMarker:
|
||||
return "ReadMarker"
|
||||
case .timelineStart:
|
||||
return "TimelineStart"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,17 @@
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
protocol RoomTimelineProviderProtocol {
|
||||
var itemsPublisher: CurrentValuePublisher<[TimelineItemProxy], Never> { get }
|
||||
import MatrixRustSDK
|
||||
|
||||
struct TimelineProviderUpdate {
|
||||
let items: [TimelineItemProxy]
|
||||
let backPaginationState: BackPaginationStatus
|
||||
}
|
||||
|
||||
@MainActor
|
||||
// sourcery: AutoMockable
|
||||
protocol RoomTimelineProviderProtocol {
|
||||
var updatePublisher: AnyPublisher<TimelineProviderUpdate, Never> { get }
|
||||
var itemProxies: [TimelineItemProxy] { get }
|
||||
var backPaginationState: BackPaginationStatus { get }
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
|
||||
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomtimelineprovider", qos: .utility)
|
||||
|
||||
timelineProvider
|
||||
.itemsPublisher
|
||||
.updatePublisher
|
||||
.receive(on: serialDispatchQueue)
|
||||
.sink { [weak self] _ in
|
||||
guard let self else { return }
|
||||
@@ -183,7 +183,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
|
||||
// Handle this parallel to the timeline items so we're not forced
|
||||
// to bundle the Rust side objects within them
|
||||
func debugInfo(for itemID: String) -> TimelineItemDebugInfo {
|
||||
for timelineItemProxy in timelineProvider.itemsPublisher.value {
|
||||
for timelineItemProxy in timelineProvider.itemProxies {
|
||||
switch timelineItemProxy {
|
||||
case .event(let item):
|
||||
if item.id == itemID {
|
||||
@@ -245,7 +245,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
|
||||
var isBackPaginating = false
|
||||
var lastEncryptedHistoryItemIndex: Int?
|
||||
|
||||
let collapsibleChunks = timelineProvider.itemsPublisher.value.groupBy { isItemCollapsible($0) }
|
||||
let collapsibleChunks = timelineProvider.itemProxies.groupBy { isItemCollapsible($0) }
|
||||
|
||||
for (index, collapsibleChunk) in collapsibleChunks.enumerated() {
|
||||
let isLastItem = index == collapsibleChunks.indices.last
|
||||
@@ -294,6 +294,17 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
|
||||
// Remove everything up to the last encrypted history item.
|
||||
// It only contains encrypted messages, state changes and date separators.
|
||||
newTimelineItems.removeFirst(lastEncryptedHistoryItemIndex)
|
||||
} else {
|
||||
// Otherwise check if we need to add anything to the top of the timeline.
|
||||
switch timelineProvider.backPaginationState {
|
||||
case .timelineStartReached:
|
||||
let timelineStart = TimelineStartRoomTimelineItem(name: roomProxy.displayName ?? roomProxy.name)
|
||||
newTimelineItems.insert(timelineStart, at: 0)
|
||||
case .paginating:
|
||||
newTimelineItems.insert(PaginationIndicatorRoomTimelineItem(), at: 0)
|
||||
case .idle:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
timelineItems = newTimelineItems
|
||||
@@ -330,10 +341,6 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
|
||||
return SeparatorRoomTimelineItem(id: identifier, text: dateString)
|
||||
case .readMarker:
|
||||
return ReadMarkerRoomTimelineItem()
|
||||
case .loadingIndicator:
|
||||
return PaginationIndicatorRoomTimelineItem()
|
||||
case .timelineStart:
|
||||
return TimelineStartRoomTimelineItem(name: roomProxy.displayName ?? roomProxy.name)
|
||||
}
|
||||
case .unknown:
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user