Newer
Older
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import UIKit
import Shared
import Combine
import CasePaths
import Voxophone
import ComposableArchitecture
public final class ChatInputView: UIToolbar {
public init(store: Store<ChatInputState, ChatInputAction>) {
self.store = store
self.viewStore = ViewStore(store)
super.init(frame: .zero)
setup()
observeStore()
setupUIActions()
viewStore.send(.setup)
}
required init?(coder: NSCoder) { nil }
deinit {
viewStore.send(.destroy)
}
public func setMaxHeight(_ function: @escaping () -> CGFloat) {
text.maxHeight = function
}
public func setupReply(message: String, sender: String) {
viewStore.send(.text(.didTriggerReply(message, sender)))
}
let store: Store<ChatInputState, ChatInputAction>
let viewStore: ViewStore<ChatInputState, ChatInputAction>
private var cancellables: Set<AnyCancellable> = []
let stack = UIStackView()
let text = TextInputView()
let audio = AudioView()
let actions = ActionsView()
private func setup() {
isTranslucent = false
translatesAutoresizingMaskIntoConstraints = false
barTintColor = Asset.neutralWhite.color
stack.axis = .vertical
stack.spacing = 8
stack.addArrangedSubview(text)
stack.addArrangedSubview(audio)
stack.addArrangedSubview(actions)
addSubview(stack)
stack.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 8),
stack.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 8),
stack.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -8),
stack.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -8),
])
}
private func observeStore() {
viewStore.publisher
.map(\.isPresentingActions)
.combineLatest(viewStore.publisher.map(\.canAddAttachments))
.sink { [unowned self] isPresentingActions, canAddAttachments in
if canAddAttachments {
text.showActionsButton.isHidden = isPresentingActions
text.hideActionsButton.isHidden = !isPresentingActions
actions.isHidden = !isPresentingActions
} else {
text.showActionsButton.isHidden = true
text.hideActionsButton.isHidden = true
actions.isHidden = true
}
}
.store(in: &cancellables)
viewStore.publisher
.map(\.reply)
.sink { [unowned self] reply in
guard let reply = reply else {
text.replyView.isHidden = true
return
}
text.replyView.isHidden = false
text.replyView.messageLabel.text = reply.text
text.replyView.nameLabel.text = reply.name
}.store(in: &cancellables)
viewStore.publisher
.map(\.audio)
.map { $0 != nil }
.sink { [unowned self] in
text.isHidden = $0
audio.isHidden = !$0
}
.store(in: &cancellables)
viewStore.publisher
.map(\.text.isEmpty)
.combineLatest(viewStore.publisher.map(\.canAddAttachments))
.sink { [unowned self] textIsEmpty, canAddAttachments in
if canAddAttachments {
text.sendButton.isHidden = textIsEmpty
text.audioButton.isHidden = !textIsEmpty
} else {
text.sendButton.isHidden = false
text.audioButton.isHidden = true
}
text.sendButton.isEnabled = !textIsEmpty
text.placeholderView.isHidden = !textIsEmpty
}
.store(in: &cancellables)
viewStore.publisher
.map(\.text)
.sink { [unowned self] in
if text.textView.markedTextRange == nil {
text.textView.text = $0
} else if $0 == "" {
text.textView.text = $0
}
text.updateHeight()
}.store(in: &cancellables)
let timeFormatter = DateComponentsFormatter()
timeFormatter.unitsStyle = .positional
timeFormatter.allowedUnits = [.minute, .second]
timeFormatter.zeroFormattingBehavior = .pad
viewStore.publisher
.map(\.audio)
.sink { [unowned self] in
switch $0 {
case let .idle(_, duration):
audio.playButton.isHidden = false
audio.stopPlaybackButton.isHidden = true
audio.stopRecordingButton.isHidden = true
audio.sendButton.isHidden = false
audio.timeLabel.text = timeFormatter.string(from: duration)
case let .recording(_, time):
audio.playButton.isHidden = true
audio.stopPlaybackButton.isHidden = true
audio.stopRecordingButton.isHidden = false
audio.sendButton.isHidden = true
audio.timeLabel.text = timeFormatter.string(from: time)
case let .playing(_, _, time):
audio.playButton.isHidden = true
audio.stopPlaybackButton.isHidden = false
audio.stopRecordingButton.isHidden = true
audio.sendButton.isHidden = false
audio.timeLabel.text = timeFormatter.string(from: time)
case .none:
audio.playButton.isHidden = true
audio.stopPlaybackButton.isHidden = true
audio.stopRecordingButton.isHidden = true
audio.sendButton.isHidden = true
audio.timeLabel.text = ""
}
}
.store(in: &cancellables)
}
private func setupUIActions() {
text.textDidChange = { [unowned self] text in viewStore.send(.text(.didUpdate(text))) }
text.replyView.abortButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.text(.didTapAbortReply)) }
.store(in: &cancellables)
text.showActionsButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.text(.didTapShowActions)) }
.store(in: &cancellables)
text.hideActionsButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.text(.didTapHideActions)) }
.store(in: &cancellables)
text.sendButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.text(.didTapSend)) }
.store(in: &cancellables)
text.audioButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.text(.didTapAudio)) }
.store(in: &cancellables)
audio.cancelButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.audio(.didTapCancel)) }
.store(in: &cancellables)
audio.playButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.audio(.didTapPlay)) }
.store(in: &cancellables)
audio.stopPlaybackButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.audio(.didTapStopPlayback)) }
.store(in: &cancellables)
audio.stopRecordingButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.audio(.didTapStopRecording)) }
.store(in: &cancellables)
audio.sendButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.audio(.didTapSend)) }
.store(in: &cancellables)
actions.libraryButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.actions(.didTapLibrary)) }
.store(in: &cancellables)
actions.cameraButton.publisher(for: .touchUpInside)
.sink { [unowned self] in viewStore.send(.actions(.didTapCamera)) }
.store(in: &cancellables)
}
}