-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CommentPostFailedCell.swift
127 lines (100 loc) · 3.68 KB
/
CommentPostFailedCell.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
119
120
121
122
123
124
125
126
127
import KsApi
import Library
import Prelude
import UIKit
final class CommentPostFailedCell: UITableViewCell, ValueCell {
// MARK: - Properties
private let viewModel = CommentCellViewModel()
private lazy var bodyTextView: UITextView = { UITextView(frame: .zero) }()
private lazy var commentCellHeaderStackView: CommentCellHeaderStackView = {
CommentCellHeaderStackView(frame: .zero)
}()
private lazy var rootStackView = {
UIStackView(frame: .zero)
|> \.translatesAutoresizingMaskIntoConstraints .~ false
}()
private lazy var retryButton = { UIButton(frame: .zero)
|> \.isUserInteractionEnabled .~ false
}()
// MARK: - Lifecycle
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setupConstraints()
self.bindStyles()
self.configureViews()
self.bindViewModel()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: - Styles
override func bindStyles() {
super.bindStyles()
_ = self
|> baseTableViewCellStyle()
_ = self.rootStackView
|> commentCellRootStackViewStyle
_ = self.bodyTextView
|> commentBodyTextViewStyle
|> \.textColor .~ .ksr_support_400
self.viewModel.inputs.bindStyles()
}
// MARK: - Configuration
internal func configureWith(value: Comment) {
self.commentCellHeaderStackView
.configureWith(comment: value)
self.viewModel.inputs.configureWith(comment: value, project: nil)
}
private func configureViews() {
_ = ([self.commentCellHeaderStackView, self.bodyTextView, self.retryButton], self.rootStackView)
|> ksr_addArrangedSubviewsToStackView()
}
private func setupConstraints() {
_ = (self.rootStackView, self.contentView)
|> ksr_addSubviewToParent()
|> ksr_constrainViewToMarginsInParent()
}
// MARK: - View model
internal override func bindViewModel() {
self.bodyTextView.rac.text = self.viewModel.outputs.body
self.viewModel.outputs.commentStatus
.observeForUI()
.observeValues { [weak self] status in
guard let self = self else { return }
_ = self.retryButton
|> status == .retrying ? postingButtonStyle : retryButtonStyle
}
self.viewModel.outputs.shouldIndentContent
.observeForUI()
.observeValues { shouldIndent in
guard shouldIndent else { return }
_ = self.rootStackView
|> commentCellIndentedRootStackViewStyle
}
}
}
// MARK: Styles
private let retryButtonStyle: ButtonStyle = { button in
button
|> UIButton.lens
.title(for: .normal) %~
{ _ in Strings.Couldnt_post() }
|> UIButton.lens.titleLabel.font .~ UIFont.ksr_subhead()
|> UIButton.lens.image(for: .normal) .~ Library.image(named: "circle-back")
|> UIButton.lens.titleColor(for: .normal) .~ UIColor.ksr_celebrate_700
|> UIButton.lens.tintColor .~ UIColor.ksr_celebrate_700
|> UIButton.lens.titleEdgeInsets .~ UIEdgeInsets(left: Styles.grid(1))
|> UIButton.lens.contentHorizontalAlignment .~ .left
|> UIButton.lens.titleLabel.numberOfLines .~ 0
}
private let postingButtonStyle: ButtonStyle = { button in
button
|> UIButton.lens.title(for: .normal) %~ { _ in Strings.Posting() }
|> UIButton.lens.titleLabel.font .~ UIFont.ksr_subhead()
|> UIButton.lens.image(for: .normal) .~ Library.image(named: "circle-back")?
.withRenderingMode(.alwaysTemplate)
|> UIButton.lens.titleColor(for: .normal) .~ UIColor.ksr_support_400
|> UIButton.lens.tintColor .~ UIColor.ksr_support_400
|> UIButton.lens.titleEdgeInsets .~ UIEdgeInsets(left: Styles.grid(1))
|> UIButton.lens.contentHorizontalAlignment .~ .left
}