2020-12-16 02:39:38 +01:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2021-01-02 00:31:39 +01:00
|
|
|
import AVFoundation
|
2020-12-16 02:39:38 +01:00
|
|
|
import Combine
|
2021-01-01 01:49:59 +01:00
|
|
|
import Mastodon
|
2020-12-16 02:39:38 +01:00
|
|
|
import UIKit
|
|
|
|
import ViewModels
|
|
|
|
|
2021-02-15 09:47:30 +01:00
|
|
|
final class CompositionInputAccessoryView: UIView {
|
2021-01-14 18:49:53 +01:00
|
|
|
let tagForInputView = UUID().hashValue
|
2021-02-15 22:52:28 +01:00
|
|
|
let autocompleteSelections: AnyPublisher<String, Never>
|
2021-01-01 01:49:59 +01:00
|
|
|
|
2020-12-16 02:39:38 +01:00
|
|
|
private let viewModel: CompositionViewModel
|
2021-01-01 01:49:59 +01:00
|
|
|
private let parentViewModel: NewStatusViewModel
|
2021-02-15 09:47:30 +01:00
|
|
|
private let toolbar = UIToolbar()
|
|
|
|
private let autocompleteCollectionView = UICollectionView(
|
|
|
|
frame: .zero,
|
|
|
|
collectionViewLayout: CompositionInputAccessoryView.autocompleteLayout())
|
|
|
|
private let autocompleteDataSource: AutocompleteDataSource
|
|
|
|
private let autocompleteCollectionViewHeightConstraint: NSLayoutConstraint
|
2021-02-15 22:52:28 +01:00
|
|
|
private let autocompleteSelectionsSubject = PassthroughSubject<String, Never>()
|
2020-12-16 02:39:38 +01:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-02-14 03:28:30 +01:00
|
|
|
init(viewModel: CompositionViewModel,
|
|
|
|
parentViewModel: NewStatusViewModel,
|
|
|
|
autocompleteQueryPublisher: AnyPublisher<String?, Never>) {
|
2020-12-16 02:39:38 +01:00
|
|
|
self.viewModel = viewModel
|
2021-01-01 01:49:59 +01:00
|
|
|
self.parentViewModel = parentViewModel
|
2021-02-15 09:47:30 +01:00
|
|
|
autocompleteDataSource = AutocompleteDataSource(
|
|
|
|
collectionView: autocompleteCollectionView,
|
|
|
|
queryPublisher: autocompleteQueryPublisher,
|
|
|
|
parentViewModel: parentViewModel)
|
|
|
|
autocompleteCollectionViewHeightConstraint =
|
2021-02-15 22:52:28 +01:00
|
|
|
autocompleteCollectionView.heightAnchor.constraint(equalToConstant: .hairline)
|
|
|
|
autocompleteSelections = autocompleteSelectionsSubject.eraseToAnyPublisher()
|
2020-12-16 02:39:38 +01:00
|
|
|
|
2021-01-19 06:16:21 +01:00
|
|
|
super.init(
|
|
|
|
frame: .init(
|
|
|
|
origin: .zero,
|
|
|
|
size: .init(width: UIScreen.main.bounds.width, height: .minimumButtonDimension)))
|
2020-12-16 02:39:38 +01:00
|
|
|
|
|
|
|
initialSetup()
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2021-02-15 22:52:28 +01:00
|
|
|
|
|
|
|
override func didMoveToSuperview() {
|
|
|
|
super.didMoveToSuperview()
|
|
|
|
|
|
|
|
layoutIfNeeded()
|
|
|
|
}
|
2020-12-16 02:39:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension CompositionInputAccessoryView {
|
2021-02-15 09:47:30 +01:00
|
|
|
static let autocompleteCollectionViewMaxHeight: CGFloat = 150
|
|
|
|
|
|
|
|
var heightConstraint: NSLayoutConstraint? {
|
|
|
|
superview?.constraints.first(where: { $0.identifier == "accessoryHeight" })
|
|
|
|
}
|
|
|
|
|
2021-01-01 01:49:59 +01:00
|
|
|
// swiftlint:disable:next function_body_length
|
2020-12-16 02:39:38 +01:00
|
|
|
func initialSetup() {
|
2021-01-19 06:16:21 +01:00
|
|
|
autoresizingMask = .flexibleHeight
|
|
|
|
|
2021-02-15 09:47:30 +01:00
|
|
|
addSubview(autocompleteCollectionView)
|
|
|
|
autocompleteCollectionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
autocompleteCollectionView.alwaysBounceVertical = false
|
|
|
|
autocompleteCollectionView.backgroundColor = .clear
|
|
|
|
autocompleteCollectionView.layer.cornerRadius = .defaultCornerRadius
|
|
|
|
autocompleteCollectionView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
|
2021-02-15 20:17:06 +01:00
|
|
|
autocompleteCollectionView.dataSource = autocompleteDataSource
|
|
|
|
autocompleteCollectionView.delegate = self
|
2021-02-15 09:47:30 +01:00
|
|
|
|
|
|
|
let autocompleteBackgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemChromeMaterial))
|
|
|
|
|
|
|
|
autocompleteCollectionView.backgroundView = autocompleteBackgroundView
|
|
|
|
|
|
|
|
addSubview(toolbar)
|
|
|
|
toolbar.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
toolbar.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
autocompleteCollectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
autocompleteCollectionView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
autocompleteCollectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
autocompleteCollectionView.bottomAnchor.constraint(equalTo: toolbar.topAnchor),
|
|
|
|
toolbar.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
toolbar.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
toolbar.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
toolbar.heightAnchor.constraint(equalToConstant: .minimumButtonDimension),
|
|
|
|
autocompleteCollectionViewHeightConstraint
|
|
|
|
])
|
2021-01-27 02:45:25 +01:00
|
|
|
|
2021-01-10 08:08:45 +01:00
|
|
|
var attachmentActions = [
|
|
|
|
UIAction(
|
|
|
|
title: NSLocalizedString("compose.browse", comment: ""),
|
|
|
|
image: UIImage(systemName: "ellipsis")) { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2021-01-01 01:49:59 +01:00
|
|
|
|
2021-01-10 08:08:45 +01:00
|
|
|
self.parentViewModel.presentDocumentPicker(viewModel: self.viewModel)
|
|
|
|
},
|
|
|
|
UIAction(
|
|
|
|
title: NSLocalizedString("compose.photo-library", comment: ""),
|
|
|
|
image: UIImage(systemName: "rectangle.on.rectangle")) { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2020-12-16 02:39:38 +01:00
|
|
|
|
2021-01-10 08:08:45 +01:00
|
|
|
self.parentViewModel.presentMediaPicker(viewModel: self.viewModel)
|
|
|
|
}
|
|
|
|
]
|
2021-01-03 23:37:06 +01:00
|
|
|
|
2021-01-02 00:31:39 +01:00
|
|
|
#if !IS_SHARE_EXTENSION
|
2021-01-10 08:08:45 +01:00
|
|
|
attachmentActions.insert(UIAction(
|
|
|
|
title: NSLocalizedString("compose.take-photo-or-video", comment: ""),
|
|
|
|
image: UIImage(systemName: "camera.fill")) { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2021-01-02 00:31:39 +01:00
|
|
|
|
2021-01-10 08:08:45 +01:00
|
|
|
self.parentViewModel.presentCamera(viewModel: self.viewModel)
|
|
|
|
},
|
|
|
|
at: 1)
|
2021-01-02 00:31:39 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
let attachmentButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "paperclip"),
|
|
|
|
menu: UIMenu(children: attachmentActions))
|
2021-02-01 20:53:47 +01:00
|
|
|
|
|
|
|
attachmentButton.accessibilityLabel =
|
|
|
|
NSLocalizedString("compose.attachments-button.accessibility-label", comment: "")
|
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
let pollButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "chart.bar.xaxis"),
|
|
|
|
primaryAction: UIAction { [weak self] _ in self?.viewModel.displayPoll.toggle() })
|
2021-02-01 20:53:47 +01:00
|
|
|
|
|
|
|
pollButton.accessibilityLabel = NSLocalizedString("compose.poll-button.accessibility-label", comment: "")
|
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
let visibilityButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: parentViewModel.visibility.systemImageName),
|
2021-01-29 00:32:08 +01:00
|
|
|
menu: visibilityMenu(selectedVisibility: parentViewModel.visibility))
|
2021-02-01 20:53:47 +01:00
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
let contentWarningButton = UIBarButtonItem(
|
|
|
|
title: NSLocalizedString("status.content-warning-abbreviation", comment: ""),
|
|
|
|
primaryAction: UIAction { [weak self] _ in self?.viewModel.displayContentWarning.toggle() })
|
2021-02-01 20:53:47 +01:00
|
|
|
|
|
|
|
viewModel.$displayContentWarning.sink {
|
|
|
|
if $0 {
|
|
|
|
contentWarningButton.accessibilityHint =
|
|
|
|
NSLocalizedString("compose.content-warning-button.remove", comment: "")
|
|
|
|
} else {
|
|
|
|
contentWarningButton.accessibilityHint =
|
|
|
|
NSLocalizedString("compose.content-warning-button.add", comment: "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
let emojiButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "face.smiling"),
|
|
|
|
primaryAction: UIAction { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2021-01-14 18:49:53 +01:00
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
self.parentViewModel.presentEmojiPicker(tag: self.tagForInputView)
|
|
|
|
})
|
2021-02-01 20:53:47 +01:00
|
|
|
|
|
|
|
emojiButton.accessibilityLabel = NSLocalizedString("compose.emoji-button", comment: "")
|
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
let addButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "plus.circle.fill"),
|
|
|
|
primaryAction: UIAction { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2021-01-14 18:49:53 +01:00
|
|
|
|
2021-01-18 09:24:11 +01:00
|
|
|
self.parentViewModel.insert(after: self.viewModel)
|
|
|
|
})
|
2020-12-16 02:39:38 +01:00
|
|
|
|
2021-02-01 20:53:47 +01:00
|
|
|
switch parentViewModel.identityContext.appPreferences.statusWord {
|
|
|
|
case .toot:
|
|
|
|
addButton.accessibilityLabel =
|
|
|
|
NSLocalizedString("compose.add-button-accessibility-label.toot", comment: "")
|
|
|
|
case .post:
|
|
|
|
addButton.accessibilityLabel =
|
|
|
|
NSLocalizedString("compose.add-button-accessibility-label.post", comment: "")
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:47:30 +01:00
|
|
|
let charactersBarItem = UIBarButtonItem()
|
2021-01-18 09:24:11 +01:00
|
|
|
|
2021-02-15 09:47:30 +01:00
|
|
|
charactersBarItem.isEnabled = false
|
2021-01-18 09:24:11 +01:00
|
|
|
|
2021-02-15 09:47:30 +01:00
|
|
|
toolbar.items = [
|
2021-01-18 09:24:11 +01:00
|
|
|
attachmentButton,
|
|
|
|
UIBarButtonItem.fixedSpace(.defaultSpacing),
|
|
|
|
pollButton,
|
|
|
|
UIBarButtonItem.fixedSpace(.defaultSpacing),
|
|
|
|
visibilityButton,
|
|
|
|
UIBarButtonItem.fixedSpace(.defaultSpacing),
|
|
|
|
contentWarningButton,
|
|
|
|
UIBarButtonItem.fixedSpace(.defaultSpacing),
|
|
|
|
emojiButton,
|
|
|
|
UIBarButtonItem.flexibleSpace(),
|
|
|
|
charactersBarItem,
|
|
|
|
UIBarButtonItem.fixedSpace(.defaultSpacing),
|
|
|
|
addButton]
|
2021-01-01 01:49:59 +01:00
|
|
|
|
2021-01-10 08:08:45 +01:00
|
|
|
viewModel.$canAddAttachment
|
|
|
|
.sink { attachmentButton.isEnabled = $0 }
|
|
|
|
.store(in: &cancellables)
|
2021-01-04 02:15:50 +01:00
|
|
|
|
2021-01-11 01:06:20 +01:00
|
|
|
viewModel.$attachmentViewModels
|
2021-03-31 04:14:18 +02:00
|
|
|
.combineLatest(viewModel.$attachmentUploadViewModels)
|
|
|
|
.sink { pollButton.isEnabled = $0.isEmpty && $1.isEmpty }
|
2021-01-11 01:06:20 +01:00
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-04 02:15:50 +01:00
|
|
|
viewModel.$remainingCharacters.sink {
|
2021-02-15 09:47:30 +01:00
|
|
|
charactersBarItem.title = String($0)
|
|
|
|
charactersBarItem.setTitleTextAttributes(
|
|
|
|
[.foregroundColor: $0 < 0 ? UIColor.systemRed : UIColor.label],
|
|
|
|
for: .disabled)
|
|
|
|
charactersBarItem.accessibilityHint = String.localizedStringWithFormat(
|
2021-02-01 20:53:47 +01:00
|
|
|
NSLocalizedString("compose.characters-remaining-accessibility-label-%ld", comment: ""),
|
|
|
|
$0)
|
2021-01-04 02:15:50 +01:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
2021-01-03 23:37:06 +01:00
|
|
|
|
2021-01-01 01:49:59 +01:00
|
|
|
viewModel.$isPostable
|
2021-01-18 09:24:11 +01:00
|
|
|
.sink { addButton.isEnabled = $0 }
|
2021-01-01 01:49:59 +01:00
|
|
|
.store(in: &cancellables)
|
2020-12-16 02:39:38 +01:00
|
|
|
|
2021-02-15 09:47:30 +01:00
|
|
|
self.autocompleteCollectionView.publisher(for: \.contentSize)
|
|
|
|
.map(\.height)
|
|
|
|
.removeDuplicates()
|
|
|
|
.throttle(for: .seconds(TimeInterval.shortAnimationDuration), scheduler: DispatchQueue.main, latest: true)
|
|
|
|
.sink { [weak self] height in
|
|
|
|
UIView.animate(withDuration: .zeroIfReduceMotion(.shortAnimationDuration)) {
|
|
|
|
self?.setAutocompleteCollectionViewHeight(height)
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 03:28:30 +01:00
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-01 01:49:59 +01:00
|
|
|
parentViewModel.$visibility
|
2021-01-29 00:32:08 +01:00
|
|
|
.sink { [weak self] in
|
|
|
|
visibilityButton.image = UIImage(systemName: $0.systemImageName)
|
|
|
|
visibilityButton.menu = self?.visibilityMenu(selectedVisibility: $0)
|
2021-02-03 07:00:29 +01:00
|
|
|
visibilityButton.accessibilityLabel = String.localizedStringWithFormat(
|
|
|
|
NSLocalizedString("compose.visibility-button.accessibility-label-%@", comment: ""),
|
|
|
|
$0.title ?? "")
|
2021-01-29 00:32:08 +01:00
|
|
|
}
|
2021-01-01 01:49:59 +01:00
|
|
|
.store(in: &cancellables)
|
2020-12-16 02:39:38 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 00:32:08 +01:00
|
|
|
|
2021-02-15 20:17:06 +01:00
|
|
|
extension CompositionInputAccessoryView: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
2021-02-15 22:52:28 +01:00
|
|
|
|
|
|
|
guard let item = autocompleteDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case let .account(account):
|
|
|
|
autocompleteSelectionsSubject.send("@".appending(account.acct))
|
|
|
|
case let .tag(tag):
|
|
|
|
autocompleteSelectionsSubject.send("#".appending(tag.name))
|
|
|
|
case let .emoji(emoji):
|
|
|
|
let escaped = emoji.applyingDefaultSkinTone(identityContext: parentViewModel.identityContext).escaped
|
|
|
|
|
|
|
|
autocompleteSelectionsSubject.send(escaped)
|
|
|
|
autocompleteDataSource.updateUse(emoji: emoji)
|
|
|
|
}
|
|
|
|
|
|
|
|
UISelectionFeedbackGenerator().selectionChanged()
|
|
|
|
|
|
|
|
// To dismiss without waiting for the throttle
|
|
|
|
UIView.animate(withDuration: .zeroIfReduceMotion(.shortAnimationDuration)) {
|
|
|
|
self.setAutocompleteCollectionViewHeight(.hairline)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView,
|
|
|
|
contextMenuConfigurationForItemAt indexPath: IndexPath,
|
|
|
|
point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
guard let item = autocompleteDataSource.itemIdentifier(for: indexPath),
|
|
|
|
case let .emoji(emojiItem) = item,
|
|
|
|
case let .system(emoji, _) = emojiItem,
|
|
|
|
!emoji.skinToneVariations.isEmpty
|
|
|
|
else { return nil }
|
|
|
|
|
|
|
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
|
|
|
|
UIMenu(children: ([emoji] + emoji.skinToneVariations).map { skinToneVariation in
|
|
|
|
UIAction(title: skinToneVariation.emoji) { [weak self] _ in
|
|
|
|
self?.autocompleteSelectionsSubject.send(skinToneVariation.emoji)
|
|
|
|
self?.autocompleteDataSource.updateUse(emoji: emojiItem)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-15 20:17:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 00:32:08 +01:00
|
|
|
private extension CompositionInputAccessoryView {
|
2021-02-15 09:47:30 +01:00
|
|
|
static func autocompleteLayout() -> UICollectionViewLayout {
|
|
|
|
var listConfig = UICollectionLayoutListConfiguration(appearance: .plain)
|
|
|
|
|
|
|
|
listConfig.backgroundColor = .clear
|
|
|
|
|
|
|
|
return UICollectionViewCompositionalLayout { index, environment -> NSCollectionLayoutSection? in
|
|
|
|
guard let autocompleteSection = AutocompleteSection(rawValue: index) else { return nil }
|
|
|
|
|
|
|
|
switch autocompleteSection {
|
|
|
|
case .search:
|
|
|
|
return .list(using: listConfig, layoutEnvironment: environment)
|
|
|
|
case .emoji:
|
|
|
|
let itemSize = NSCollectionLayoutSize(
|
|
|
|
widthDimension: .fractionalWidth(1.0),
|
|
|
|
heightDimension: .fractionalHeight(1.0))
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: itemSize)
|
|
|
|
let groupSize = NSCollectionLayoutSize(
|
|
|
|
widthDimension: .absolute(.minimumButtonDimension),
|
|
|
|
heightDimension: .absolute(.minimumButtonDimension))
|
|
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
|
|
|
|
let section = NSCollectionLayoutSection(group: group)
|
|
|
|
|
|
|
|
section.interGroupSpacing = .defaultSpacing
|
|
|
|
section.orthogonalScrollingBehavior = .continuous
|
|
|
|
section.contentInsets = NSDirectionalEdgeInsets(
|
|
|
|
top: .compactSpacing,
|
|
|
|
leading: .compactSpacing,
|
|
|
|
bottom: .compactSpacing,
|
|
|
|
trailing: .compactSpacing)
|
|
|
|
|
|
|
|
return section
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 00:32:08 +01:00
|
|
|
func visibilityMenu(selectedVisibility: Status.Visibility) -> UIMenu {
|
|
|
|
UIMenu(children: Status.Visibility.allCasesExceptUnknown.reversed().map { visibility in
|
|
|
|
UIAction(
|
|
|
|
title: visibility.title ?? "",
|
|
|
|
image: UIImage(systemName: visibility.systemImageName),
|
|
|
|
discoverabilityTitle: visibility.description,
|
|
|
|
state: visibility == selectedVisibility ? .on : .off) { [weak self] _ in
|
|
|
|
self?.parentViewModel.visibility = visibility
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-15 09:47:30 +01:00
|
|
|
|
|
|
|
func setAutocompleteCollectionViewHeight(_ height: CGFloat) {
|
|
|
|
let autocompleteCollectionViewHeight = min(max(height, .hairline), Self.autocompleteCollectionViewMaxHeight)
|
|
|
|
|
|
|
|
autocompleteCollectionViewHeightConstraint.constant = autocompleteCollectionViewHeight
|
|
|
|
autocompleteCollectionView.alpha = autocompleteCollectionViewHeightConstraint.constant == .hairline ? 0 : 1
|
|
|
|
|
|
|
|
heightConstraint?.constant = .minimumButtonDimension + autocompleteCollectionViewHeight
|
|
|
|
updateConstraints()
|
|
|
|
superview?.superview?.layoutIfNeeded()
|
|
|
|
}
|
2021-01-29 00:32:08 +01:00
|
|
|
}
|