diff --git a/Tusker/Views/Poll/StatusPollView.swift b/Tusker/Views/Poll/StatusPollView.swift index dd5d3bc2a3..99b87424f7 100644 --- a/Tusker/Views/Poll/StatusPollView.swift +++ b/Tusker/Views/Poll/StatusPollView.swift @@ -23,7 +23,7 @@ class StatusPollView: UIView { weak var mastodonController: MastodonController! private var statusID: String! - private var poll: Poll! + private(set) var poll: Poll? private var optionsView: PollOptionsView! private var voteButton: UIButton! @@ -76,12 +76,15 @@ class StatusPollView: UIView { ]) } - func updateUI(status: StatusMO, poll: Poll) { + func updateUI(status: StatusMO, poll: Poll?) { self.statusID = status.id self.poll = poll + // remove old options options.forEach { $0.removeFromSuperview() } + guard let poll = poll else { return } + // poll.voted is nil if there is no user (e.g., public timeline), in which case the poll also cannot be voted upon if (poll.voted ?? true) || poll.expired || status.account.id == mastodonController.account.id { canVote = false @@ -138,7 +141,7 @@ class StatusPollView: UIView { UIImpactFeedbackGenerator(style: .medium).impactOccurred() - let request = Poll.vote(poll.id, choices: optionsView.checkedOptionIndices) + let request = Poll.vote(poll!.id, choices: optionsView.checkedOptionIndices) mastodonController.run(request) { (response) in switch response { case let .failure(error): diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index ba84e78631..70550d0960 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -215,13 +215,9 @@ class BaseStatusTableViewCell: UITableViewCell, MenuPreviewProvider { // keep menu in sync with changed states e.g. bookmarked, muted moreButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: actionsForStatus(status, sourceView: moreButton)) - if let poll = status.poll { - pollView.isHidden = false - pollView.mastodonController = mastodonController - pollView.updateUI(status: status, poll: poll) - } else { - pollView.isHidden = true - } + pollView.isHidden = status.poll == nil + pollView.mastodonController = mastodonController + pollView.updateUI(status: status, poll: status.poll) } func updateUI(account: AccountMO) { @@ -359,6 +355,7 @@ class BaseStatusTableViewCell: UITableViewCell, MenuPreviewProvider { contentTextView.isHidden = collapsed cardView.isHidden = cardView.card == nil || collapsed attachmentsView.isHidden = attachmentsView.attachments.count == 0 || collapsed + pollView.isHidden = pollView.poll == nil || collapsed let buttonImage = UIImage(systemName: collapsed ? "chevron.down" : "chevron.up")!