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
import UIKit
import Shared
import Combine
import Voxophone
import AVFoundation
struct CellFactory {
var canBuild: (ChatItem) -> Bool
var build: (ChatItem, UICollectionView, IndexPath) -> UICollectionViewCell
func callAsFunction(
item: ChatItem,
collectionView: UICollectionView,
indexPath: IndexPath
) -> UICollectionViewCell {
build(item, collectionView, indexPath)
}
}
extension CellFactory {
static func combined(factories: [CellFactory]) -> Self {
.init(
canBuild: { _ in true },
build: { item, collectionView, indexPath in
guard let factory = factories.first(where: { $0.canBuild(item)}) else {
fatalError("Couldn't find a factory for \(item). Did you forget to implement?")
}
return factory(
item: item,
collectionView: collectionView,
indexPath: indexPath
)
}
)
}
}
extension CellFactory {
static func incomingAudio(
voxophone: Voxophone
) -> Self {
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
fatalError()
// .init(
// canBuild: { item in
// (item.status == .received || item.status == .receiving)
// && item.payload.reply == nil
//
// }, build: { item, collectionView, indexPath in
// guard let attachment = item.payload.attachment else { fatalError() }
//
// let cell: IncomingAudioCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// let url = FileManager.url(for: "\(attachment.name).\(attachment._extension.written)")!
//
// var model = AudioMessageCellState(
// date: item.date,
// audioURL: url,
// isPlaying: false,
// transferProgress: attachment.progress,
// isLoudspeaker: false,
// duration: (try? AVAudioPlayer(contentsOf: url).duration) ?? 0.0,
// playbackTime: 0.0
// )
//
// cell.leftView.setup(with: model)
// cell.canReply = false
// cell.performReply = {}
//
// Bubbler.build(audioBubble: cell.leftView, with: item)
//
// voxophone.$state
// .sink {
// switch $0 {
// case .playing(url, _, time: let time, _):
// model.isPlaying = true
// model.playbackTime = time
// default:
// model.isPlaying = false
// model.playbackTime = 0.0
// }
//
// model.isLoudspeaker = $0.isLoudspeaker
//
// cell.leftView.setup(with: model)
// }.store(in: &cell.leftView.cancellables)
//
// cell.leftView.didTapRight = {
// guard item.status != .receiving else { return }
//
// voxophone.toggleLoudspeaker()
// }
//
// cell.leftView.didTapLeft = {
// guard item.status != .receiving else { return }
//
// if case .playing(url, _, _, _) = voxophone.state {
// voxophone.reset()
// } else {
// voxophone.load(url)
// voxophone.play()
// }
// }
//
// return cell
// }
// )
}
static func outgoingAudio(
voxophone: Voxophone
) -> Self {
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
fatalError()
// .init(
// canBuild: { item in
// (item.status == .sent ||
// item.status == .sending ||
// item.status == .sendingFailed ||
// item.status == .sendingTimedOut)
// && item.payload.reply == nil
// && item.payload.attachment != nil
// && item.payload.attachment?._extension == .audio
//
// }, build: { item, collectionView, indexPath in
// guard let attachment = item.payload.attachment else { fatalError() }
//
// let cell: OutgoingAudioCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// let url = FileManager.url(for: "\(attachment.name).\(attachment._extension.written)")!
// var model = AudioMessageCellState(
// date: item.date,
// audioURL: url,
// isPlaying: false,
// transferProgress: attachment.progress,
// isLoudspeaker: false,
// duration: (try? AVAudioPlayer(contentsOf: url).duration) ?? 0.0,
// playbackTime: 0.0
// )
//
// cell.rightView.setup(with: model)
// cell.canReply = false
// cell.performReply = {}
//
// Bubbler.build(audioBubble: cell.rightView, with: item)
//
// voxophone.$state
// .sink {
// switch $0 {
// case .playing(url, _, time: let time, _):
// model.isPlaying = true
// model.playbackTime = time
// default:
// model.isPlaying = false
// model.playbackTime = 0.0
// }
//
// model.isLoudspeaker = $0.isLoudspeaker
//
// cell.rightView.setup(with: model)
// }.store(in: &cell.rightView.cancellables)
//
// cell.rightView.didTapRight = {
// voxophone.toggleLoudspeaker()
// }
//
// cell.rightView.didTapLeft = {
// if case .playing(url, _, _, _) = voxophone.state {
// voxophone.reset()
// } else {
// voxophone.load(url)
// voxophone.play()
// }
// }
//
// return cell
// }
// )
}
}
extension CellFactory {
static func outgoingImage() -> Self {
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
fatalError()
// .init(
// canBuild: { item in
// (item.status == .sent ||
// item.status == .sending ||
// item.status == .sendingFailed ||
// item.status == .sendingTimedOut)
// && item.payload.reply == nil
// && item.payload.attachment != nil
// && item.payload.attachment?._extension == .image
//
// }, build: { item, collectionView, indexPath in
// guard let attachment = item.payload.attachment else { fatalError() }
//
// let cell: OutgoingImageCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
//
// Bubbler.build(imageBubble: cell.rightView, with: item)
//
// cell.canReply = false
// cell.performReply = {}
//
// if let image = UIImage(data: attachment.data!) {
// cell.rightView.imageView.image = UIImage(cgImage: image.cgImage!, scale: image.scale, orientation: .up)
// }
//
// return cell
// }
// )
fatalError()
// .init(
// canBuild: { item in
// (item.status == .received || item.status == .receiving)
// && item.payload.reply == nil
// && item.payload.attachment != nil
// && item.payload.attachment?._extension == .image
//
// }, build: { item, collectionView, indexPath in
// guard let attachment = item.payload.attachment else { fatalError() }
//
// let cell: IncomingImageCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
//
// Bubbler.build(imageBubble: cell.leftView, with: item)
// cell.canReply = false
// cell.performReply = {}
// cell.leftView.imageView.image = UIImage(data: attachment.data!)
// return cell
// }
// )
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
}
}
extension CellFactory {
static func outgoingReply(
performReply: @escaping () -> Void,
name: @escaping (Data) -> String,
text: @escaping (Data) -> String,
showRound: @escaping (String?) -> Void
) -> Self {
.init(
canBuild: { item in
(item.status == .sent || item.status == .sending)
&& item.payload.reply != nil
}, build: { item, collectionView, indexPath in
let cell: OutgoingReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
Bubbler.buildReply(
bubble: cell.rightView,
with: item,
reply: .init(
text: text(item.payload.reply!.messageId),
sender: name(item.payload.reply!.senderId)
)
)
cell.performReply = performReply
cell.rightView.didTapShowRound = { showRound(item.roundURL) }
return cell
}
)
}
static func incomingReply(
performReply: @escaping () -> Void,
name: @escaping (Data) -> String,
text: @escaping (Data) -> String,
showRound: @escaping (String?) -> Void
) -> Self {
.init(
canBuild: { item in
&& item.payload.reply != nil
}, build: { item, collectionView, indexPath in
let cell: IncomingReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
Bubbler.buildReply(
bubble: cell.leftView,
with: item,
reply: .init(
text: text(item.payload.reply!.messageId),
sender: name(item.payload.reply!.senderId)
)
)
cell.performReply = performReply
cell.leftView.didTapShowRound = { showRound(item.roundURL) }
cell.leftView.revertBottomStackOrder()
return cell
}
)
}
static func outgoingFailedReply(
performReply: @escaping () -> Void,
name: @escaping (Data) -> String,
text: @escaping (Data) -> String
) -> Self {
.init(
canBuild: { item in
(item.status == .sendingFailed || item.status == .sendingTimedOut)
&& item.payload.reply != nil
}, build: { item, collectionView, indexPath in
let cell: OutgoingFailedReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
Bubbler.buildReply(
bubble: cell.rightView,
with: item,
reply: .init(
text: text(item.payload.reply!.messageId),
sender: name(item.payload.reply!.senderId)
)
)
cell.performReply = performReply
return cell
}
)
}
}
extension CellFactory {
static func incomingText(
performReply: @escaping () -> Void,
showRound: @escaping (String?) -> Void
) -> Self {
.init(
canBuild: { item in
&& item.payload.reply == nil
}, build: { item, collectionView, indexPath in
let cell: IncomingTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
Bubbler.build(bubble: cell.leftView, with: item)
cell.performReply = performReply
cell.leftView.didTapShowRound = { showRound(item.roundURL) }
cell.leftView.revertBottomStackOrder()
return cell
}
)
}
static func outgoingText(
performReply: @escaping () -> Void,
showRound: @escaping (String?) -> Void
) -> Self {
.init(
canBuild: { item in
(item.status == .sending || item.status == .sent)
&& item.payload.reply == nil
}, build: { item, collectionView, indexPath in
let cell: OutgoingTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
Bubbler.build(bubble: cell.rightView, with: item)
cell.performReply = performReply
cell.rightView.didTapShowRound = { showRound(item.roundURL) }
return cell
}
)
}
static func outgoingFailedText(performReply: @escaping () -> Void) -> Self {
.init(
canBuild: { item in
(item.status == .sendingFailed || item.status == .sendingTimedOut)
&& item.payload.reply == nil
}, build: { item, collectionView, indexPath in
let cell: OutgoingFailedTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
Bubbler.build(bubble: cell.rightView, with: item)
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
cell.performReply = performReply
return cell
}
)
}
}
struct ActionFactory {
enum Action {
case copy
case retry
case reply
case delete
var title: String {
switch self {
case .copy:
return Localized.Chat.BubbleMenu.copy
case .retry:
return Localized.Chat.BubbleMenu.retry
case .reply:
return Localized.Chat.BubbleMenu.reply
case .delete:
return Localized.Chat.BubbleMenu.delete
}
}
}
static func build(
from item: ChatItem,
action: Action,
closure: @escaping (ChatItem) -> Void
) -> UIAction? {
switch action {
case .reply:
guard item.status == .received || item.status == .sent else { return nil }
guard item.status == .sendingFailed || item.status == .sendingTimedOut else { return nil }
case .delete, .copy:
break
}
return UIAction(
title: action.title,
state: .off,
handler: { _ in closure(item) }