Skip to content

Commit c6eca07

Browse files
committed
Reuse logic from LLC
1 parent 11e4303 commit c6eca07

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/Polls/PollAttachmentView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public struct PollAttachmentView<Factory: ViewFactory>: View {
5555
PollOptionView(
5656
viewModel: viewModel,
5757
option: option,
58-
optionVotes: poll.voteCountsByOption?[option.id],
59-
maxVotes: poll.voteCountsByOption?.values.max()
58+
optionVotes: poll.voteCount(for: option),
59+
maxVotes: poll.currentMaximumVoteCount
6060
)
6161
.layoutPriority(1) // do not compress long text
6262
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/Polls/PollAttachmentViewModel.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,7 @@ public class PollAttachmentViewModel: ObservableObject, PollControllerDelegate {
219219
///
220220
/// - Note: When multiple options have the highest vote count, this function returns false.
221221
public func hasMostVotes(for option: PollOption) -> Bool {
222-
guard let allCounts = poll.voteCountsByOption else { return false }
223-
guard let optionVoteCount = allCounts[option.id], optionVoteCount > 0 else { return false }
224-
guard let highestVotePerOption = allCounts.values.max() else { return false }
225-
guard optionVoteCount == highestVotePerOption else { return false }
226-
// Check if only one option has highest number for votes
227-
let optionsByVoteCounts = Dictionary(grouping: allCounts, by: { $0.value })
228-
return optionsByVoteCounts[optionVoteCount]?.count == 1
222+
return poll.isOptionWithMostVotes(option)
229223
}
230224

231225
// MARK: - PollControllerDelegate
@@ -244,7 +238,7 @@ public class PollAttachmentViewModel: ObservableObject, PollControllerDelegate {
244238
// MARK: - private
245239

246240
private func currentUserVote(for option: PollOption) -> PollVote? {
247-
currentUserVotes.first(where: { $0.optionId == option.id })
241+
poll.currentUserVote(for: option)
248242
}
249243

250244
private func notifySheetPresentation(shown: Bool) {

StreamChatSwiftUITests/Tests/ChatChannel/PollAttachmentViewModel_Tests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ final class PollAttachmentViewModel_Tests: StreamChatTestCase {
5858

5959
func test_pollAttachmentViewModel_removeVoteCalled() {
6060
// Given
61+
let vote = makePollVote()
6162
let pollController = makePollController()
6263
let viewModel = PollAttachmentViewModel(
6364
message: .mock(),
64-
poll: .unique,
65+
poll: .mock(ownVotes: [vote]),
6566
pollController: pollController
6667
)
67-
let vote = makePollVote()
68-
viewModel.currentUserVotes = [vote]
6968

7069
// When
7170
viewModel.removePollVote(for: .init(id: vote.optionId!, text: ""))

0 commit comments

Comments
 (0)