|
| 1 | +// |
| 2 | +// Copyright © 2024 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import UIKit |
| 6 | + |
| 7 | +/// The cell to configure the multiple votes poll feature. |
| 8 | +open class PollCreationMultipleVotesFeatureCell: _CollectionViewCell, ThemeProvider, UITextFieldDelegate { |
| 9 | + public struct Content { |
| 10 | + public var feature: MultipleVotesPollFeature |
| 11 | + public var maximumVotesErrorText: String? |
| 12 | + |
| 13 | + public init( |
| 14 | + feature: MultipleVotesPollFeature, |
| 15 | + maximumVotesErrorText: String? |
| 16 | + ) { |
| 17 | + self.feature = feature |
| 18 | + self.maximumVotesErrorText = maximumVotesErrorText |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public var content: Content? { |
| 23 | + didSet { |
| 24 | + updateContentIfNeeded() |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /// The main container that holds the subviews. |
| 29 | + open private(set) lazy var container = VContainer(spacing: 4) |
| 30 | + |
| 31 | + /// A view that displays the feature name and the switch to enable/disable the feature. |
| 32 | + open private(set) lazy var featureSwitchView = components |
| 33 | + .pollCreationFeatureSwitchView.init() |
| 34 | + .withoutAutoresizingMaskConstraints |
| 35 | + |
| 36 | + /// A view to configure the maximum votes per user. |
| 37 | + open private(set) lazy var maximumVotesSwitchView = PollCreationMaximumVotesSwitchView() |
| 38 | + .withoutAutoresizingMaskConstraints |
| 39 | + |
| 40 | + private var currentMaximumVotesText: String = "" { |
| 41 | + didSet { |
| 42 | + validateMaximumVotesValue(currentMaximumVotesText) |
| 43 | + onMaximumVotesTextChanged?(currentMaximumVotesText) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// A closure that is triggered whenever the feature is enabled or disabled. |
| 48 | + public var onFeatureEnabledChanged: ((Bool) -> Void)? |
| 49 | + |
| 50 | + /// A closure that is triggered whenever the maximum votes value changes. |
| 51 | + public var onMaximumVotesValueChanged: ((Int?) -> Void)? |
| 52 | + |
| 53 | + /// A closure that is triggered whenever the maximum votes text changes. |
| 54 | + public var onMaximumVotesTextChanged: ((String) -> Void)? |
| 55 | + |
| 56 | + /// A closure that is triggered whenever the validation of the maximum votes changes. |
| 57 | + public var onMaximumVotesErrorTextChanged: ((String?) -> Void)? |
| 58 | + |
| 59 | + /// The text for the maximum votes input placeholder. |
| 60 | + open var maximumVotesPlaceholderText: String { |
| 61 | + L10n.Polls.Creation.maximumVotesPlaceholder |
| 62 | + } |
| 63 | + |
| 64 | + /// The error text for the maximum votes input. |
| 65 | + open var maximumVotesErrorText: String { |
| 66 | + L10n.Polls.Creation.maximumVotesError |
| 67 | + } |
| 68 | + |
| 69 | + override open func setUp() { |
| 70 | + super.setUp() |
| 71 | + |
| 72 | + contentView.isUserInteractionEnabled = true |
| 73 | + |
| 74 | + maximumVotesSwitchView.textFieldView.inputTextField.delegate = self |
| 75 | + |
| 76 | + featureSwitchView.onValueChange = { [weak self] isOn in |
| 77 | + self?.onFeatureEnabledChanged?(isOn) |
| 78 | + self?.resetMaximumVotesInput() |
| 79 | + self?.clearMaxVotesError() |
| 80 | + } |
| 81 | + |
| 82 | + maximumVotesSwitchView.textFieldView.onTextChanged = { [weak self] _, newValue in |
| 83 | + self?.currentMaximumVotesText = newValue |
| 84 | + } |
| 85 | + |
| 86 | + maximumVotesSwitchView.onValueChange = { [weak self] _ in |
| 87 | + self?.resetMaximumVotesInput() |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override open func setUpAppearance() { |
| 92 | + super.setUpAppearance() |
| 93 | + |
| 94 | + backgroundColor = appearance.colorPalette.background |
| 95 | + container.backgroundColor = appearance.colorPalette.background1 |
| 96 | + container.layer.cornerRadius = 16 |
| 97 | + } |
| 98 | + |
| 99 | + override open func setUpLayout() { |
| 100 | + super.setUpLayout() |
| 101 | + |
| 102 | + container.views { |
| 103 | + featureSwitchView |
| 104 | + .height(PollCreationVC.pollCreationInputViewHeight) |
| 105 | + maximumVotesSwitchView |
| 106 | + .height(PollCreationVC.pollCreationInputViewHeight) |
| 107 | + } |
| 108 | + .layout { |
| 109 | + $0.isLayoutMarginsRelativeArrangement = true |
| 110 | + $0.directionalLayoutMargins = .init(top: 0, leading: 12, bottom: 0, trailing: 12) |
| 111 | + } |
| 112 | + .embed(in: self, insets: .init(top: 6, leading: 12, bottom: 6, trailing: 12)) |
| 113 | + } |
| 114 | + |
| 115 | + override open func updateContent() { |
| 116 | + super.updateContent() |
| 117 | + |
| 118 | + guard let content = self.content else { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + featureSwitchView.featureNameLabel.text = content.feature.name |
| 123 | + featureSwitchView.switchView.isOn = content.feature.isEnabled |
| 124 | + maximumVotesSwitchView.isHidden = content.feature.maxVotesConfig == nil ? true : !content.feature.isEnabled |
| 125 | + maximumVotesSwitchView.switchView.isOn = content.feature.maxVotesConfig?.isEnabled ?? false |
| 126 | + maximumVotesSwitchView.textFieldView.content = .init( |
| 127 | + placeholder: maximumVotesPlaceholderText, |
| 128 | + errorText: content.maximumVotesErrorText |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + /// Sets the maximum votes in the maximum votes switch view. |
| 133 | + open func setMaximumVotesText(_ text: String) { |
| 134 | + maximumVotesSwitchView.textFieldView.setText(text) |
| 135 | + } |
| 136 | + |
| 137 | + /// Validates the maximum votes text and shows an error if it is not valid. |
| 138 | + open func validateMaximumVotesValue(_ newValue: String) { |
| 139 | + let errorText = maximumVotesErrorText |
| 140 | + |
| 141 | + if newValue.isEmpty && maximumVotesSwitchView.switchView.isOn { |
| 142 | + showMaxVotesError(message: errorText) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + if newValue.isEmpty { |
| 147 | + clearMaxVotesError() |
| 148 | + maximumVotesSwitchView.switchView.setOn(false, animated: true) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + maximumVotesSwitchView.switchView.setOn(true, animated: true) |
| 153 | + |
| 154 | + guard let value = Int(newValue), value >= 1 && value <= 10 else { |
| 155 | + showMaxVotesError(message: errorText) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + clearMaxVotesError() |
| 160 | + onMaximumVotesValueChanged?(value) |
| 161 | + } |
| 162 | + |
| 163 | + /// Shows an error in the maximum votes switch view. |
| 164 | + open func showMaxVotesError(message: String) { |
| 165 | + maximumVotesSwitchView.textFieldView.content?.errorText = message |
| 166 | + onMaximumVotesErrorTextChanged?(message) |
| 167 | + } |
| 168 | + |
| 169 | + /// Clears the error of the maximum votes switch view. |
| 170 | + open func clearMaxVotesError() { |
| 171 | + maximumVotesSwitchView.textFieldView.content?.errorText = nil |
| 172 | + onMaximumVotesErrorTextChanged?(nil) |
| 173 | + } |
| 174 | + |
| 175 | + open func resetMaximumVotesInput() { |
| 176 | + maximumVotesSwitchView.textFieldView.inputTextField.text = nil |
| 177 | + currentMaximumVotesText = "" |
| 178 | + } |
| 179 | + |
| 180 | + open func textFieldShouldReturn(_ textField: UITextField) -> Bool { |
| 181 | + textField.resignFirstResponder() |
| 182 | + return true |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +open class PollCreationMaximumVotesSwitchView: _View, ThemeProvider { |
| 187 | + /// A text field that supports showing validator errors. |
| 188 | + open private(set) lazy var textFieldView = components |
| 189 | + .pollCreationTextFieldView.init() |
| 190 | + .withoutAutoresizingMaskConstraints |
| 191 | + |
| 192 | + /// A view to switch on or off. Used to enable or disable poll features. |
| 193 | + open private(set) lazy var switchView = UISwitch() |
| 194 | + .withoutAutoresizingMaskConstraints |
| 195 | + |
| 196 | + /// A closure that is triggered whenever the switch value changes. |
| 197 | + public var onValueChange: ((Bool) -> Void)? |
| 198 | + |
| 199 | + override open func setUp() { |
| 200 | + super.setUp() |
| 201 | + |
| 202 | + textFieldView.inputTextField.keyboardType = .numberPad |
| 203 | + switchView.addTarget(self, action: #selector(switchChangedValue(sender:)), for: .valueChanged) |
| 204 | + } |
| 205 | + |
| 206 | + override open func setUpLayout() { |
| 207 | + super.setUpLayout() |
| 208 | + |
| 209 | + HContainer(spacing: 4, alignment: .center) { |
| 210 | + textFieldView |
| 211 | + switchView |
| 212 | + } |
| 213 | + .embed(in: self) |
| 214 | + } |
| 215 | + |
| 216 | + @objc open func switchChangedValue(sender: Any?) { |
| 217 | + onValueChange?(switchView.isOn) |
| 218 | + } |
| 219 | +} |
0 commit comments