Set visible ranges on the home screen sliding sync view (#342)

* Set visible ranges on the home screen sliding sync view
* Prevent the visible items from being updated while in search mode
* Enable diffs on invalidations as they seem to fix duplicated events in the home screen room list
* Promoted some diffing logs to info from verbose
This commit is contained in:
Stefan Ceriu
2022-11-25 17:50:43 +02:00
committed by GitHub
parent 1781f92882
commit 00cd2ab707
14 changed files with 271 additions and 88 deletions
@@ -89,7 +89,7 @@ class ClientProxy: ClientProxyProtocol {
RequiredState(key: "m.room.encryption", value: "")])
.name(name: "HomeScreenView")
.syncMode(mode: .selective)
.addRange(from: 0, to: 50) // FIXME: Replace this with a dynamic solution
.addRange(from: 0, to: 20)
.build()
let slidingSync = try slidingSyncBuilder
@@ -98,8 +98,9 @@ class ClientProxy: ClientProxyProtocol {
.coldCache(name: "ElementX")
.build()
self.roomSummaryProvider = RoomSummaryProvider(slidingSyncController: slidingSync,
slidingSyncView: slidingSyncView,
let slidingSyncViewProxy = SlidingSyncViewProxy(clientProxy: self, slidingSync: slidingSync, slidingSyncView: slidingSyncView)
self.roomSummaryProvider = RoomSummaryProvider(slidingSyncViewProxy: slidingSyncViewProxy,
roomMessageFactory: RoomMessageFactory())
self.slidingSync = slidingSync
@@ -162,6 +163,11 @@ class ClientProxy: ClientProxyProtocol {
slidingSync?.setObserver(observer: nil)
}
func restartSync() {
slidingSyncObserverToken?.cancel()
slidingSyncObserverToken = slidingSync?.sync()
}
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol? {
let (slidingSyncRoom, room) = await Task.dispatch(on: clientQueue) {
self.roomTupleForIdentifier(identifier)
@@ -58,7 +58,7 @@ enum PushFormat {
// }
}
protocol ClientProxyProtocol: MediaProxyProtocol {
protocol ClientProxyProtocol: AnyObject, MediaProxyProtocol {
var callbacks: PassthroughSubject<ClientProxyCallback, Never> { get }
var userIdentifier: String { get }
@@ -77,6 +77,8 @@ protocol ClientProxyProtocol: MediaProxyProtocol {
func stopSync()
func restartSync()
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol?
func loadUserDisplayName() async -> Result<String, ClientProxyError>
@@ -18,7 +18,7 @@ import Combine
import Foundation
import MatrixRustSDK
struct MockClientProxy: ClientProxyProtocol {
class MockClientProxy: ClientProxyProtocol {
let callbacks = PassthroughSubject<ClientProxyCallback, Never>()
let userIdentifier: String
@@ -29,10 +29,17 @@ struct MockClientProxy: ClientProxyProtocol {
var roomSummaryProvider: RoomSummaryProviderProtocol? = MockRoomSummaryProvider()
internal init(userIdentifier: String, roomSummaryProvider: RoomSummaryProviderProtocol? = MockRoomSummaryProvider()) {
self.userIdentifier = userIdentifier
self.roomSummaryProvider = roomSummaryProvider
}
func startSync() { }
func stopSync() { }
func restartSync() { }
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol? {
nil
}
@@ -0,0 +1,113 @@
//
// 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 Foundation
import MatrixRustSDK
private class SlidingSyncViewObserver: SlidingSyncViewRoomListObserver, SlidingSyncViewStateObserver, SlidingSyncViewRoomsCountObserver {
/// Publishes room list diffs as they come in through sliding sync
let roomListDiffPublisher = PassthroughSubject<SlidingSyncViewRoomsListDiff, Never>()
/// Publishes the current state of sliding sync, such as whether its catching up or live.
let stateUpdatePublisher = CurrentValueSubject<SlidingSyncState, Never>(.cold)
/// Publishes the number of available rooms
let countUpdatePublisher = CurrentValueSubject<UInt, Never>(0)
// MARK: - SlidingSyncViewRoomListObserver
func didReceiveUpdate(diff: SlidingSyncViewRoomsListDiff) {
MXLog.verbose("Received room diff")
roomListDiffPublisher.send(diff)
}
// MARK: - SlidingSyncViewStateObserver
func didReceiveUpdate(newState: SlidingSyncState) {
MXLog.verbose("Updated state: \(newState)")
stateUpdatePublisher.send(newState)
}
// MARK: - SlidingSyncViewRoomsCountObserver
func didReceiveUpdate(count: UInt32) {
MXLog.verbose("Updated room count: \(count)")
countUpdatePublisher.send(UInt(count))
}
}
class SlidingSyncViewProxy {
private weak var clientProxy: ClientProxyProtocol?
private let slidingSync: SlidingSyncProtocol
private let slidingSyncView: SlidingSyncViewProtocol
private var listUpdateObserverToken: StoppableSpawn?
private var stateUpdateObserverToken: StoppableSpawn?
private var countUpdateObserverToken: StoppableSpawn?
private var cancellables = Set<AnyCancellable>()
let diffPublisher = PassthroughSubject<SlidingSyncViewRoomsListDiff, Never>()
let statePublisher = PassthroughSubject<SlidingSyncState, Never>()
let countPublisher = PassthroughSubject<UInt, Never>()
deinit {
listUpdateObserverToken?.cancel()
stateUpdateObserverToken?.cancel()
countUpdateObserverToken?.cancel()
}
init(clientProxy: ClientProxyProtocol, slidingSync: SlidingSyncProtocol, slidingSyncView: SlidingSyncViewProtocol) {
self.clientProxy = clientProxy
self.slidingSync = slidingSync
self.slidingSyncView = slidingSyncView
let slidingSyncViewObserver = SlidingSyncViewObserver()
slidingSyncViewObserver.stateUpdatePublisher
.subscribe(statePublisher)
.store(in: &cancellables)
slidingSyncViewObserver.countUpdatePublisher
.subscribe(countPublisher)
.store(in: &cancellables)
slidingSyncViewObserver.roomListDiffPublisher
.subscribe(diffPublisher)
.store(in: &cancellables)
listUpdateObserverToken = slidingSyncView.observeRoomList(observer: slidingSyncViewObserver)
stateUpdateObserverToken = slidingSyncView.observeState(observer: slidingSyncViewObserver)
countUpdateObserverToken = slidingSyncView.observeRoomsCount(observer: slidingSyncViewObserver)
}
func currentRoomsList() -> [RoomListEntry] {
slidingSyncView.currentRoomsList()
}
func roomForIdentifier(_ identifier: String) throws -> SlidingSyncRoomProtocol? {
try slidingSync.getRoom(roomId: identifier)
}
func updateVisibleRange(_ range: ClosedRange<Int>) {
MXLog.info("Setting sliding sync view range to \(range)")
slidingSyncView.setRange(start: UInt32(range.lowerBound), end: UInt32(range.upperBound))
clientProxy?.restartSync()
}
}
@@ -27,8 +27,6 @@ class MockRoomSummaryProvider: RoomSummaryProviderProtocol {
let statePublisher: CurrentValueSubject<RoomSummaryProviderState, Never>
let countPublisher: CurrentValueSubject<UInt, Never>
func updateRoomsWithIdentifiers(_ identifiers: [String]) { }
convenience init() {
self.init(state: .loading)
}
@@ -46,6 +44,10 @@ class MockRoomSummaryProvider: RoomSummaryProviderProtocol {
}
}
func updateRoomsWithIdentifiers(_ identifiers: [String]) { }
func updateVisibleRange(_ range: ClosedRange<Int>) { }
// MARK: - Private
static let rooms: [RoomSummary] = [
@@ -18,48 +18,11 @@ import Combine
import Foundation
import MatrixRustSDK
private class WeakRoomSummaryProviderWrapper: SlidingSyncViewRoomListObserver, SlidingSyncViewStateObserver, SlidingSyncViewRoomsCountObserver {
/// Publishes room list diffs as they come in through sliding sync
let roomListDiffPublisher = PassthroughSubject<SlidingSyncViewRoomsListDiff, Never>()
/// Publishes the current state of sliding sync, such as whether its catching up or live.
let stateUpdatePublisher = CurrentValueSubject<SlidingSyncState, Never>(.cold)
/// Publishes the number of available rooms
let countUpdatePublisher = CurrentValueSubject<UInt, Never>(0)
// MARK: - SlidingSyncViewRoomListObserver
func didReceiveUpdate(diff: SlidingSyncViewRoomsListDiff) {
MXLog.verbose("Received room diff")
roomListDiffPublisher.send(diff)
}
// MARK: - SlidingSyncViewStateObserver
func didReceiveUpdate(newState: SlidingSyncState) {
MXLog.verbose("Updated state: \(newState)")
stateUpdatePublisher.send(newState)
}
// MARK: - SlidingSyncViewRoomsCountObserver
func didReceiveUpdate(count: UInt32) {
MXLog.verbose("Updated room count: \(count)")
countUpdatePublisher.send(UInt(count))
}
}
class RoomSummaryProvider: RoomSummaryProviderProtocol {
private let slidingSyncController: SlidingSyncProtocol
private let slidingSyncView: SlidingSyncViewProtocol
private let slidingSyncViewProxy: SlidingSyncViewProxy
private let roomMessageFactory: RoomMessageFactoryProtocol
private let serialDispatchQueue: DispatchQueue
private var listUpdateObserverToken: StoppableSpawn?
private var stateUpdateObserverToken: StoppableSpawn?
private var countUpdateObserverToken: StoppableSpawn?
private var cancellables = Set<AnyCancellable>()
let roomListPublisher = CurrentValueSubject<[RoomSummary], Never>([])
@@ -72,47 +35,34 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
}
}
deinit {
listUpdateObserverToken?.cancel()
stateUpdateObserverToken?.cancel()
countUpdateObserverToken?.cancel()
}
init(slidingSyncController: SlidingSyncProtocol, slidingSyncView: SlidingSyncViewProtocol, roomMessageFactory: RoomMessageFactoryProtocol) {
self.slidingSyncView = slidingSyncView
self.slidingSyncController = slidingSyncController
init(slidingSyncViewProxy: SlidingSyncViewProxy, roomMessageFactory: RoomMessageFactoryProtocol) {
self.slidingSyncViewProxy = slidingSyncViewProxy
self.roomMessageFactory = roomMessageFactory
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomsummaryprovider")
let weakProvider = WeakRoomSummaryProviderWrapper()
rooms = slidingSyncView.currentRoomsList().map { roomListEntry in
rooms = slidingSyncViewProxy.currentRoomsList().map { roomListEntry in
buildSummaryForRoomListEntry(roomListEntry)
}
roomListPublisher.send(rooms) // didSet not called from initialisers
weakProvider.stateUpdatePublisher
slidingSyncViewProxy.statePublisher
.map(RoomSummaryProviderState.init)
.subscribe(statePublisher)
.store(in: &cancellables)
weakProvider.countUpdatePublisher
slidingSyncViewProxy.countPublisher
.subscribe(countPublisher)
.store(in: &cancellables)
weakProvider.roomListDiffPublisher
slidingSyncViewProxy.diffPublisher
.collect(.byTime(serialDispatchQueue, 0.25))
.sink { self.updateRoomsWithDiffs($0) }
.store(in: &cancellables)
listUpdateObserverToken = slidingSyncView.observeRoomList(observer: weakProvider)
stateUpdateObserverToken = slidingSyncView.observeState(observer: weakProvider)
countUpdateObserverToken = slidingSyncView.observeRoomsCount(observer: weakProvider)
}
func updateRoomsWithIdentifiers(_ identifiers: [String]) {
#warning("This is a valid check but Rust doesn't set it correct for selective ranged syncs")
#warning("This is a valid check but Rust doesn't set it correctly for selective ranged syncs")
// guard statePublisher.value == .live else {
// return
// }
@@ -143,18 +93,17 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
rooms = newSummaries
}
func updateVisibleRange(_ range: ClosedRange<Int>) {
slidingSyncViewProxy.updateVisibleRange(range)
}
// MARK: - Private
fileprivate func updateRoomsWithDiffs(_ diffs: [SlidingSyncViewRoomsListDiff]) {
MXLog.verbose("Received diffs")
MXLog.info("Received diffs")
rooms = diffs
.reduce(rooms) { currentItems, diff in
// Invalidations are a no-op for the moment
if diff.isInvalidation {
return currentItems
}
guard let collectionDiff = buildDiff(from: diff, on: currentItems) else {
MXLog.error("Failed building CollectionDifference from \(diff)")
return currentItems
@@ -165,12 +114,12 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
return currentItems
}
MXLog.verbose("Applied diff \(collectionDiff), new count: \(updatedItems.count)")
MXLog.verbose("Applied diff, new count: \(updatedItems.count)")
return updatedItems
}
MXLog.verbose("Finished applying diffs")
MXLog.info("Finished applying diffs")
}
private func buildEmptyRoomSummary(forIdentifier identifier: String = UUID().uuidString) -> RoomSummary {
@@ -178,7 +127,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
}
private func buildRoomSummaryForIdentifier(_ identifier: String) -> RoomSummary {
guard let room = try? slidingSyncController.getRoom(roomId: identifier) else {
guard let room = try? slidingSyncViewProxy.roomForIdentifier(identifier) else {
MXLog.error("Failed finding room with id: \(identifier)")
return buildEmptyRoomSummary(forIdentifier: identifier)
}
@@ -60,4 +60,6 @@ protocol RoomSummaryProviderProtocol {
/// without necessarily changing their position in the list
/// - Parameter identifiers: the identifiers for the rooms that have changed
func updateRoomsWithIdentifiers(_ identifiers: [String])
func updateVisibleRange(_ range: ClosedRange<Int>)
}
@@ -131,7 +131,7 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
// MARK: - Private
private func updateItemsWithDiffs(_ diffs: [TimelineDiff]) {
MXLog.verbose("Received timeline diffs")
MXLog.info("Received timeline diffs")
itemProxies = diffs
.reduce(itemProxies) { currentItems, diff in
@@ -150,7 +150,7 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
return updatedItems
}
MXLog.verbose("Finished applying diffs")
MXLog.info("Finished applying diffs")
}
// swiftlint:disable:next cyclomatic_complexity function_body_length