Implement pagination in recents list service

This commit is contained in:
ismailgulek
2022-01-04 04:30:12 +03:00
parent 22a41a1f89
commit 4c69041e91
5 changed files with 102 additions and 3 deletions
@@ -223,6 +223,21 @@ public class RecentsListService: NSObject, RecentsListServiceProtocol {
return visibleFetchers.reduce(0, { $0 + ($1.data?.counts.numberOfRooms ?? 0) })
}
public func paginate(inSection section: RecentsListServiceSection) {
guard let fetcher = fetcher(forSection: section) else {
return
}
guard let data = fetcher.data else {
// first page is not fetched yet
return
}
guard data.paginationOptions != .none else {
// pagination is not enabled
return
}
fetcher.paginate()
}
public func updateMode(_ mode: RecentsDataSourceMode) {
self.mode = mode
if let fetcher = favoritedRoomListDataFetcher {
@@ -341,6 +356,44 @@ public class RecentsListService: NSObject, RecentsListServiceProtocol {
return fetcherTypesForMode[mode]?.contains(.suggested) ?? false
}
private func fetcher(forSection section: RecentsListServiceSection) -> MXRoomListDataFetcher? {
switch section {
case .invited:
return invitedRoomListDataFetcher
case .favorited:
return favoritedRoomListDataFetcher
case .people:
return directRoomListDataFetcher
case .conversation:
return conversationRoomListDataFetcher
case .lowPriority:
return lowPriorityRoomListDataFetcher
case .serverNotice:
return serverNoticeRoomListDataFetcher
case .suggested:
return suggestedRoomListDataFetcher
}
}
private func section(forFetcher fetcher: MXRoomListDataFetcher) -> RecentsListServiceSection? {
if fetcher === invitedRoomListDataFetcher {
return .invited
} else if fetcher === favoritedRoomListDataFetcher {
return .favorited
} else if fetcher === directRoomListDataFetcher {
return .people
} else if fetcher === conversationRoomListDataFetcher {
return .conversation
} else if fetcher === lowPriorityRoomListDataFetcher {
return .lowPriority
} else if fetcher === serverNoticeRoomListDataFetcher {
return .serverNotice
} else if fetcher === suggestedRoomListDataFetcher {
return .suggested
}
return nil
}
private func createCommonRoomListDataFetcher(withDataTypes dataTypes: MXRoomSummaryDataTypes = [],
onlySuggested: Bool = false,
paginate: Bool = true) -> MXRoomListDataFetcher {
@@ -454,7 +507,10 @@ public class RecentsListService: NSObject, RecentsListServiceProtocol {
}
private func notifyDataChange(on fetcher: MXRoomListDataFetcher) {
multicastDelegate.invoke({ $0.serviceDidChangeData(self) })
if let section = section(forFetcher: fetcher) {
multicastDelegate.invoke { $0.recentsListServiceDidChangeData?(self, forSection: section) }
}
multicastDelegate.invoke { $0.recentsListServiceDidChangeData?(self) }
}
deinit {
@@ -170,6 +170,10 @@ public class MockRecentsListService: NSObject, RecentsListServiceProtocol {
}
}
public func paginate(inSection section: RecentsListServiceSection) {
// no-op
}
public func updateMode(_ mode: RecentsDataSourceMode) {
self.mode = mode
notifyDataChange()
@@ -210,7 +214,7 @@ public class MockRecentsListService: NSObject, RecentsListServiceProtocol {
}
private func notifyDataChange() {
multicastDelegate.invoke({ $0.serviceDidChangeData(self) })
multicastDelegate.invoke({ $0.recentsListServiceDidChangeData?(self) })
}
}
@@ -21,5 +21,11 @@ public protocol RecentsListServiceDelegate: AnyObject {
/// Delegate method to be called when service data updated
/// - Parameter service: service object
func serviceDidChangeData(_ service: RecentsListServiceProtocol)
@objc optional func recentsListServiceDidChangeData(_ service: RecentsListServiceProtocol)
/// Delegate method to be called when a specific section data updated. Called for each updated section before `recentsListServiceDidChangeData` if implemented.
/// - Parameter service: service object
/// - Parameter section: updated section
@objc optional func recentsListServiceDidChangeData(_ service: RecentsListServiceProtocol,
forSection section: RecentsListServiceSection)
}
@@ -87,6 +87,11 @@ public protocol RecentsListServiceProtocol {
/// Stop service. Do not use after stopping.
func stop()
// MARK: Pagination
/// Paginate in the given section.
func paginate(inSection section: RecentsListServiceSection)
// MARK: - Delegate
/// Add delegate instance for the service
@@ -0,0 +1,28 @@
//
// Copyright 2021 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 Foundation
@objc
public enum RecentsListServiceSection: Int {
case invited
case favorited
case people
case conversation
case lowPriority
case serverNotice
case suggested
}