|
| 1 | +// |
| 2 | +// ServiceAlertTableViewCell.swift |
| 3 | +// TCAT |
| 4 | +// |
| 5 | +// Created by Omar Rasheed on 12/7/18. |
| 6 | +// Copyright © 2018 cuappdev. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import SnapKit |
| 11 | + |
| 12 | +class ServiceAlertTableViewCell: UITableViewCell { |
| 13 | + |
| 14 | + static let identifier: String = "serviceAlertCell" |
| 15 | + private let fileName: String = "serviceAlertTableViewCell" |
| 16 | + var alert: Alert? |
| 17 | + var rowNum: Int! |
| 18 | + |
| 19 | + let borderInset = 16 |
| 20 | + let busIconSpacing = 10 |
| 21 | + var maxIconsPerRow: Int { |
| 22 | + let iconWidth = Int(BusIconType.directionSmall.width) |
| 23 | + let screenWidth = Int(UIScreen.main.bounds.width) |
| 24 | + let totalConstraintInset = borderInset * 2 |
| 25 | + |
| 26 | + return (screenWidth - totalConstraintInset + busIconSpacing) / (iconWidth + busIconSpacing) |
| 27 | + } |
| 28 | + |
| 29 | + var timeSpanLabel: UILabel! |
| 30 | + var descriptionLabel: UILabel! |
| 31 | + var affectedRoutesLabel: UILabel! |
| 32 | + var affectedRoutesStackView: UIStackView? |
| 33 | + var topSeparator: UIView? |
| 34 | + |
| 35 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 36 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 37 | + |
| 38 | + setupTimeSpanLabel() |
| 39 | + setupDescriptionLabel() |
| 40 | + } |
| 41 | + |
| 42 | + func setData() { |
| 43 | + if let fromDate = alert?.fromDate, let toDate = alert?.toDate { |
| 44 | + timeSpanLabel.text = formatTimeString(fromDate, toDate: toDate) |
| 45 | + } |
| 46 | + |
| 47 | + descriptionLabel.text = alert?.message |
| 48 | + if let routes = alert?.routes, !routes.isEmpty { |
| 49 | + setupAffectedRoutesStackView() |
| 50 | + setupaffectedRoutesLabel() |
| 51 | + } |
| 52 | + |
| 53 | + if rowNum > 0 { |
| 54 | + setupTopSeparator() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private func setupTimeSpanLabel() { |
| 59 | + |
| 60 | + timeSpanLabel = UILabel() |
| 61 | + timeSpanLabel.numberOfLines = 0 |
| 62 | + timeSpanLabel.font = .getFont(.semibold, size: 18) |
| 63 | + timeSpanLabel.textColor = Colors.primaryText |
| 64 | + |
| 65 | + contentView.addSubview(timeSpanLabel) |
| 66 | + } |
| 67 | + |
| 68 | + private func setupDescriptionLabel() { |
| 69 | + |
| 70 | + descriptionLabel = UILabel() |
| 71 | + descriptionLabel.numberOfLines = 0 |
| 72 | + descriptionLabel.font = .getFont(.regular, size: 14) |
| 73 | + descriptionLabel.textColor = Colors.primaryText |
| 74 | + |
| 75 | + contentView.addSubview(descriptionLabel) |
| 76 | + } |
| 77 | + |
| 78 | + private func setupaffectedRoutesLabel() { |
| 79 | + |
| 80 | + affectedRoutesLabel = UILabel() |
| 81 | + affectedRoutesLabel.font = .getFont(.semibold, size: 18) |
| 82 | + affectedRoutesLabel.textColor = Colors.primaryText |
| 83 | + affectedRoutesLabel.text = Constants.General.affectedRoutes |
| 84 | + |
| 85 | + contentView.addSubview(affectedRoutesLabel) |
| 86 | + } |
| 87 | + |
| 88 | + private func setupAffectedRoutesStackView() { |
| 89 | + |
| 90 | + if var routes = alert?.routes, !routes.isEmpty { |
| 91 | + affectedRoutesStackView = UIStackView() |
| 92 | + for _ in 0..<rowCount() { |
| 93 | + var subviews = [BusIcon]() |
| 94 | + for _ in 0..<maxIconsPerRow { |
| 95 | + if !routes.isEmpty { |
| 96 | + let route = routes.removeFirst() |
| 97 | + subviews.append(BusIcon(type: .directionSmall, number: route)) |
| 98 | + } |
| 99 | + } |
| 100 | + let rowStackView = UIStackView(arrangedSubviews: subviews) |
| 101 | + rowStackView.axis = .horizontal |
| 102 | + rowStackView.spacing = 10 |
| 103 | + rowStackView.alignment = .leading |
| 104 | + affectedRoutesStackView?.addArrangedSubview(rowStackView) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + guard let stackView = affectedRoutesStackView else { return } |
| 109 | + stackView.axis = .vertical |
| 110 | + stackView.alignment = .top |
| 111 | + stackView.spacing = 10 |
| 112 | + contentView.addSubview(stackView) |
| 113 | + } |
| 114 | + |
| 115 | + private func setupTopSeparator() { |
| 116 | + |
| 117 | + topSeparator = UIView() |
| 118 | + topSeparator?.backgroundColor = Colors.backgroundWash |
| 119 | + |
| 120 | + contentView.addSubview(topSeparator!) |
| 121 | + } |
| 122 | + |
| 123 | + func descriptionLabelConstraints(topConstraint: UIView) { |
| 124 | + descriptionLabel.snp.remakeConstraints { (make) in |
| 125 | + if topConstraint == contentView { |
| 126 | + make.top.equalToSuperview().inset(borderInset) |
| 127 | + } else { |
| 128 | + make.top.equalTo(topConstraint.snp.bottom).offset(12) |
| 129 | + } |
| 130 | + make.leading.trailing.equalToSuperview().inset(borderInset) |
| 131 | + if let text = descriptionLabel.text { |
| 132 | + let width = contentView.frame.width - (CGFloat)(2 * borderInset) |
| 133 | + let heightValue = ceil(text.heightWithConstrainedWidth(width: width, font: descriptionLabel.font)) |
| 134 | + make.height.equalTo(ceil(heightValue)) |
| 135 | + } else { |
| 136 | + make.height.equalTo(descriptionLabel.intrinsicContentSize.height) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + func timeSpanLabelConstraints(topConstraint: UIView) { |
| 142 | + timeSpanLabel.snp.remakeConstraints { (make) in |
| 143 | + if topConstraint == contentView { |
| 144 | + make.top.equalToSuperview().inset(borderInset) |
| 145 | + } else { |
| 146 | + make.top.equalTo(topConstraint.snp.bottom).offset(12) |
| 147 | + } |
| 148 | + make.leading.trailing.equalToSuperview().inset(borderInset) |
| 149 | + make.height.equalTo(timeSpanLabel.intrinsicContentSize.height) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + func topSeparatorConstraints() { |
| 154 | + if let topSeparator = topSeparator { |
| 155 | + topSeparator.snp.remakeConstraints { (make) in |
| 156 | + make.top.leading.trailing.equalToSuperview() |
| 157 | + make.height.equalTo(8) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + override func updateConstraints() { |
| 163 | + if let topSeparator = topSeparator, timeSpanLabel.isDescendant(of: contentView) { |
| 164 | + // Both topSeparator and timeSpanLabel exist |
| 165 | + topSeparatorConstraints() |
| 166 | + timeSpanLabelConstraints(topConstraint: topSeparator) |
| 167 | + descriptionLabelConstraints(topConstraint: timeSpanLabel) |
| 168 | + } else if timeSpanLabel.isDescendant(of: contentView) { |
| 169 | + // Only timeSpanLabel, no topSeparator |
| 170 | + timeSpanLabelConstraints(topConstraint: contentView) |
| 171 | + descriptionLabelConstraints(topConstraint: timeSpanLabel) |
| 172 | + } else if let topSeparator = topSeparator { |
| 173 | + // Only topSeparator, no timeSpanLabel |
| 174 | + topSeparatorConstraints() |
| 175 | + descriptionLabelConstraints(topConstraint: topSeparator) |
| 176 | + } else { |
| 177 | + // Neither |
| 178 | + descriptionLabelConstraints(topConstraint: contentView) |
| 179 | + } |
| 180 | + |
| 181 | + if let stackView = affectedRoutesStackView { |
| 182 | + // When both a separator view and stackView are required |
| 183 | + affectedRoutesLabel.snp.remakeConstraints { (make) in |
| 184 | + make.leading.equalTo(descriptionLabel) |
| 185 | + make.top.equalTo(descriptionLabel.snp.bottom).offset(24) |
| 186 | + make.width.equalTo(affectedRoutesLabel.intrinsicContentSize.width) |
| 187 | + make.height.equalTo(affectedRoutesLabel.intrinsicContentSize.height) |
| 188 | + } |
| 189 | + stackView.snp.remakeConstraints { (make) in |
| 190 | + make.top.equalTo(affectedRoutesLabel.snp.bottom).offset(8) |
| 191 | + make.leading.equalTo(descriptionLabel) |
| 192 | + make.trailing.bottom.equalToSuperview().inset(borderInset) |
| 193 | + } |
| 194 | + } else { |
| 195 | + descriptionLabel.snp.makeConstraints { (make) in |
| 196 | + make.bottom.equalToSuperview().inset(borderInset) |
| 197 | + } |
| 198 | + } |
| 199 | + super.updateConstraints() |
| 200 | + } |
| 201 | + |
| 202 | + private func formatTimeString(_ fromDate: String, toDate: String) -> String { |
| 203 | + |
| 204 | + let newformatter = DateFormatter() |
| 205 | + newformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sZZZZ" |
| 206 | + newformatter.locale = Locale(identifier: "en_US_POSIX") |
| 207 | + |
| 208 | + let fromDate = newformatter.date(from: fromDate) |
| 209 | + let toDate = newformatter.date(from: toDate) |
| 210 | + |
| 211 | + let formatter = DateFormatter() |
| 212 | + formatter.dateFormat = "EEEE M/d" |
| 213 | + |
| 214 | + if let unWrappedFromDate = fromDate, let unWrappedToDate = toDate { |
| 215 | + let formattedFromDate = formatter.string(from: unWrappedFromDate) |
| 216 | + let formattedToDate = formatter.string(from: unWrappedToDate) |
| 217 | + |
| 218 | + return "\(formattedFromDate) - \(formattedToDate)" |
| 219 | + } |
| 220 | + |
| 221 | + timeSpanLabel.removeFromSuperview() |
| 222 | + return "Time: Unknown" |
| 223 | + } |
| 224 | + |
| 225 | + private func rowCount() -> Int { |
| 226 | + guard let routes = alert?.routes else { return 0 } |
| 227 | + if routes.count > maxIconsPerRow { |
| 228 | + let addExtra = routes.count % maxIconsPerRow > 0 ? 1 : 0 |
| 229 | + let rowCount = routes.count / maxIconsPerRow |
| 230 | + |
| 231 | + return rowCount + addExtra |
| 232 | + } else { |
| 233 | + return 1 |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + private func getDayOfWeek(_ today:Date) -> Int? { |
| 238 | + let myCalendar = Calendar(identifier: .gregorian) |
| 239 | + let weekDay = myCalendar.component(.weekday, from: today) |
| 240 | + return weekDay |
| 241 | + } |
| 242 | + |
| 243 | + override func prepareForReuse() { |
| 244 | + if let stackView = affectedRoutesStackView { |
| 245 | + stackView.removeFromSuperview() |
| 246 | + } |
| 247 | + |
| 248 | + affectedRoutesStackView = nil |
| 249 | + |
| 250 | + if let routesLabel = affectedRoutesLabel { |
| 251 | + routesLabel.removeFromSuperview() |
| 252 | + } |
| 253 | + |
| 254 | + affectedRoutesLabel = nil |
| 255 | + } |
| 256 | + required init?(coder aDecoder: NSCoder) { |
| 257 | + fatalError("init(coder:) has not been implemented") |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | + |
0 commit comments