Begin PollHistoryService

This commit is contained in:
Alfonso Grillo
2023-01-19 16:22:22 +01:00
parent 4aee4cef82
commit 3f343a028b
3 changed files with 94 additions and 5 deletions
@@ -19,6 +19,7 @@ import SwiftUI
struct PollHistoryCoordinatorParameters {
let mode: PollHistoryMode
let room: MXRoom
}
final class PollHistoryCoordinator: Coordinator, Presentable {
@@ -32,9 +33,7 @@ final class PollHistoryCoordinator: Coordinator, Presentable {
init(parameters: PollHistoryCoordinatorParameters) {
self.parameters = parameters
#warning("replace with the real service after that it's done")
let viewModel = PollHistoryViewModel(mode: parameters.mode, pollService: MockPollHistoryService())
let viewModel = PollHistoryViewModel(mode: parameters.mode, pollService: PollHistoryService(room: parameters.room) )
let view = PollHistory(viewModel: viewModel.context)
pollHistoryViewModel = viewModel
pollHistoryHostingController = VectorHostingController(rootView: view)
@@ -16,9 +16,99 @@
import MatrixSDK
import Foundation
import Combine
final class PollHistoryService: PollHistoryServiceProtocol {
private let room: MXRoom
private let livePolls: PassthroughSubject<TimelinePollDetails, Never> = .init()
private var listner: Any?
private var timeline: MXEventTimeline?
private var pollAggregators: [String: PollAggregator] = [:]
init(room: MXRoom) {
self.room = room
}
func fetchHistory() async throws -> [PollListData] {
[]
guard timeline == nil else {
paginate()
return []
}
room.liveTimeline { [weak self] timeline in
guard
let self = self,
let timeline = timeline
else {
#warning("Handle error")
return
}
self.setup(timeline: timeline)
self.paginate()
}
return []
}
}
private extension PollHistoryService {
enum Constants {
static let pageSize: UInt = 250
}
func setup(timeline: MXEventTimeline) {
self.timeline = timeline
listner = timeline.listenToEvents([MXEventType.pollStart]) { [weak self] event, direction, roomState in
self?.aggregatePoll(pollStartEvent: event)
}
}
func paginate() {
guard let timeline = timeline else {
return
}
timeline.paginate(Constants.pageSize,
direction: .backwards,
onlyFromStore: false) { response in
switch response {
case .success:
break
case .failure(let error):
#warning("Handle error")
break
}
}
}
func aggregatePoll(pollStartEvent: MXEvent) {
guard pollAggregators[pollStartEvent.eventId] == nil else {
return
}
guard let aggregator = try? PollAggregator(session: room.mxSession, room: room, pollEvent: pollStartEvent, delegate: self) else {
return
}
pollAggregators[pollStartEvent.eventId] = aggregator
}
}
// MARK: - PollAggregatorDelegate
extension PollHistoryService: PollAggregatorDelegate {
func pollAggregatorDidStartLoading(_ aggregator: PollAggregator) {
}
func pollAggregatorDidEndLoading(_ aggregator: PollAggregator) {
}
func pollAggregator(_ aggregator: PollAggregator, didFailWithError: Error) {
}
func pollAggregatorDidUpdateData(_ aggregator: PollAggregator) {
}
}