Don't leave space for checkbox when no checkboxes are shown

This commit is contained in:
Shadowfacts 2023-05-13 14:14:38 -04:00
parent bf02b185ed
commit 735659dee6
2 changed files with 28 additions and 16 deletions

View File

@ -13,20 +13,23 @@ class PollOptionView: UIView {
private let unselectedBackgroundColor = UIColor(white: 0.5, alpha: 0.25)
let checkbox: PollOptionCheckboxView
private(set) var checkbox: PollOptionCheckboxView?
init(poll: Poll, option: Poll.Option, mastodonController: MastodonController) {
checkbox = PollOptionCheckboxView(multiple: poll.multiple)
super.init(frame: .zero)
let minHeight: CGFloat = 35
layer.cornerRadius = 0.1 * minHeight
layer.cornerCurve = .continuous
backgroundColor = unselectedBackgroundColor
checkbox.translatesAutoresizingMaskIntoConstraints = false
addSubview(checkbox)
let showCheckbox = poll.ownVotes?.isEmpty == false || !poll.effectiveExpired
if showCheckbox {
checkbox = PollOptionCheckboxView(multiple: poll.multiple)
checkbox!.translatesAutoresizingMaskIntoConstraints = false
addSubview(checkbox!)
}
let label = EmojiLabel()
label.translatesAutoresizingMaskIntoConstraints = false
@ -88,12 +91,9 @@ class PollOptionView: UIView {
NSLayoutConstraint.activate([
minHeightConstraint,
checkbox.centerYAnchor.constraint(equalTo: centerYAnchor),
checkbox.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
label.topAnchor.constraint(equalTo: topAnchor, constant: 4),
label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -4),
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 8),
label.trailingAnchor.constraint(equalTo: percentLabel.leadingAnchor, constant: -4),
percentLabel.topAnchor.constraint(equalTo: topAnchor),
@ -101,6 +101,16 @@ class PollOptionView: UIView {
percentLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
])
if let checkbox {
NSLayoutConstraint.activate([
checkbox.centerYAnchor.constraint(equalTo: centerYAnchor),
checkbox.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 8),
])
} else {
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8).isActive = true
}
isAccessibilityElement = true
}

View File

@ -14,7 +14,7 @@ class PollOptionsView: UIControl {
var mastodonController: MastodonController!
var checkedOptionIndices: [Int] {
options.enumerated().filter { $0.element.checkbox.isChecked }.map(\.offset)
options.enumerated().filter { $0.element.checkbox?.isChecked == true }.map(\.offset)
}
var checkedOptionsChanged: (() -> Void)?
@ -32,7 +32,7 @@ class PollOptionsView: UIControl {
override var isEnabled: Bool {
didSet {
options.forEach { $0.checkbox.readOnly = !isEnabled }
options.forEach { $0.checkbox?.readOnly = !isEnabled }
}
}
@ -65,9 +65,11 @@ class PollOptionsView: UIControl {
options = poll.options.enumerated().map { (index, opt) in
let optionView = PollOptionView(poll: poll, option: opt, mastodonController: mastodonController)
optionView.checkbox.readOnly = !isEnabled
optionView.checkbox.isChecked = poll.ownVotes?.contains(index) ?? false
optionView.checkbox.voted = poll.voted ?? false
if let checkbox = optionView.checkbox {
checkbox.readOnly = !isEnabled
checkbox.isChecked = poll.ownVotes?.contains(index) ?? false
checkbox.voted = poll.voted ?? false
}
stack.addArrangedSubview(optionView)
return optionView
}
@ -77,13 +79,13 @@ class PollOptionsView: UIControl {
private func selectOption(_ option: PollOptionView) {
if poll.multiple {
option.checkbox.isChecked.toggle()
option.checkbox?.isChecked.toggle()
} else {
for opt in options {
if opt === option {
opt.checkbox.isChecked = true
opt.checkbox?.isChecked = true
} else {
opt.checkbox.isChecked = false
opt.checkbox?.isChecked = false
}
}
}