|
1 | 1 | import UIKit |
| 2 | +import MixinServices |
2 | 3 |
|
3 | 4 | final class ShareMarketAsPictureView: UIView { |
4 | 5 |
|
5 | | - // Manipulating UIImage to render on CGContext is quite cumbersome, and the behavior varies |
6 | | - // across different iOS versions. Therefore, an invisible view is used to capture a screenshot |
7 | | - // to generate the image for sharing. |
| 6 | + @IBOutlet weak var chartSectionView: UIView! |
| 7 | + @IBOutlet weak var titleStackView: UIStackView! |
| 8 | + @IBOutlet weak var titleLabel: UILabel! |
| 9 | + @IBOutlet weak var rankLabel: InsetLabel! |
| 10 | + @IBOutlet weak var priceLabel: UILabel! |
| 11 | + @IBOutlet weak var changeLabel: MarketColoredLabel! |
| 12 | + @IBOutlet weak var tokenIconView: PlainTokenIconView! |
| 13 | + @IBOutlet weak var chartView: ChartView! |
| 14 | + @IBOutlet weak var periodSelectorStackView: UIStackView! |
| 15 | + @IBOutlet weak var marketCapTitleLabel: UILabel! |
| 16 | + @IBOutlet weak var marketCapContentLabel: UILabel! |
| 17 | + @IBOutlet weak var volumeTitleLabel: UILabel! |
| 18 | + @IBOutlet weak var volumeContentLabel: UILabel! |
| 19 | + @IBOutlet weak var highTitleLabel: UILabel! |
| 20 | + @IBOutlet weak var highContentLabel: UILabel! |
| 21 | + @IBOutlet weak var lowTitleLabel: UILabel! |
| 22 | + @IBOutlet weak var lowContentLabel: UILabel! |
| 23 | + @IBOutlet weak var periodSelectorHeightConstraint: NSLayoutConstraint! |
| 24 | + |
| 25 | + @IBOutlet weak var statisticsSectionView: UIView! |
| 26 | + @IBOutlet weak var nameLabel: UILabel! |
| 27 | + @IBOutlet weak var statisticsStackView: UIStackView! |
8 | 28 |
|
9 | | - @IBOutlet weak var screenshotWrapperView: UIView! |
10 | | - @IBOutlet weak var screenshotImageView: UIImageView! |
11 | | - @IBOutlet weak var displayImageView: UIImageView! |
12 | 29 | @IBOutlet weak var obiView: ShareObiView! |
13 | 30 |
|
| 31 | + private weak var unavailableView: UIView? |
| 32 | + |
| 33 | + private var sectionViews: [UIView] { |
| 34 | + [chartSectionView, statisticsSectionView] |
| 35 | + } |
| 36 | + |
| 37 | + private var statsTitleLabels: [UILabel] { |
| 38 | + [ |
| 39 | + marketCapTitleLabel, |
| 40 | + volumeTitleLabel, |
| 41 | + highTitleLabel, |
| 42 | + lowTitleLabel, |
| 43 | + ] |
| 44 | + } |
| 45 | + |
| 46 | + private var statsContentLabels: [UILabel] { |
| 47 | + [ |
| 48 | + marketCapContentLabel, |
| 49 | + volumeContentLabel, |
| 50 | + highContentLabel, |
| 51 | + lowContentLabel, |
| 52 | + ] |
| 53 | + } |
| 54 | + |
14 | 55 | override func awakeFromNib() { |
15 | 56 | super.awakeFromNib() |
| 57 | + for sectionView in sectionViews { |
| 58 | + sectionView.layer.cornerRadius = 8 |
| 59 | + sectionView.layer.masksToBounds = true |
| 60 | + } |
| 61 | + titleStackView.setCustomSpacing(9, after: titleLabel) |
| 62 | + titleLabel.font = .systemFont( |
| 63 | + ofSize: 14, |
| 64 | + weight: .accessiblityBoldTextCounterWeight(.regular) |
| 65 | + ) |
| 66 | + rankLabel.contentInset = UIEdgeInsets(top: 1, left: 4, bottom: 1, right: 4) |
| 67 | + rankLabel.layer.masksToBounds = true |
| 68 | + rankLabel.layer.cornerRadius = 4 |
| 69 | + rankLabel.font = .systemFont( |
| 70 | + ofSize: 12, |
| 71 | + weight: .accessiblityBoldTextCounterWeight(.medium) |
| 72 | + ) |
| 73 | + priceLabel.font = .systemFont( |
| 74 | + ofSize: 22, |
| 75 | + weight: .accessiblityBoldTextCounterWeight(.medium) |
| 76 | + ) |
| 77 | + changeLabel.font = .systemFont( |
| 78 | + ofSize: 14, |
| 79 | + weight: .accessiblityBoldTextCounterWeight(.regular) |
| 80 | + ) |
| 81 | + chartView.annotateExtremums = true |
| 82 | + chartView.minPointPosition = 135 / 184 |
| 83 | + chartView.maxPointPosition = 23 / 184 |
| 84 | + chartView.delegate = self |
| 85 | + for (i, period) in PriceHistoryPeriod.allCases.enumerated() { |
| 86 | + let label = InsetLabel() |
| 87 | + label.tag = i |
| 88 | + label.font = .systemFont( |
| 89 | + ofSize: 14, |
| 90 | + weight: .accessiblityBoldTextCounterWeight(.regular) |
| 91 | + ) |
| 92 | + label.textAlignment = .center |
| 93 | + label.text = switch period { |
| 94 | + case .day: |
| 95 | + R.string.localizable.days_count_short(1) |
| 96 | + case .week: |
| 97 | + R.string.localizable.weeks_count_short(1) |
| 98 | + case .month: |
| 99 | + R.string.localizable.months_count_short(1) |
| 100 | + case .year: |
| 101 | + R.string.localizable.years_count_short(1) |
| 102 | + case .all: |
| 103 | + R.string.localizable.all() |
| 104 | + } |
| 105 | + label.layer.cornerRadius = periodSelectorHeightConstraint.constant / 2 |
| 106 | + label.layer.masksToBounds = true |
| 107 | + label.contentInset = UIEdgeInsets(top: 4, left: 10, bottom: 4, right: 10) |
| 108 | + periodSelectorStackView.addArrangedSubview(label) |
| 109 | + } |
| 110 | + |
| 111 | + nameLabel.font = .systemFont( |
| 112 | + ofSize: 14, |
| 113 | + weight: .accessiblityBoldTextCounterWeight(.regular) |
| 114 | + ) |
| 115 | + for label in statsTitleLabels { |
| 116 | + label.font = .systemFont( |
| 117 | + ofSize: 12, |
| 118 | + weight: .accessiblityBoldTextCounterWeight(.regular) |
| 119 | + ) |
| 120 | + } |
| 121 | + marketCapTitleLabel.text = R.string.localizable.market_cap().uppercased() |
| 122 | + volumeTitleLabel.text = R.string.localizable.volume_24h().uppercased() |
| 123 | + highTitleLabel.text = R.string.localizable.high_24h().uppercased() |
| 124 | + lowTitleLabel.text = R.string.localizable.low_24h().uppercased() |
| 125 | + for label in statsContentLabels { |
| 126 | + label.font = .systemFont( |
| 127 | + ofSize: 14, |
| 128 | + weight: .accessiblityBoldTextCounterWeight(.regular) |
| 129 | + ) |
| 130 | + } |
16 | 131 | obiView.load(content: .installMixin(gradient: true)) |
17 | 132 | } |
18 | 133 |
|
19 | | - func setImage(_ image: UIImage) { |
20 | | - let ratio = image.size.width / image.size.height |
21 | | - screenshotImageView.image = image |
22 | | - screenshotImageView.snp.makeConstraints { make in |
23 | | - make.width.equalTo(screenshotImageView.snp.height).multipliedBy(ratio) |
| 134 | + func showUnavailableView() { |
| 135 | + let unavailableView: UIView |
| 136 | + if let view = self.unavailableView { |
| 137 | + unavailableView = view |
| 138 | + } else { |
| 139 | + unavailableView = PriceDataUnavailableView() |
| 140 | + addSubview(unavailableView) |
| 141 | + unavailableView.snp.makeConstraints { make in |
| 142 | + make.top.equalTo(titleStackView.snp.bottom).offset(24) |
| 143 | + make.leading.equalToSuperview().offset(16) |
| 144 | + make.trailing.equalToSuperview().offset(-16) |
| 145 | + make.bottom.equalToSuperview().offset(-20) |
| 146 | + } |
| 147 | + self.unavailableView = unavailableView |
24 | 148 | } |
25 | | - displayImageView.image = image |
26 | | - displayImageView.snp.remakeConstraints { make in |
27 | | - make.width.equalTo(displayImageView.snp.height).multipliedBy(ratio).priority(.low) |
| 149 | + unavailableView.isHidden = false |
| 150 | + periodSelectorStackView.isHidden = true |
| 151 | + } |
| 152 | + |
| 153 | + func hideUnavailableView() { |
| 154 | + unavailableView?.isHidden = true |
| 155 | + periodSelectorStackView.isHidden = false |
| 156 | + } |
| 157 | + |
| 158 | + func setPeriodSelection(index: Int) { |
| 159 | + for case let label as UILabel in periodSelectorStackView.arrangedSubviews { |
| 160 | + if label.tag == index { |
| 161 | + label.backgroundColor = R.color.background_secondary() |
| 162 | + label.textColor = R.color.text() |
| 163 | + } else { |
| 164 | + label.backgroundColor = .clear |
| 165 | + label.textColor = R.color.text_quaternary() |
| 166 | + } |
28 | 167 | } |
29 | 168 | } |
30 | 169 |
|
31 | 170 | } |
| 171 | + |
| 172 | +extension ShareMarketAsPictureView: ChartView.Delegate { |
| 173 | + |
| 174 | + func chartView(_ view: ChartView, extremumAnnotationForPoint point: ChartView.Point) -> String { |
| 175 | + CurrencyFormatter.localizedString( |
| 176 | + from: point.value * Currency.current.decimalRate, |
| 177 | + format: .fiatMoneyPrice, |
| 178 | + sign: .never, |
| 179 | + symbol: .currencySymbol |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + func chartView(_ view: ChartView, inspectionAnnotationForPoint point: ChartView.Point) -> String { |
| 184 | + "" |
| 185 | + } |
| 186 | + |
| 187 | + func chartView(_ view: ChartView, didSelectPoint point: ChartView.Point) { |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + func chartViewDidCancelSelection(_ view: ChartView) { |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments