|
| 1 | +import AutoLayout |
| 2 | +import Combine |
| 3 | +import UIKit |
| 4 | + |
| 5 | +/// A UIKit component that displays a countdown. |
| 6 | +public class UKCountdown: UIView, UKComponent { |
| 7 | + // MARK: - Public Properties |
| 8 | + |
| 9 | + /// A model that defines the appearance properties. |
| 10 | + public var model: CountdownVM { |
| 11 | + didSet { |
| 12 | + self.update(oldValue) |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + /// The main container stack view containing all time labels and colon labels. |
| 17 | + public let stackView = UIStackView() |
| 18 | + |
| 19 | + /// A label showing the number of days remaining. |
| 20 | + public let daysLabel = UILabel() |
| 21 | + |
| 22 | + /// A label showing the number of hours remaining. |
| 23 | + public let hoursLabel = UILabel() |
| 24 | + |
| 25 | + /// A label showing the number of minutes remaining. |
| 26 | + public let minutesLabel = UILabel() |
| 27 | + |
| 28 | + /// A label showing the number of seconds remaining. |
| 29 | + public let secondsLabel = UILabel() |
| 30 | + |
| 31 | + /// An array of colon labels used as separators between the time segments (days/hours/minutes/seconds). |
| 32 | + public let colonLabels: [UILabel] = [ |
| 33 | + UILabel(), |
| 34 | + UILabel(), |
| 35 | + UILabel() |
| 36 | + ] |
| 37 | + |
| 38 | + // MARK: - Private Properties |
| 39 | + |
| 40 | + /// Constraints specifically applied to the "days" label. |
| 41 | + private var daysConstraints = LayoutConstraints() |
| 42 | + |
| 43 | + private let manager = CountdownManager() |
| 44 | + |
| 45 | + private var cancellables: Set<AnyCancellable> = [] |
| 46 | + |
| 47 | + // MARK: - Initialization |
| 48 | + |
| 49 | + /// Initializer. |
| 50 | + /// - Parameters: |
| 51 | + /// - model: A model that defines the appearance properties. |
| 52 | + public init(model: CountdownVM) { |
| 53 | + self.model = model |
| 54 | + |
| 55 | + super.init(frame: .zero) |
| 56 | + |
| 57 | + self.setup() |
| 58 | + self.style() |
| 59 | + self.layout() |
| 60 | + } |
| 61 | + |
| 62 | + required init?(coder: NSCoder) { |
| 63 | + fatalError("init(coder:) has not been implemented") |
| 64 | + } |
| 65 | + |
| 66 | + deinit { |
| 67 | + self.manager.stop() |
| 68 | + self.cancellables.forEach { |
| 69 | + $0.cancel() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // MARK: - Setup |
| 74 | + |
| 75 | + private func setup() { |
| 76 | + self.addSubview(self.stackView) |
| 77 | + |
| 78 | + self.stackView.addArrangedSubview(self.daysLabel) |
| 79 | + self.stackView.addArrangedSubview(self.colonLabels[0]) |
| 80 | + self.stackView.addArrangedSubview(self.hoursLabel) |
| 81 | + self.stackView.addArrangedSubview(self.colonLabels[1]) |
| 82 | + self.stackView.addArrangedSubview(self.minutesLabel) |
| 83 | + self.stackView.addArrangedSubview(self.colonLabels[2]) |
| 84 | + self.stackView.addArrangedSubview(self.secondsLabel) |
| 85 | + |
| 86 | + self.setupSubscriptions() |
| 87 | + self.manager.start(until: self.model.until) |
| 88 | + } |
| 89 | + |
| 90 | + private func setupSubscriptions() { |
| 91 | + self.manager.$days |
| 92 | + .sink { [weak self] newValue in |
| 93 | + guard let self else { return } |
| 94 | + self.daysLabel.attributedText = self.model.timeText(value: newValue, unit: .days) |
| 95 | + } |
| 96 | + .store(in: &self.cancellables) |
| 97 | + |
| 98 | + self.manager.$hours |
| 99 | + .sink { [weak self] newValue in |
| 100 | + guard let self else { return } |
| 101 | + self.hoursLabel.attributedText = self.model.timeText(value: newValue, unit: .hours) |
| 102 | + } |
| 103 | + .store(in: &self.cancellables) |
| 104 | + |
| 105 | + self.manager.$minutes |
| 106 | + .sink { [weak self] newValue in |
| 107 | + guard let self else { return } |
| 108 | + self.minutesLabel.attributedText = self.model.timeText(value: newValue, unit: .minutes) |
| 109 | + } |
| 110 | + .store(in: &self.cancellables) |
| 111 | + |
| 112 | + self.manager.$seconds |
| 113 | + .sink { [weak self] newValue in |
| 114 | + guard let self else { return } |
| 115 | + self.secondsLabel.attributedText = self.model.timeText(value: newValue, unit: .seconds) |
| 116 | + } |
| 117 | + .store(in: &self.cancellables) |
| 118 | + } |
| 119 | + |
| 120 | + // MARK: - Style |
| 121 | + |
| 122 | + private func style() { |
| 123 | + Self.Style.mainView(self, model: self.model) |
| 124 | + Self.Style.stackView(self.stackView, model: self.model) |
| 125 | + |
| 126 | + Self.Style.timeLabel(self.daysLabel, model: self.model) |
| 127 | + Self.Style.timeLabel(self.hoursLabel, model: self.model) |
| 128 | + Self.Style.timeLabel(self.minutesLabel, model: self.model) |
| 129 | + Self.Style.timeLabel(self.secondsLabel, model: self.model) |
| 130 | + |
| 131 | + self.colonLabels.forEach { |
| 132 | + Self.Style.colonLabel($0, model: self.model) |
| 133 | + } |
| 134 | + |
| 135 | + self.daysLabel.attributedText = self.model.timeText(value: self.manager.days, unit: .days) |
| 136 | + self.hoursLabel.attributedText = self.model.timeText(value: self.manager.hours, unit: .hours) |
| 137 | + self.minutesLabel.attributedText = self.model.timeText(value: self.manager.minutes, unit: .minutes) |
| 138 | + self.secondsLabel.attributedText = self.model.timeText(value: self.manager.seconds, unit: .seconds) |
| 139 | + } |
| 140 | + |
| 141 | + // MARK: - Layout |
| 142 | + |
| 143 | + private func layout() { |
| 144 | + self.stackView.centerVertically() |
| 145 | + self.stackView.centerHorizontally() |
| 146 | + |
| 147 | + self.stackView.topAnchor.constraint( |
| 148 | + greaterThanOrEqualTo: self.topAnchor |
| 149 | + ).isActive = true |
| 150 | + self.stackView.bottomAnchor.constraint( |
| 151 | + lessThanOrEqualTo: self.bottomAnchor |
| 152 | + ).isActive = true |
| 153 | + self.stackView.leadingAnchor.constraint( |
| 154 | + greaterThanOrEqualTo: self.leadingAnchor |
| 155 | + ).isActive = true |
| 156 | + self.stackView.trailingAnchor.constraint( |
| 157 | + lessThanOrEqualTo: self.trailingAnchor |
| 158 | + ).isActive = true |
| 159 | + |
| 160 | + self.daysConstraints.width = self.daysLabel.widthAnchor.constraint( |
| 161 | + equalToConstant: self.model.defaultMinWidth |
| 162 | + ) |
| 163 | + self.daysConstraints.width?.isActive = true |
| 164 | + |
| 165 | + self.daysConstraints.height = self.daysLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: self.model.height) |
| 166 | + self.daysConstraints.height?.isActive = true |
| 167 | + |
| 168 | + self.hoursLabel.widthAnchor.constraint(equalTo: self.daysLabel.widthAnchor).isActive = true |
| 169 | + self.hoursLabel.heightAnchor.constraint(equalTo: self.daysLabel.heightAnchor).isActive = true |
| 170 | + |
| 171 | + self.minutesLabel.widthAnchor.constraint(equalTo: self.daysLabel.widthAnchor).isActive = true |
| 172 | + self.minutesLabel.heightAnchor.constraint(equalTo: self.daysLabel.heightAnchor).isActive = true |
| 173 | + |
| 174 | + self.secondsLabel.widthAnchor.constraint(equalTo: self.daysLabel.widthAnchor).isActive = true |
| 175 | + self.secondsLabel.heightAnchor.constraint(equalTo: self.daysLabel.heightAnchor).isActive = true |
| 176 | + |
| 177 | + switch self.model.style { |
| 178 | + case .plain: |
| 179 | + self.daysConstraints.height?.isActive = false |
| 180 | + self.daysConstraints.width?.constant = self.model.timeWidth(manager: self.manager) |
| 181 | + case .light: |
| 182 | + self.daysConstraints.width?.constant = max( |
| 183 | + self.model.timeWidth(manager: self.manager), |
| 184 | + self.model.lightBackgroundMinWidth |
| 185 | + ) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // MARK: - Update |
| 190 | + |
| 191 | + public func update(_ oldModel: CountdownVM) { |
| 192 | + guard self.model != oldModel else { return } |
| 193 | + |
| 194 | + if self.model.until != oldModel.until { |
| 195 | + self.manager.stop() |
| 196 | + self.manager.start(until: self.model.until) |
| 197 | + } |
| 198 | + |
| 199 | + if self.model.shouldUpdateHeight(oldModel) { |
| 200 | + switch self.model.style { |
| 201 | + case .plain: |
| 202 | + self.daysConstraints.height?.isActive = false |
| 203 | + case .light: |
| 204 | + self.daysConstraints.height?.isActive = true |
| 205 | + self.daysConstraints.height?.constant = self.model.height |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + if self.model.shouldRecalculateWidth(oldModel) { |
| 210 | + let newWidth = self.model.timeWidth(manager: self.manager) |
| 211 | + switch self.model.style { |
| 212 | + case .plain: |
| 213 | + self.daysConstraints.width?.constant = newWidth |
| 214 | + case .light: |
| 215 | + self.daysConstraints.width?.constant = max(newWidth, self.model.lightBackgroundMinWidth) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + self.style() |
| 220 | + |
| 221 | + self.layoutIfNeeded() |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +// MARK: - Style Helpers |
| 226 | + |
| 227 | +extension UKCountdown { |
| 228 | + fileprivate enum Style { |
| 229 | + static func mainView(_ view: UIView, model: CountdownVM) { |
| 230 | + view.backgroundColor = .clear |
| 231 | + } |
| 232 | + |
| 233 | + static func stackView(_ stackView: UIStackView, model: CountdownVM) { |
| 234 | + stackView.axis = .horizontal |
| 235 | + stackView.alignment = .top |
| 236 | + stackView.spacing = model.spacing |
| 237 | + } |
| 238 | + |
| 239 | + static func timeLabel(_ label: UILabel, model: CountdownVM) { |
| 240 | + switch model.style { |
| 241 | + case .plain: |
| 242 | + label.backgroundColor = .clear |
| 243 | + label.layer.cornerRadius = 0 |
| 244 | + case .light: |
| 245 | + label.backgroundColor = model.backgroundColor.uiColor |
| 246 | + label.layer.cornerRadius = 8 |
| 247 | + label.clipsToBounds = true |
| 248 | + } |
| 249 | + label.font = model.preferredFont.uiFont |
| 250 | + label.textColor = model.foregroundColor.uiColor |
| 251 | + label.textAlignment = .center |
| 252 | + label.numberOfLines = 0 |
| 253 | + label.lineBreakMode = .byClipping |
| 254 | + } |
| 255 | + |
| 256 | + static func colonLabel(_ label: UILabel, model: CountdownVM) { |
| 257 | + label.text = ":" |
| 258 | + label.font = model.preferredFont.uiFont |
| 259 | + label.textColor = model.colonColor.uiColor |
| 260 | + label.textAlignment = .center |
| 261 | + label.isVisible = model.isColumnLabelVisible |
| 262 | + } |
| 263 | + } |
| 264 | +} |
0 commit comments