|
| 1 | +import UIKit |
| 2 | +import WordPressShared |
| 3 | + |
| 4 | + |
| 5 | +struct StatsTotalInsightsData { |
| 6 | + var count: String |
| 7 | + var comparison: String = "" |
| 8 | +} |
| 9 | + |
| 10 | +class StatsTotalInsightsCell: StatsBaseCell { |
| 11 | + private weak var siteStatsInsightsDelegate: SiteStatsInsightsDelegate? |
| 12 | + private var lastPostInsight: StatsLastPostInsight? |
| 13 | + |
| 14 | + private let outerStackView = UIStackView() |
| 15 | + private let topInnerStackView = UIStackView() |
| 16 | + private let countLabel = UILabel() |
| 17 | + private let comparisonLabel = UILabel() |
| 18 | + private let graphView = UIView() |
| 19 | + |
| 20 | + // MARK: - Initialization |
| 21 | + |
| 22 | + required override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 23 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 24 | + |
| 25 | + configureView() |
| 26 | + } |
| 27 | + |
| 28 | + required init(coder: NSCoder) { |
| 29 | + fatalError() |
| 30 | + } |
| 31 | + |
| 32 | + private func configureView() { |
| 33 | + configureStackViews() |
| 34 | + configureGraphView() |
| 35 | + configureLabels() |
| 36 | + configureConstraints() |
| 37 | + } |
| 38 | + |
| 39 | + private func configureStackViews() { |
| 40 | + outerStackView.translatesAutoresizingMaskIntoConstraints = false |
| 41 | + outerStackView.axis = .vertical |
| 42 | + outerStackView.spacing = Metrics.stackViewSpacing |
| 43 | + contentView.addSubview(outerStackView) |
| 44 | + |
| 45 | + topInnerStackView.axis = .horizontal |
| 46 | + topInnerStackView.spacing = Metrics.stackViewSpacing |
| 47 | + |
| 48 | + topInnerStackView.addArrangedSubviews([countLabel, graphView]) |
| 49 | + outerStackView.addArrangedSubviews([topInnerStackView, comparisonLabel]) |
| 50 | + } |
| 51 | + |
| 52 | + private func configureGraphView() { |
| 53 | + graphView.translatesAutoresizingMaskIntoConstraints = false |
| 54 | + graphView.backgroundColor = .secondarySystemBackground |
| 55 | + graphView.setContentHuggingPriority(.required, for: .horizontal) |
| 56 | + graphView.setContentHuggingPriority(.required, for: .vertical) |
| 57 | + } |
| 58 | + |
| 59 | + private func configureLabels() { |
| 60 | + countLabel.font = WPStyleGuide.Stats.insightsCountFont |
| 61 | + countLabel.textColor = .text |
| 62 | + countLabel.text = "0" |
| 63 | + countLabel.adjustsFontSizeToFitWidth = true |
| 64 | + countLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) |
| 65 | + countLabel.setContentHuggingPriority(.required, for: .vertical) |
| 66 | + |
| 67 | + comparisonLabel.font = .preferredFont(forTextStyle: .body) |
| 68 | + comparisonLabel.textColor = .textSubtle |
| 69 | + comparisonLabel.text = "+87 (40%) compared to last week" |
| 70 | + } |
| 71 | + |
| 72 | + private func configureConstraints() { |
| 73 | + topConstraint = outerStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: StatsBaseCell.Metrics.padding) |
| 74 | + |
| 75 | + NSLayoutConstraint.activate([ |
| 76 | + topConstraint, |
| 77 | + outerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -StatsBaseCell.Metrics.padding), |
| 78 | + outerStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: StatsBaseCell.Metrics.padding), |
| 79 | + outerStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -StatsBaseCell.Metrics.padding), |
| 80 | + graphView.widthAnchor.constraint(equalTo: graphView.heightAnchor, multiplier: 2.55), |
| 81 | + graphView.heightAnchor.constraint(equalTo: countLabel.heightAnchor) |
| 82 | + ]) |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: This will need updating to pass some graph data too. |
| 86 | + // Assuming this will be something like a small array of ints |
| 87 | + func configure(count: String, statSection: StatSection, siteStatsInsightsDelegate: SiteStatsInsightsDelegate?) { |
| 88 | + self.statSection = statSection |
| 89 | + self.siteStatsInsightsDelegate = siteStatsInsightsDelegate |
| 90 | + |
| 91 | + countLabel.text = count |
| 92 | + } |
| 93 | + |
| 94 | + private enum Metrics { |
| 95 | + static let stackViewSpacing: CGFloat = 8.0 |
| 96 | + } |
| 97 | +} |
0 commit comments