-
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathComposeContentViewController.swift
More file actions
697 lines (609 loc) · 31.3 KB
/
ComposeContentViewController.swift
File metadata and controls
697 lines (609 loc) · 31.3 KB
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
226
227
228
229
230
231
232
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//
// ComposeContentViewController.swift
//
//
// Created by MainasuK on 22/9/30.
//
import os.log
import UIKit
import SwiftUI
import Combine
import PhotosUI
import MastodonCore
import NaturalLanguage
public final class ComposeContentViewController: UIViewController {
static let minAutoCompleteVisibleHeight: CGFloat = 100
let logger = Logger(subsystem: "ComposeContentViewController", category: "ViewController")
var disposeBag = Set<AnyCancellable>()
public var viewModel: ComposeContentViewModel!
private(set) lazy var composeContentToolbarViewModel = ComposeContentToolbarView.ViewModel(delegate: self)
// tableView container
let tableView: ComposeTableView = {
let tableView = ComposeTableView()
tableView.estimatedRowHeight = UITableView.automaticDimension
tableView.alwaysBounceVertical = true
tableView.separatorStyle = .none
tableView.tableFooterView = UIView()
return tableView
}()
// auto complete
private(set) lazy var autoCompleteViewController: AutoCompleteViewController = {
let viewController = AutoCompleteViewController()
viewController.viewModel = AutoCompleteViewModel(context: viewModel.context, authContext: viewModel.authContext)
viewController.delegate = self
// viewController.viewModel.customEmojiViewModel.value = viewModel.customEmojiViewModel
return viewController
}()
// toolbar
lazy var composeContentToolbarView = ComposeContentToolbarView(viewModel: composeContentToolbarViewModel)
var composeContentToolbarViewBottomLayoutConstraint: NSLayoutConstraint!
let composeContentToolbarBackgroundView = UIView()
// media picker
static func createPhotoLibraryPickerConfiguration(selectionLimit: Int = 4) -> PHPickerConfiguration {
var configuration = PHPickerConfiguration()
configuration.filter = .any(of: [.images, .videos])
configuration.selectionLimit = selectionLimit
return configuration
}
public private(set) lazy var photoLibraryPicker: PHPickerViewController = {
let imagePicker = PHPickerViewController(configuration: ComposeContentViewController.createPhotoLibraryPickerConfiguration())
imagePicker.delegate = self
return imagePicker
}()
public private(set) lazy var imagePickerController: UIImagePickerController = {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .camera
imagePickerController.delegate = self
return imagePickerController
}()
public private(set) lazy var documentPickerController: UIDocumentPickerViewController = {
let documentPickerController = UIDocumentPickerViewController(forOpeningContentTypes: [.image, .movie])
documentPickerController.delegate = self
return documentPickerController
}()
// emoji picker inputView
let customEmojiPickerInputView: CustomEmojiPickerInputView = {
let view = CustomEmojiPickerInputView(
frame: CGRect(x: 0, y: 0, width: 0, height: 300),
inputViewStyle: .keyboard
)
return view
}()
}
extension ComposeContentViewController {
public override func viewDidLoad() {
super.viewDidLoad()
viewModel.delegate = self
// setup view
self.setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
ThemeService.shared.currentTheme
.receive(on: RunLoop.main)
.sink { [weak self] theme in
guard let self = self else { return }
self.setupBackgroundColor(theme: theme)
}
.store(in: &disposeBag)
// setup tableView
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
tableView.pinToParent()
tableView.delegate = self
viewModel.setupDataSource(tableView: tableView)
// setup emoji picker
customEmojiPickerInputView.collectionView.delegate = self
viewModel.customEmojiPickerInputViewModel.customEmojiPickerInputView = customEmojiPickerInputView
viewModel.setupCustomEmojiPickerDiffableDataSource(collectionView: customEmojiPickerInputView.collectionView)
// setup toolbar
let toolbarHostingView = UIHostingController(rootView: composeContentToolbarView)
toolbarHostingView.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toolbarHostingView.view)
composeContentToolbarViewBottomLayoutConstraint = view.bottomAnchor.constraint(equalTo: toolbarHostingView.view.bottomAnchor)
NSLayoutConstraint.activate([
toolbarHostingView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
toolbarHostingView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
composeContentToolbarViewBottomLayoutConstraint,
toolbarHostingView.view.heightAnchor.constraint(equalToConstant: ComposeContentToolbarView.toolbarHeight),
])
toolbarHostingView.view.preservesSuperviewLayoutMargins = true
//composeToolbarView.delegate = self
composeContentToolbarBackgroundView.translatesAutoresizingMaskIntoConstraints = false
view.insertSubview(composeContentToolbarBackgroundView, belowSubview: toolbarHostingView.view)
NSLayoutConstraint.activate([
composeContentToolbarBackgroundView.topAnchor.constraint(equalTo: toolbarHostingView.view.topAnchor),
composeContentToolbarBackgroundView.leadingAnchor.constraint(equalTo: toolbarHostingView.view.leadingAnchor),
composeContentToolbarBackgroundView.trailingAnchor.constraint(equalTo: toolbarHostingView.view.trailingAnchor),
view.bottomAnchor.constraint(equalTo: composeContentToolbarBackgroundView.bottomAnchor),
])
// bind keyboard
let keyboardEventPublishers = Publishers.CombineLatest3(
KeyboardResponderService.shared.isShow,
KeyboardResponderService.shared.state,
KeyboardResponderService.shared.endFrame
)
Publishers.CombineLatest3(
keyboardEventPublishers,
viewModel.$isEmojiActive,
viewModel.$autoCompleteInfo
)
.sink(receiveValue: { [weak self] keyboardEvents, isEmojiActive, autoCompleteInfo in
guard let self = self else { return }
let (isShow, state, endFrame) = keyboardEvents
let extraMargin: CGFloat = {
var margin = ComposeContentToolbarView.toolbarHeight
if autoCompleteInfo != nil {
margin += ComposeContentViewController.minAutoCompleteVisibleHeight
}
return margin
}()
guard isShow, state == .dock else {
self.tableView.contentInset.bottom = extraMargin
self.tableView.verticalScrollIndicatorInsets.bottom = extraMargin
if let superView = self.autoCompleteViewController.tableView.superview {
let autoCompleteTableViewBottomInset: CGFloat = {
let tableViewFrameInWindow = superView.convert(self.autoCompleteViewController.tableView.frame, to: nil)
let padding = tableViewFrameInWindow.maxY + ComposeContentToolbarView.toolbarHeight + AutoCompleteViewController.chevronViewHeight - self.view.frame.maxY
return max(0, padding)
}()
self.autoCompleteViewController.tableView.contentInset.bottom = autoCompleteTableViewBottomInset
self.autoCompleteViewController.tableView.verticalScrollIndicatorInsets.bottom = autoCompleteTableViewBottomInset
}
UIView.animate(withDuration: 0.3) {
self.composeContentToolbarViewBottomLayoutConstraint.constant = self.view.safeAreaInsets.bottom
if self.view.window != nil {
self.view.layoutIfNeeded()
}
}
return
}
// isShow AND dock state
// adjust inset for auto-complete
let autoCompleteTableViewBottomInset: CGFloat = {
guard let superview = self.autoCompleteViewController.tableView.superview else { return .zero }
let tableViewFrameInWindow = superview.convert(self.autoCompleteViewController.tableView.frame, to: nil)
let padding = tableViewFrameInWindow.maxY + ComposeContentToolbarView.toolbarHeight + AutoCompleteViewController.chevronViewHeight - endFrame.minY
return max(0, padding)
}()
self.autoCompleteViewController.tableView.contentInset.bottom = autoCompleteTableViewBottomInset
self.autoCompleteViewController.tableView.verticalScrollIndicatorInsets.bottom = autoCompleteTableViewBottomInset
// adjust inset for tableView
let contentFrame = self.view.convert(self.tableView.frame, to: nil)
let padding = contentFrame.maxY + extraMargin - endFrame.minY
guard padding > 0 else {
self.tableView.contentInset.bottom = self.view.safeAreaInsets.bottom + extraMargin
self.tableView.verticalScrollIndicatorInsets.bottom = self.view.safeAreaInsets.bottom + extraMargin
return
}
self.tableView.contentInset.bottom = padding - self.view.safeAreaInsets.bottom
self.tableView.verticalScrollIndicatorInsets.bottom = padding - self.view.safeAreaInsets.bottom
UIView.animate(withDuration: 0.3) {
self.composeContentToolbarViewBottomLayoutConstraint.constant = endFrame.height
self.view.layoutIfNeeded()
}
})
.store(in: &disposeBag)
// setup snap behavior
Publishers.CombineLatest(
viewModel.$replyToCellFrame,
viewModel.$scrollViewState
)
.receive(on: DispatchQueue.main)
.sink { [weak self] replyToCellFrame, scrollViewState in
guard let self = self else { return }
guard replyToCellFrame != .zero else { return }
switch scrollViewState {
case .fold:
self.tableView.contentInset.top = -replyToCellFrame.height
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: set contentInset.top: -%s", ((#file as NSString).lastPathComponent), #line, #function, replyToCellFrame.height.description)
case .expand:
self.tableView.contentInset.top = 0
}
}
.store(in: &disposeBag)
// bind auto-complete
viewModel.$autoCompleteInfo
.receive(on: DispatchQueue.main)
.sink { [weak self] info in
guard let self = self else { return }
guard let textView = self.viewModel.contentMetaText?.textView else { return }
if self.autoCompleteViewController.view.superview == nil {
self.autoCompleteViewController.view.frame = self.view.bounds
// add to container view. seealso: `viewDidLayoutSubviews()`
self.viewModel.composeContentTableViewCell.contentView.addSubview(self.autoCompleteViewController.view)
self.addChild(self.autoCompleteViewController)
self.autoCompleteViewController.didMove(toParent: self)
self.autoCompleteViewController.view.isHidden = true
self.tableView.autoCompleteViewController = self.autoCompleteViewController
}
self.updateAutoCompleteViewControllerLayout()
self.autoCompleteViewController.view.isHidden = info == nil
guard let info = info else { return }
let symbolBoundingRectInContainer = textView.convert(info.symbolBoundingRect, to: self.autoCompleteViewController.chevronView)
print(info.symbolBoundingRect)
self.autoCompleteViewController.view.frame.origin.y = info.textBoundingRect.maxY + self.viewModel.contentTextViewFrame.minY
self.autoCompleteViewController.viewModel.symbolBoundingRect.value = symbolBoundingRectInContainer
self.autoCompleteViewController.viewModel.inputText.value = String(info.inputText)
}
.store(in: &disposeBag)
// bind emoji picker
viewModel.customEmojiViewModel?.emojis
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] emojis in
guard let self = self else { return }
if emojis.isEmpty {
self.customEmojiPickerInputView.activityIndicatorView.startAnimating()
} else {
self.customEmojiPickerInputView.activityIndicatorView.stopAnimating()
}
})
.store(in: &disposeBag)
// bind toolbar
bindToolbarViewModel()
// bind attachment picker
viewModel.$attachmentViewModels
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.resetImagePicker()
}
.store(in: &disposeBag)
}
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
viewModel.viewLayoutFrame.update(view: view)
updateAutoCompleteViewControllerLayout()
}
public override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
viewModel.viewLayoutFrame.update(view: view)
}
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate { [weak self] coordinatorContext in
guard let self = self else { return }
self.viewModel.viewLayoutFrame.update(view: self.view)
}
}
}
extension ComposeContentViewController {
private func setupBackgroundColor(theme: Theme) {
let backgroundColor = UIColor(dynamicProvider: { traitCollection in
switch traitCollection.userInterfaceStyle {
case .light: return .systemBackground
default: return theme.systemElevatedBackgroundColor
}
})
view.backgroundColor = backgroundColor
tableView.backgroundColor = backgroundColor
composeContentToolbarBackgroundView.backgroundColor = theme.composeToolbarBackgroundColor
}
private func bindToolbarViewModel() {
viewModel.$isAttachmentButtonEnabled.assign(to: &composeContentToolbarViewModel.$isAttachmentButtonEnabled)
viewModel.$isPollButtonEnabled.assign(to: &composeContentToolbarViewModel.$isPollButtonEnabled)
viewModel.$isPollActive.assign(to: &composeContentToolbarViewModel.$isPollActive)
viewModel.$isEmojiActive.assign(to: &composeContentToolbarViewModel.$isEmojiActive)
viewModel.$isContentWarningActive.assign(to: &composeContentToolbarViewModel.$isContentWarningActive)
viewModel.$visibility.assign(to: &composeContentToolbarViewModel.$visibility)
viewModel.$isVisibilityButtonEnabled.assign(to: &composeContentToolbarViewModel.$isVisibilityButtonEnabled)
viewModel.$maxTextInputLimit.assign(to: &composeContentToolbarViewModel.$maxTextInputLimit)
viewModel.$contentWeightedLength.assign(to: &composeContentToolbarViewModel.$contentWeightedLength)
viewModel.$contentWarningWeightedLength.assign(to: &composeContentToolbarViewModel.$contentWarningWeightedLength)
let languageRecognizer = NLLanguageRecognizer()
viewModel.$content
// run on background thread since NLLanguageRecognizer seems to do CPU-bound work
// that we don’t want on main
.receive(on: DispatchQueue.global(qos: .utility))
.sink { [unowned self] content in
if content.isEmpty {
DispatchQueue.main.async {
self.composeContentToolbarViewModel.suggestedLanguages = []
}
return
}
defer { languageRecognizer.reset() }
languageRecognizer.processString(content)
let hypotheses = languageRecognizer
.languageHypotheses(withMaximum: 3)
DispatchQueue.main.async {
self.composeContentToolbarViewModel.suggestedLanguages = hypotheses
.filter { _, probability in probability > 0.1 }
.keys
.map(\.rawValue)
if let bestLanguage = hypotheses.max(by: { $0.value < $1.value }), bestLanguage.value > 0.99 {
self.composeContentToolbarViewModel.highConfidenceSuggestedLanguage = bestLanguage.key.rawValue
} else {
self.composeContentToolbarViewModel.highConfidenceSuggestedLanguage = nil
}
}
}
.store(in: &disposeBag)
viewModel.$language.assign(to: &composeContentToolbarViewModel.$language)
composeContentToolbarViewModel.$language
.dropFirst()
.receive(on: DispatchQueue.main)
.sink { [weak self] language in
guard let self = self else { return }
if self.viewModel.language != language {
self.viewModel.language = language
}
}
.store(in: &disposeBag)
viewModel.$recentLanguages.assign(to: &composeContentToolbarViewModel.$recentLanguages)
// bind back to source due to visibility not update via delegate
composeContentToolbarViewModel.$visibility
.dropFirst()
.receive(on: DispatchQueue.main)
.sink { [weak self] visibility in
guard let self = self else { return }
if self.viewModel.visibility != visibility {
self.viewModel.visibility = visibility
}
}
.store(in: &disposeBag)
}
private func updateAutoCompleteViewControllerLayout() {
// pin autoCompleteViewController frame to current view
if let containerView = autoCompleteViewController.view.superview {
let viewFrameInWindow = containerView.convert(autoCompleteViewController.view.frame, to: view)
if viewFrameInWindow.origin.x != 0 {
autoCompleteViewController.view.frame.origin.x = -viewFrameInWindow.origin.x
}
autoCompleteViewController.view.frame.size.width = view.frame.width
}
}
private func resetImagePicker() {
let selectionLimit = max(1, viewModel.maxMediaAttachmentLimit - viewModel.attachmentViewModels.count)
let configuration = ComposeContentViewController.createPhotoLibraryPickerConfiguration(selectionLimit: selectionLimit)
photoLibraryPicker = createImagePicker(configuration: configuration)
}
private func createImagePicker(configuration: PHPickerConfiguration) -> PHPickerViewController {
let imagePicker = PHPickerViewController(configuration: configuration)
imagePicker.delegate = self
return imagePicker
}
}
// MARK: - UIScrollViewDelegate
extension ComposeContentViewController {
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
guard scrollView === tableView else { return }
let replyToCellFrame = viewModel.replyToCellFrame
guard replyToCellFrame != .zero else { return }
// try to find some patterns:
// print("""
// repliedToCellFrame: \(viewModel.repliedToCellFrame.value.height)
// scrollView.contentOffset.y: \(scrollView.contentOffset.y)
// scrollView.contentSize.height: \(scrollView.contentSize.height)
// scrollView.frame: \(scrollView.frame)
// scrollView.adjustedContentInset.top: \(scrollView.adjustedContentInset.top)
// scrollView.adjustedContentInset.bottom: \(scrollView.adjustedContentInset.bottom)
// """)
switch viewModel.scrollViewState {
case .fold:
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fold")
guard velocity.y < 0 else { return }
let offsetY = scrollView.contentOffset.y + scrollView.adjustedContentInset.top
if offsetY < -44 {
tableView.contentInset.top = 0
targetContentOffset.pointee = CGPoint(x: 0, y: -scrollView.adjustedContentInset.top)
viewModel.scrollViewState = .expand
}
case .expand:
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): expand")
guard velocity.y > 0 else { return }
// check if top across
let topOffset = (scrollView.contentOffset.y + scrollView.adjustedContentInset.top) - replyToCellFrame.height
// check if bottom bounce
let bottomOffsetY = scrollView.contentOffset.y + (scrollView.frame.height - scrollView.adjustedContentInset.bottom)
let bottomOffset = bottomOffsetY - scrollView.contentSize.height
if topOffset > 44 {
// do not interrupt user scrolling
viewModel.scrollViewState = .fold
} else if bottomOffset > 44 {
tableView.contentInset.top = -replyToCellFrame.height
targetContentOffset.pointee = CGPoint(x: 0, y: -replyToCellFrame.height)
viewModel.scrollViewState = .fold
}
}
}
}
// MARK: - UITableViewDelegate
extension ComposeContentViewController: UITableViewDelegate { }
// MARK: - PHPickerViewControllerDelegate
extension ComposeContentViewController: PHPickerViewControllerDelegate {
public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion: nil)
let attachmentViewModels: [AttachmentViewModel] = results.map { result in
AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .pickerResult(result),
sizeLimit: viewModel.sizeLimit,
delegate: viewModel
)
}
viewModel.attachmentViewModels += attachmentViewModels
}
}
// MARK: - UIImagePickerControllerDelegate
extension ComposeContentViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[.originalImage] as? UIImage else { return }
let attachmentViewModel = AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .image(image),
sizeLimit: viewModel.sizeLimit,
delegate: viewModel
)
viewModel.attachmentViewModels += [attachmentViewModel]
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
picker.dismiss(animated: true, completion: nil)
}
}
// MARK: - UIDocumentPickerDelegate
extension ComposeContentViewController: UIDocumentPickerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
let attachmentViewModel = AttachmentViewModel(
api: viewModel.context.apiService,
authContext: viewModel.authContext,
input: .url(url),
sizeLimit: viewModel.sizeLimit,
delegate: viewModel
)
viewModel.attachmentViewModels += [attachmentViewModel]
}
}
// MARK: - ComposeContentToolbarViewDelegate
extension ComposeContentViewController: ComposeContentToolbarViewDelegate {
func composeContentToolbarView(
_ viewModel: ComposeContentToolbarView.ViewModel,
toolbarItemDidPressed action: ComposeContentToolbarView.ViewModel.Action
) {
switch action {
case .poll:
self.viewModel.isPollActive.toggle()
case .emoji:
self.viewModel.isEmojiActive.toggle()
case .contentWarning:
self.viewModel.isContentWarningActive.toggle()
if self.viewModel.isContentWarningActive {
Task { @MainActor in
try? await Task.sleep(nanoseconds: .second / 20) // 0.05s
self.viewModel.setContentWarningTextViewFirstResponderIfNeeds()
} // end Task
} else {
if self.viewModel.contentWarningMetaText?.textView.isFirstResponder == true {
self.viewModel.setContentTextViewFirstResponderIfNeeds()
}
}
}
}
func composeContentToolbarView(
_ viewModel: ComposeContentToolbarView.ViewModel,
attachmentMenuDidPressed action: ComposeContentToolbarView.ViewModel.AttachmentAction
) {
switch action {
case .photoLibrary:
present(photoLibraryPicker, animated: true, completion: nil)
case .camera:
present(imagePickerController, animated: true, completion: nil)
case .browse:
#if SNAPSHOT
guard let image = UIImage(named: "Athens") else { return }
let attachmentService = MastodonAttachmentService(
context: context,
image: image,
initialAuthenticationBox: viewModel.authenticationBox
)
viewModel.attachmentServices = viewModel.attachmentServices + [attachmentService]
#else
present(documentPickerController, animated: true, completion: nil)
#endif
}
}
}
// MARK: - AutoCompleteViewControllerDelegate
extension ComposeContentViewController: AutoCompleteViewControllerDelegate {
func autoCompleteViewController(
_ viewController: AutoCompleteViewController,
didSelectItem item: AutoCompleteItem
) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): did select item: \(String(describing: item))")
guard let info = viewModel.autoCompleteInfo else { return }
guard let metaText = viewModel.contentMetaText else { return }
let _replacedText: String? = {
var text: String
switch item {
case .hashtag(let hashtag):
text = "#" + hashtag.name
case .hashtagV1(let hashtagName):
text = "#" + hashtagName
case .account(let account):
text = "@" + account.acct
case .emoji(let emoji):
text = ":" + emoji.shortcode + ":"
case .bottomLoader:
return nil
}
return text
}()
guard let replacedText = _replacedText else { return }
guard let text = metaText.textView.text else { return }
let range = NSRange(info.toHighlightEndRange, in: text)
metaText.textStorage.replaceCharacters(in: range, with: replacedText)
viewModel.autoCompleteInfo = nil
// set selected range
let newRange = NSRange(location: range.location + (replacedText as NSString).length, length: 0)
guard metaText.textStorage.length <= newRange.location else { return }
metaText.textView.selectedRange = newRange
// append a space and trigger textView delegate update
DispatchQueue.main.async {
metaText.textView.insertText(" ")
}
}
}
// MARK: - UICollectionViewDelegate
extension ComposeContentViewController: UICollectionViewDelegate {
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: select %s", ((#file as NSString).lastPathComponent), #line, #function, indexPath.debugDescription)
switch collectionView {
case customEmojiPickerInputView.collectionView:
guard let diffableDataSource = viewModel.customEmojiPickerDiffableDataSource else { return }
let item = diffableDataSource.itemIdentifier(for: indexPath)
guard case let .emoji(attribute) = item else { return }
let emoji = attribute.emoji
// make click sound
UIDevice.current.playInputClick()
// retrieve active text input and insert emoji
// the trailing space is REQUIRED to make regex happy
_ = viewModel.customEmojiPickerInputViewModel.insertText(":\(emoji.shortcode): ")
default:
assertionFailure()
}
} // end func
}
// MARK: - ComposeContentViewModelDelegate
extension ComposeContentViewController: ComposeContentViewModelDelegate {
public func composeContentViewModel(
_ viewModel: ComposeContentViewModel,
handleAutoComplete info: ComposeContentViewModel.AutoCompleteInfo
) -> Bool {
let snapshot = autoCompleteViewController.viewModel.diffableDataSource.snapshot()
guard let item = snapshot.itemIdentifiers.first else { return false }
// FIXME: redundant code
guard let metaText = viewModel.contentMetaText else { return false }
guard let text = metaText.textView.text else { return false }
let _replacedText: String? = {
var text: String
switch item {
case .hashtag, .hashtagV1:
// do no fill the hashtag
// allow user delete suffix and post they want
return nil
case .account(let account):
text = "@" + account.acct
case .emoji(let emoji):
text = ":" + emoji.shortcode + ":"
case .bottomLoader:
return nil
}
return text
}()
guard let replacedText = _replacedText else { return false }
let range = NSRange(info.toHighlightEndRange, in: text)
metaText.textStorage.replaceCharacters(in: range, with: replacedText)
viewModel.autoCompleteInfo = nil
// set selected range
let newRange = NSRange(location: range.location + (replacedText as NSString).length, length: 0)
guard metaText.textStorage.length <= newRange.location else { return true }
metaText.textView.selectedRange = newRange
// append a space and trigger textView delegate update
DispatchQueue.main.async {
metaText.textView.insertText(" ")
}
return true
}
}