From 478cffa5b84a01132e78071fa2389fb7c9bcda76 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Wed, 22 May 2024 13:45:22 +0200 Subject: [PATCH] Fix voted indicator on poll missing (IOS-274) --- Mastodon/Diffable/Status/StatusSection.swift | 1 - MastodonSDK/Sources/MastodonSDK/MastodonPoll.swift | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Mastodon/Diffable/Status/StatusSection.swift b/Mastodon/Diffable/Status/StatusSection.swift index 1ad019c7f..ceb44b2ce 100644 --- a/Mastodon/Diffable/Status/StatusSection.swift +++ b/Mastodon/Diffable/Status/StatusSection.swift @@ -144,7 +144,6 @@ extension StatusSection { authContext: AuthContext, statusView: StatusView ) { - let managedObjectContext = context.managedObjectContext statusView.pollTableViewDiffableDataSource = UITableViewDiffableDataSource(tableView: statusView.pollTableView) { tableView, indexPath, item in switch item { case .history: diff --git a/MastodonSDK/Sources/MastodonSDK/MastodonPoll.swift b/MastodonSDK/Sources/MastodonSDK/MastodonPoll.swift index d2d59a909..20f0542ce 100644 --- a/MastodonSDK/Sources/MastodonSDK/MastodonPoll.swift +++ b/MastodonSDK/Sources/MastodonSDK/MastodonPoll.swift @@ -56,14 +56,17 @@ public final class MastodonPollOption: ObservableObject, Hashable { @Published public var voted: Bool? public private(set) var optionIndex: Int? = nil - public init(poll: MastodonPoll, option: Mastodon.Entity.Poll.Option, isSelected: Bool = false) { + public init(poll: MastodonPoll, option: Mastodon.Entity.Poll.Option) { self.poll = poll self.option = option - self.isSelected = isSelected self.votesCount = option.votesCount self.title = option.title self.optionIndex = poll.options.firstIndex(of: self) - + self.isSelected = { + guard let ownVotes = poll.entity.ownVotes else { return false } + guard let index = poll.entity.options.firstIndex(of: option) else { return false } + return ownVotes.contains(index) + }() self.voted = { guard let ownVotes = poll.entity.ownVotes else { return false } guard let optionIndex else { return false }