// A WhatsApp poll in Comms, drawn as a poll (FEEDBACK-3 "polls shown"; // CONTRACT.md "Polls shown", the core's bubble.poll, 9dea975162; Android // e69a6d5152). In the bubble: the question in the body's weight, how many may // be chosen, each option with its count at the end and a thin bar of its part // of the people who voted (the accent on theirs, his own ink on his), his own // choice ticked, then the total. The whole card is read as one line (the // core's `spoken`) and a tap opens Votes: each option, its count and who chose // it. A vote arriving changes the row and the open chat is answered again, so // the bars move, and the Votes panel, read from the page as it stands, follows. // Where he can vote (`poll.votable`, CONTRACT.md "A vote"), each option is its // own control, a round box beside it ticked on his choice: a tap sends // `chat.act vote` and the core works out his choice; the total opens Votes. #if canImport(UIKit) import SwiftUI struct ChatPollView: View { let poll: ChatPoll let message: String let mine: Bool /// Where he can vote (`poll.votable`): a tap on option n votes (`chat.act vote`), and the total opens Votes. /// Without it, a tap anywhere on the card opens Votes. var vote: ((Int) -> Void)? = nil var votes: () -> Void = {} @Environment(\.chatLook) private var look /// At the accessibility sizes an option's count stands under its words, which wrap whole, never cut. @Environment(\.dynamicTypeSize) private var size private var large: Bool { size.isAccessibilitySize } /// The round box he votes with, growing with the text (Android's 20 dp at the default size). @ScaledMetric(relativeTo: .body) private var box: CGFloat = 20 private var ink: Color { mine ? look.colours.bubbleOutText : look.colours.text } private var quiet: Color { mine ? look.colours.bubbleOutDim : look.colours.dim } private var track: Color { mine ? look.colours.bubbleOutText.opacity(0.16) : look.colours.rule } private var bar: Color { mine ? look.colours.bubbleOutText.opacity(0.7) : look.colours.accent } var body: some View { if vote != nil { card } else { card .contentShape(Rectangle()) .onTapGesture(perform: votes) .accessibilityAddTraits(.isButton) } } private var card: some View { VStack(alignment: .leading, spacing: 10) { VStack(alignment: .leading, spacing: 2) { Text(verbatim: poll.question) .font(look.font(17, .semibold)) .foregroundStyle(ink) .fixedSize(horizontal: false, vertical: true) if let choose = poll.choose { Text(verbatim: choose).font(look.font(12.5)).foregroundStyle(quiet) } } ForEach(Array(poll.options.enumerated()), id: \.offset) { index, option in if let vote { // Where he votes, each option is its own control: a round box beside it, filled with a tick on his // own choice (Android's PollView). Button { vote(index) } label: { row(option, picking: true) } .buttonStyle(.plain) .accessibilityElement(children: .ignore) .accessibilityLabel(Text(verbatim: "\(option.text), \(option.said)")) .accessibilityAddTraits(option.mine ? [.isButton, .isSelected] : .isButton) .accessibilityIdentifier("chat-poll-\(message)-option-\(index)") } else { row(option, picking: false) } } if vote != nil { Button(action: votes) { total } .buttonStyle(.plain) .accessibilityLabel(Text(verbatim: poll.said)) .accessibilityIdentifier("chat-poll-\(message)-said") } else { total } } .frame(minWidth: 220, maxWidth: large ? .infinity : 280, alignment: .leading) .padding(.top, 2) .padding(.bottom, 4) } private var total: some View { Text(verbatim: poll.said).font(look.font(12.5)).foregroundStyle(quiet) .frame(maxWidth: .infinity, minHeight: vote != nil ? 28 : nil, alignment: .leading) .contentShape(Rectangle()) } private func row(_ option: ChatPollOption, picking: Bool) -> some View { VStack(alignment: .leading, spacing: 5) { HStack(alignment: picking ? .center : .firstTextBaseline, spacing: picking ? 8 : 6) { if picking { ZStack { Circle().fill(option.mine ? bar : track) if option.mine { Image(systemName: "checkmark") .font(.system(size: min(box, 36) * 0.55 * look.scale, weight: .bold)) .foregroundStyle(mine ? look.colours.bubbleOut : look.colours.onAccent) } } .frame(width: min(box, 36) * look.scale, height: min(box, 36) * look.scale) } else if option.mine { Image(systemName: "checkmark") .font(.system(size: 13 * look.scale, weight: .bold)) .foregroundStyle(ink) } Text(verbatim: option.text) .font(look.font(15)) .foregroundStyle(ink) .lineLimit(large ? nil : 3) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) if !large { count(option) } } if large { count(option) } ChatPollBar(percent: option.percent, track: track, bar: bar) } .contentShape(Rectangle()) } } extension ChatPollView { fileprivate func count(_ option: ChatPollOption) -> some View { Text(verbatim: option.said) .font(look.font(12.5)) .foregroundStyle(quiet) .lineLimit(1) .layoutPriority(1) } } /// An option's part of the people who voted: a thin track, filled from the start. struct ChatPollBar: View { let percent: Int let track: Color let bar: Color var body: some View { Capsule() .fill(track) .frame(height: 4) .overlay(alignment: .leading) { GeometryReader { space in if percent > 0 { Capsule().fill(bar).frame(width: space.size.width * CGFloat(min(percent, 100)) / 100) } } } .accessibilityHidden(true) } } /// The poll a tap opened, by its message: the Votes panel reads the poll from the page as it stands now. struct ChatPollOpen: Identifiable, Equatable { let message: String var id: String { message } } /// A poll's Votes: each option, how many chose it, and who, as the chat names them. struct ChatPollVotesSheet: View { let poll: ChatPoll @Environment(\.chatLook) private var look var body: some View { // Whole when it fits; scrolled inside the panel when it does not (the largest text, a long poll). ViewThatFits(in: .vertical) { votes ScrollView { votes } } .accessibilityElement(children: .contain) .accessibilityIdentifier("chat-poll-votes") } private var votes: some View { VStack(alignment: .leading, spacing: 4) { Text(verbatim: poll.votesTitle) .font(look.font(20, .bold)).foregroundStyle(look.colours.text) .accessibilityAddTraits(.isHeader) Text(verbatim: poll.question) .font(look.font(16, .semibold)).foregroundStyle(look.colours.text) .fixedSize(horizontal: false, vertical: true) .padding(.bottom, 6) ForEach(Array(poll.options.enumerated()), id: \.offset) { index, option in VStack(alignment: .leading, spacing: 4) { HStack(alignment: .firstTextBaseline) { Text(verbatim: option.text).font(look.font(16, .medium)).foregroundStyle(look.colours.text) .fixedSize(horizontal: false, vertical: true) Spacer(minLength: 12) Text(verbatim: option.said).font(look.font(13)).foregroundStyle(look.colours.dim) } ForEach(option.voters, id: \.self) { who in Text(verbatim: who).font(look.font(15)).foregroundStyle(look.colours.dim) } } .padding(.vertical, 8) .frame(maxWidth: .infinity, alignment: .leading) .accessibilityElement(children: .combine) .accessibilityIdentifier("chat-poll-votes-\(index)") } } .padding(.horizontal, Warm.side) .padding(.vertical, 18) } } #endif