Inject TimelinePollDetails in PollListItem

This commit is contained in:
Alfonso Grillo
2023-01-19 17:10:14 +01:00
parent f78c945507
commit c56d1f3bfe
6 changed files with 131 additions and 83 deletions
@@ -20,20 +20,29 @@ import Combine
final class PollHistoryService: PollHistoryServiceProtocol {
private let room: MXRoom
private let livePolls: PassthroughSubject<TimelinePollDetails, Never> = .init()
private let pollsSubject: PassthroughSubject<TimelinePollDetails, Never> = .init()
private let errorSubject: PassthroughSubject<Error, Never> = .init()
private var listner: Any?
private var timeline: MXEventTimeline?
private var pollAggregators: [String: PollAggregator] = [:]
var pollHistory: AnyPublisher<TimelinePollDetails, Never> {
pollsSubject.eraseToAnyPublisher()
}
var error: AnyPublisher<Error, Never> {
errorSubject.eraseToAnyPublisher()
}
init(room: MXRoom) {
self.room = room
}
func fetchHistory() async throws -> [PollListData] {
func startFetching() {
guard timeline == nil else {
paginate()
return []
return
}
room.liveTimeline { [weak self] timeline in
@@ -48,8 +57,6 @@ final class PollHistoryService: PollHistoryServiceProtocol {
self.setup(timeline: timeline)
self.paginate()
}
return []
}
}
@@ -75,6 +82,7 @@ private extension PollHistoryService {
onlyFromStore: false) { response in
switch response {
case .success:
#warning("Go on with pagination...")
break
case .failure(let error):
#warning("Handle error")
@@ -103,6 +111,7 @@ extension PollHistoryService: PollAggregatorDelegate {
}
func pollAggregatorDidEndLoading(_ aggregator: PollAggregator) {
pollsSubject.send(.init(poll: aggregator.poll, represent: .started))
}
func pollAggregator(_ aggregator: PollAggregator, didFailWithError: Error) {
@@ -14,31 +14,48 @@
// limitations under the License.
//
import Combine
final class MockPollHistoryService: PollHistoryServiceProtocol {
var activePollsData: [PollListData] = (1..<10)
private let polls: PassthroughSubject<TimelinePollDetails, Never> = .init()
var pollHistory: AnyPublisher<TimelinePollDetails, Never> {
polls.eraseToAnyPublisher()
}
var error: AnyPublisher<Error, Never> {
Empty().eraseToAnyPublisher()
}
func startFetching() {
for poll in activePollsData + pastPollsData {
polls.send(poll)
}
}
var activePollsData: [TimelinePollDetails] = (1..<10)
.map { index in
PollListData(
startDate: .init().addingTimeInterval(-CGFloat(index) * 3600),
question: "Do you like the active poll number \(index)?",
numberOfVotes: 30,
winningOption: nil
)
TimelinePollDetails(question: "Do you like the active poll number \(index)?",
answerOptions: [],
closed: false,
totalAnswerCount: 30,
type: .disclosed,
eventType: .started,
maxAllowedSelections: 1,
hasBeenEdited: false,
hasDecryptionError: false)
}
var pastPollsData: [PollListData] = (1..<10)
var pastPollsData: [TimelinePollDetails] = (1..<10)
.map { index in
PollListData(
startDate: .init().addingTimeInterval(-CGFloat(index) * 3600),
question: "Do you like the past poll number \(index)?",
numberOfVotes: 30,
winningOption: .init(id: "id", text: "Yes, of course!", count: 20, winner: true, selected: true)
)
TimelinePollDetails(question: "Do you like the active poll number \(index)?",
answerOptions: [.init(id: "id", text: "Yes, of course!", count: 20, winner: true, selected: true)],
closed: true,
totalAnswerCount: 30,
type: .disclosed,
eventType: .started,
maxAllowedSelections: 1,
hasBeenEdited: false,
hasDecryptionError: false)
}
func fetchHistory() async throws -> [PollListData] {
(activePollsData + pastPollsData)
.sorted { poll1, poll2 in
poll1.startDate > poll2.startDate
}
}
}
@@ -14,6 +14,11 @@
// limitations under the License.
//
import Combine
protocol PollHistoryServiceProtocol {
func fetchHistory() async throws -> [PollListData]
var pollHistory: AnyPublisher<TimelinePollDetails, Never> { get }
var error: AnyPublisher<Error, Never> { get }
func startFetching()
}