Enable fuzzy search behind a feature flag. (#1490)

This commit is contained in:
Doug
2023-08-14 17:41:41 +01:00
committed by GitHub
parent b58f76b170
commit b2928a0dc6
11 changed files with 62 additions and 17 deletions
@@ -45,6 +45,8 @@ class MockRoomSummaryProvider: RoomSummaryProviderProtocol {
func setRoomList(_ roomList: RoomList) { }
func updateVisibleRange(_ range: Range<Int>) { }
func updateFilterPattern(_ pattern: String?) { }
}
extension Array where Element == RoomSummary {
@@ -22,12 +22,14 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
private let roomListService: RoomListServiceProtocol
private let eventStringBuilder: RoomEventStringBuilder
private let name: String
private var appSettings: AppSettings
private let serialDispatchQueue: DispatchQueue
private var roomList: RoomListProtocol?
private var cancellables = Set<AnyCancellable>()
private var listUpdatesSubscriptionResult: RoomListEntriesWithDynamicFilterResult?
private var listUpdatesTaskHandle: TaskHandle?
private var stateUpdatesTaskHandle: TaskHandle?
@@ -53,11 +55,13 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
init(roomListService: RoomListServiceProtocol,
eventStringBuilder: RoomEventStringBuilder,
name: String) {
name: String,
appSettings: AppSettings) {
self.roomListService = roomListService
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomsummaryprovider", qos: .utility)
self.eventStringBuilder = eventStringBuilder
self.name = name
self.appSettings = appSettings
diffsPublisher
.receive(on: serialDispatchQueue)
@@ -73,21 +77,17 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
self.roomList = roomList
do {
let listUpdatesSubscriptionResult = roomList.entries(listener: RoomListEntriesListenerProxy { [weak self] updates in
listUpdatesSubscriptionResult = roomList.entriesWithDynamicFilter(listener: RoomListEntriesListenerProxy { [weak self] updates in
guard let self else { return }
MXLog.info("\(name): Received list update")
diffsPublisher.send(updates)
})
listUpdatesTaskHandle = listUpdatesSubscriptionResult.entriesStream
rooms = listUpdatesSubscriptionResult.entries.map { roomListEntry in
buildSummaryForRoomListEntry(roomListEntry)
}
// Manually call it here as the didSet doesn't work from constructors
roomListSubject.send(rooms)
listUpdatesTaskHandle = listUpdatesSubscriptionResult?.entriesStream
// Forces the listener above to be called with the current state
updateFilterPattern(nil)
let stateUpdatesSubscriptionResult = try roomList.loadingState(listener: RoomListStateObserver { [weak self] state in
guard let self else { return }
MXLog.info("\(name): Received state update: \(state)")
@@ -113,6 +113,17 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
}
}
func updateFilterPattern(_ pattern: String?) {
guard let pattern, !pattern.isEmpty else {
_ = listUpdatesSubscriptionResult?.dynamicFilter.set(kind: .all)
return
}
guard appSettings.fuzzySearchEnabled else { return }
_ = listUpdatesSubscriptionResult?.dynamicFilter.set(kind: .fuzzyMatchRoomName(pattern: pattern.lowercased()))
}
// MARK: - Private
fileprivate func updateRoomsWithDiffs(_ diffs: [RoomListEntriesUpdate]) {
@@ -99,4 +99,6 @@ protocol RoomSummaryProviderProtocol {
func setRoomList(_ roomList: RoomList)
func updateVisibleRange(_ range: Range<Int>)
func updateFilterPattern(_ pattern: String?)
}