|
| 1 | +import Combine |
| 2 | +import Foundation |
| 3 | +import UIKit |
| 4 | + |
| 5 | +/// Shows either a data label for store stats (e.g. order/visitor/conversion stats), or a redacted view when data are unavailable. |
| 6 | +final class StoreStatsDataOrRedactedView: UIView { |
| 7 | + /// State of store stats data UI. |
| 8 | + enum State { |
| 9 | + /// Store stats data are available, and a label is shown. |
| 10 | + case data |
| 11 | + /// Store stats data are unavailable, and a redacted view is shown. |
| 12 | + case redacted |
| 13 | + /// Store stats data are unavailable due to Jetpack-the-plugin, and a redacted view with Jetpack logo is shown. |
| 14 | + case redactedDueToJetpack |
| 15 | + } |
| 16 | + |
| 17 | + @Published var state: State = .data |
| 18 | + @Published var data: String? |
| 19 | + @Published var isHighlighted: Bool = false |
| 20 | + |
| 21 | + private let dataLabel = UILabel() |
| 22 | + private let redactedView = StoreStatsEmptyView() |
| 23 | + private let stackView: UIStackView |
| 24 | + |
| 25 | + private var subscriptions: Set<AnyCancellable> = [] |
| 26 | + |
| 27 | + init() { |
| 28 | + stackView = UIStackView(arrangedSubviews: [dataLabel, redactedView]) |
| 29 | + super.init(frame: .zero) |
| 30 | + |
| 31 | + configureView() |
| 32 | + configureDataLabel() |
| 33 | + observeStateForUIUpdates() |
| 34 | + observeIsHighlightedForLabelTextColor() |
| 35 | + observeDataForLabelText() |
| 36 | + } |
| 37 | + |
| 38 | + required convenience init?(coder: NSCoder) { |
| 39 | + self.init() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +private extension StoreStatsDataOrRedactedView { |
| 44 | + func configureView() { |
| 45 | + translatesAutoresizingMaskIntoConstraints = false |
| 46 | + |
| 47 | + addSubview(stackView) |
| 48 | + stackView.translatesAutoresizingMaskIntoConstraints = false |
| 49 | + pinSubviewToAllEdges(stackView) |
| 50 | + } |
| 51 | + |
| 52 | + func configureDataLabel() { |
| 53 | + dataLabel.font = Constants.statsFont |
| 54 | + dataLabel.textColor = Constants.statsTextColor |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +private extension StoreStatsDataOrRedactedView { |
| 59 | + func observeStateForUIUpdates() { |
| 60 | + $state.sink { [weak self] state in |
| 61 | + self?.updateUI(state: state) |
| 62 | + }.store(in: &subscriptions) |
| 63 | + } |
| 64 | + |
| 65 | + func updateUI(state: State) { |
| 66 | + let isDataLabelShown = state == .data |
| 67 | + dataLabel.isHidden = isDataLabelShown == false |
| 68 | + redactedView.isHidden = !dataLabel.isHidden |
| 69 | + switch state { |
| 70 | + case .redacted, .redactedDueToJetpack: |
| 71 | + redactedView.showJetpackImage = state == .redactedDueToJetpack |
| 72 | + default: |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + func observeDataForLabelText() { |
| 78 | + $data.sink { [weak self] data in |
| 79 | + self?.dataLabel.text = data |
| 80 | + }.store(in: &subscriptions) |
| 81 | + } |
| 82 | + |
| 83 | + func observeIsHighlightedForLabelTextColor() { |
| 84 | + $isHighlighted.sink { [weak self] isHighlighted in |
| 85 | + self?.dataLabel.textColor = isHighlighted ? Constants.statsHighlightTextColor: Constants.statsTextColor |
| 86 | + }.store(in: &subscriptions) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +private extension StoreStatsDataOrRedactedView { |
| 91 | + enum Constants { |
| 92 | + static let statsTextColor: UIColor = .text |
| 93 | + static let statsHighlightTextColor: UIColor = .accent |
| 94 | + static let statsFont: UIFont = .font(forStyle: .title3, weight: .semibold) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#if DEBUG |
| 99 | + |
| 100 | +import SwiftUI |
| 101 | + |
| 102 | +private struct StoreStatsDataOrRedactedViewRepresentable: UIViewRepresentable { |
| 103 | + private let state: StoreStatsDataOrRedactedView.State |
| 104 | + private let isHighlighted: Bool |
| 105 | + private let data: String? |
| 106 | + |
| 107 | + init(state: StoreStatsDataOrRedactedView.State, isHighlighted: Bool = false, data: String? = nil) { |
| 108 | + self.state = state |
| 109 | + self.isHighlighted = isHighlighted |
| 110 | + self.data = data |
| 111 | + } |
| 112 | + |
| 113 | + func makeUIView(context: Context) -> StoreStatsDataOrRedactedView { |
| 114 | + let view = StoreStatsDataOrRedactedView() |
| 115 | + view.state = state |
| 116 | + view.isHighlighted = isHighlighted |
| 117 | + view.data = data |
| 118 | + return view |
| 119 | + } |
| 120 | + |
| 121 | + func updateUIView(_ uiView: StoreStatsDataOrRedactedView, context: Context) { |
| 122 | + uiView.state = state |
| 123 | + uiView.isHighlighted = isHighlighted |
| 124 | + uiView.data = data |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +struct StoreStatsDataOrRedactedView_Previews: PreviewProvider { |
| 129 | + private static func makeStack() -> some View { |
| 130 | + VStack { |
| 131 | + StoreStatsDataOrRedactedViewRepresentable(state: .data, isHighlighted: false, data: "$32.5") |
| 132 | + StoreStatsDataOrRedactedViewRepresentable(state: .redacted) |
| 133 | + StoreStatsDataOrRedactedViewRepresentable(state: .redactedDueToJetpack, isHighlighted: false) |
| 134 | + } |
| 135 | + .background(Color(UIColor.systemBackground)) |
| 136 | + } |
| 137 | + |
| 138 | + static var previews: some View { |
| 139 | + Group { |
| 140 | + makeStack() |
| 141 | + .previewLayout(.fixed(width: 100, height: 150)) |
| 142 | + .previewDisplayName("Light") |
| 143 | + |
| 144 | + makeStack() |
| 145 | + .previewLayout(.fixed(width: 100, height: 150)) |
| 146 | + .environment(\.colorScheme, .dark) |
| 147 | + .previewDisplayName("Dark") |
| 148 | + |
| 149 | + makeStack() |
| 150 | + .previewLayout(.fixed(width: 100, height: 400)) |
| 151 | + .environment(\.sizeCategory, .accessibilityExtraExtraExtraLarge) |
| 152 | + .previewDisplayName("Large Font") |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#endif |
0 commit comments