From d79666679a5cd933a5a62eaa267a4b8f7434ea4f Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 5 Mar 2021 14:23:26 +0800 Subject: [PATCH] fix: poll option UI reuse issue --- Mastodon/Diffiable/Section/PollSection.swift | 3 +++ Mastodon/Diffiable/Section/StatusSection.swift | 12 ++++++++++-- .../View/TableviewCell/StatusTableViewCell.swift | 12 ++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Mastodon/Diffiable/Section/PollSection.swift b/Mastodon/Diffiable/Section/PollSection.swift index d7a7b43f1..45da63bde 100644 --- a/Mastodon/Diffiable/Section/PollSection.swift +++ b/Mastodon/Diffiable/Section/PollSection.swift @@ -73,9 +73,12 @@ extension PollSection { switch state { case .hidden: cell.optionPercentageLabel.isHidden = true + cell.voteProgressStripView.isHidden = true + cell.voteProgressStripView.setProgress(0.0, animated: false) case .reveal(let voted, let percentage, let animated): cell.optionPercentageLabel.isHidden = false cell.optionPercentageLabel.text = String(Int(100 * percentage)) + "%" + cell.voteProgressStripView.isHidden = false cell.voteProgressStripView.tintColor = voted ? Asset.Colors.Background.Poll.highlight.color : Asset.Colors.Background.Poll.disabled.color cell.voteProgressStripView.setProgress(CGFloat(percentage), animated: animated) } diff --git a/Mastodon/Diffiable/Section/StatusSection.swift b/Mastodon/Diffiable/Section/StatusSection.swift index 3c15996d6..4863f4756 100644 --- a/Mastodon/Diffiable/Section/StatusSection.swift +++ b/Mastodon/Diffiable/Section/StatusSection.swift @@ -297,7 +297,7 @@ extension StatusSection { .map { option -> PollItem in let attribute: PollItem.Attribute = { let selectState: PollItem.Attribute.SelectState = { - // make isPollVoted check later to make only local change possible + // make isPollVoted check later to make the local change possible if !votedOptions.isEmpty { return votedOptions.contains(option) ? .on : .off } else if poll.expired { @@ -309,7 +309,15 @@ extension StatusSection { } }() let voteState: PollItem.Attribute.VoteState = { - guard isPollVoted else { return .hidden } + var needsReveal: Bool + if poll.expired { + needsReveal = true + } else if isPollVoted { + needsReveal = true + } else { + needsReveal = false + } + guard needsReveal else { return .hidden } let percentage: Double = { guard poll.votesCount.intValue > 0 else { return 0.0 } return Double(option.votesCount?.intValue ?? 0) / Double(poll.votesCount.intValue) diff --git a/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift b/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift index e8500e05f..64ffb167b 100644 --- a/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift +++ b/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift @@ -103,12 +103,16 @@ extension StatusTableViewCell { extension StatusTableViewCell: UITableViewDelegate { func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { if tableView === statusView.pollTableView, let diffableDataSource = statusView.pollTableViewDataSource { + var pollID: String? + defer { + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: indexPath: %s. PollID: %s", ((#file as NSString).lastPathComponent), #line, #function, indexPath.debugDescription, pollID ?? "") + } guard let item = diffableDataSource.itemIdentifier(for: indexPath), case let .opion(objectID, _) = item, let option = delegate?.managedObjectContext.object(with: objectID) as? PollOption else { return false } - + pollID = option.poll.id return !option.poll.expired } else { return true @@ -117,7 +121,10 @@ extension StatusTableViewCell: UITableViewDelegate { func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if tableView === statusView.pollTableView, let diffableDataSource = statusView.pollTableViewDataSource { - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: indexPath", ((#file as NSString).lastPathComponent), #line, #function, indexPath.debugDescription) + var pollID: String? + defer { + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: indexPath: %s. PollID: %s", ((#file as NSString).lastPathComponent), #line, #function, indexPath.debugDescription, pollID ?? "") + } guard let context = delegate?.context else { return nil } guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return nil } @@ -127,6 +134,7 @@ extension StatusTableViewCell: UITableViewDelegate { return nil } let poll = option.poll + pollID = poll.id // disallow select when: poll expired OR user voted remote OR user voted local let userID = activeMastodonAuthenticationBox.userID