-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PledgeAmountCell.swift
118 lines (95 loc) · 4.12 KB
/
PledgeAmountCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import Library
import Prelude
import Prelude_UIKit
import UIKit
final class PledgeAmountCell: UITableViewCell, ValueCell {
// MARK: - Properties
private lazy var amountInputView: AmountInputView = { AmountInputView(frame: .zero) }()
private lazy var inputStackView: UIStackView = { UIStackView(frame: .zero) }()
private lazy var label: UILabel = { UILabel(frame: .zero) }()
private lazy var rootStackView: UIStackView = { UIStackView(frame: .zero) }()
private lazy var spacer: UIView = {
UIView(frame: .zero)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
} ()
private lazy var stepper: UIStepper = { UIStepper(frame: .zero) }()
// MARK: - Lifecycle
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
_ = self
|> \.accessibilityElements .~ [self.label, self.stepper, self.amountInputView]
|> \.backgroundColor .~ UIColor.ksr_grey_300
_ = (self.rootStackView, self.contentView)
|> ksr_addSubviewToParent()
|> ksr_constrainViewToEdgesInParent()
self.rootStackView.addArrangedSubview(self.label)
self.rootStackView.addArrangedSubview(self.inputStackView)
self.inputStackView.addArrangedSubview(self.stepper)
self.inputStackView.addArrangedSubview(spacer)
self.inputStackView.addArrangedSubview(self.amountInputView)
self.spacer.widthAnchor.constraint(greaterThanOrEqualToConstant: Styles.grid(3)).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Styles
override func bindStyles() {
super.bindStyles()
_ = self.inputStackView
|> inputStackViewStyle(self.traitCollection.ksr_isAccessibilityCategory())
_ = self.label
|> labelStyle
_ = self.rootStackView
|> rootStackViewStyle
_ = self.stepper
|> stepperStyle
}
// MARK: - Configuration
func configureWith(value: (amount: Double, currency: String)) {
self.amountInputView.configureWith(
amount: String(format: "%i", value.amount),
placeholder: "\(0)",
currency: value.currency
)
}
}
// MARK: - Styles
private func inputStackViewStyle(_ isAccessibilityCategory: Bool) -> ((UIStackView) -> UIStackView) {
return { (stackView: UIStackView) in
let alignment: UIStackView.Alignment = (isAccessibilityCategory ? .leading : .center)
let axis: NSLayoutConstraint.Axis = (isAccessibilityCategory ? .vertical : .horizontal)
let distribution: UIStackView.Distribution = (isAccessibilityCategory ? .equalSpacing : .fill)
let spacing: CGFloat = (isAccessibilityCategory ? Styles.grid(1) : 0)
return stackView
|> \.alignment .~ alignment
|> \.axis .~ axis
|> \.distribution .~ distribution
|> \.spacing .~ spacing
}
}
private let labelStyle: LabelStyle = { (label: UILabel) in
label
|> \.adjustsFontForContentSizeCategory .~ true
|> \.font .~ UIFont.ksr_headline()
|> \.numberOfLines .~ 0
|> \.text %~ { _ in Strings.Your_pledge_amount() }
}
private let rootStackViewStyle: StackViewStyle = { (stackView: UIStackView) in
stackView
|> \.axis .~ NSLayoutConstraint.Axis.vertical
|> \.isLayoutMarginsRelativeArrangement .~ true
|> \.layoutMargins .~ UIEdgeInsets(
top: Styles.grid(2), left: Styles.grid(4), bottom: Styles.grid(3), right: Styles.grid(4)
)
|> \.spacing .~ (Styles.grid(1) + Styles.gridHalf(1))
}
private func stepperStyle(_ stepper: UIStepper) -> UIStepper {
return stepper
|> \.tintColor .~ UIColor.clear
<> UIStepper.lens.decrementImage(for: .normal) .~ image(named: "stepper-decrement-normal")
<> UIStepper.lens.decrementImage(for: .disabled) .~ image(named: "stepper-decrement-disabled")
<> UIStepper.lens.decrementImage(for: .highlighted) .~ image(named: "stepper-decrement-highlighted")
<> UIStepper.lens.incrementImage(for: .normal) .~ image(named: "stepper-increment-normal")
<> UIStepper.lens.incrementImage(for: .disabled) .~ image(named: "stepper-increment-disabled")
<> UIStepper.lens.incrementImage(for: .highlighted) .~ image(named: "stepper-increment-highlighted")
}