2020-12-16 01:39:38 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2021-01-01 23:31:39 +00:00
|
|
|
import AVFoundation
|
2020-12-16 01:39:38 +00:00
|
|
|
import Combine
|
2021-01-01 00:49:59 +00:00
|
|
|
import Mastodon
|
2020-12-16 01:39:38 +00:00
|
|
|
import UIKit
|
|
|
|
import ViewModels
|
|
|
|
|
2021-02-15 08:47:30 +00:00
|
|
|
final class CompositionInputAccessoryView: UIView {
|
2021-01-14 17:49:53 +00:00
|
|
|
let tagForInputView = UUID().hashValue
|
2021-02-15 21:52:28 +00:00
|
|
|
let autocompleteSelections: AnyPublisher<String, Never>
|
2021-01-01 00:49:59 +00:00
|
|
|
|
2020-12-16 01:39:38 +00:00
|
|
|
private let viewModel: CompositionViewModel
|
2021-01-01 00:49:59 +00:00
|
|
|
private let parentViewModel: NewStatusViewModel
|
2021-02-15 08:47:30 +00: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 21:52:28 +00:00
|
|
|
private let autocompleteSelectionsSubject = PassthroughSubject<String, Never>()
|
2020-12-16 01:39:38 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-02-14 02:28:30 +00:00
|
|
|
init(viewModel: CompositionViewModel,
|
|
|
|
parentViewModel: NewStatusViewModel,
|
|
|
|
autocompleteQueryPublisher: AnyPublisher<String?, Never>) {
|
2020-12-16 01:39:38 +00:00
|
|
|
self.viewModel = viewModel
|
2021-01-01 00:49:59 +00:00
|
|
|
self.parentViewModel = parentViewModel
|
2021-02-15 08:47:30 +00:00
|
|
|
autocompleteDataSource = AutocompleteDataSource(
|
|
|
|
collectionView: autocompleteCollectionView,
|
|
|
|
queryPublisher: autocompleteQueryPublisher,
|
|
|
|
parentViewModel: parentViewModel)
|
|
|
|
autocompleteCollectionViewHeightConstraint =
|
2021-02-15 21:52:28 +00:00
|
|
|
autocompleteCollectionView.heightAnchor.constraint(equalToConstant: .hairline)
|
|
|
|
autocompleteSelections = autocompleteSelectionsSubject.eraseToAnyPublisher()
|
2020-12-16 01:39:38 +00:00
|
|
|
|
2021-01-19 05:16:21 +00:00
|
|
|
super.init(
|
|
|
|
frame: .init(
|
|
|
|
origin: .zero,
|
|
|
|
size: .init(width: UIScreen.main.bounds.width, height: .minimumButtonDimension)))
|
2020-12-16 01:39:38 +00:00
|
|
|
|
|
|
|
initialSetup()
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2021-02-15 21:52:28 +00:00
|
|
|
|
|
|
|
override func didMoveToSuperview() {
|
|
|
|
super.didMoveToSuperview()
|
|
|
|
|
|
|
|
layoutIfNeeded()
|
|
|
|
}
|
2020-12-16 01:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension CompositionInputAccessoryView {
|
2021-02-15 08:47:30 +00:00
|
|
|
static let autocompleteCollectionViewMaxHeight: CGFloat = 150
|
|
|
|
|
|
|
|
var heightConstraint: NSLayoutConstraint? {
|
|
|
|
superview?.constraints.first(where: { $0.identifier == "accessoryHeight" })
|
|
|
|
}
|
|
|
|
|
2021-01-01 00:49:59 +00:00
|
|
|
// swiftlint:disable:next function_body_length
|
2020-12-16 01:39:38 +00:00
|
|
|
func initialSetup() {
|
2021-01-19 05:16:21 +00:00
|
|
|
autoresizingMask = .flexibleHeight
|
|
|
|
|
2021-02-15 08:47:30 +00:00
|
|
|
addSubview(autocompleteCollectionView)
|
|
|
|
autocompleteCollectionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
autocompleteCollectionView.alwaysBounceVertical = false
|
|
|
|
autocompleteCollectionView.backgroundColor = .clear
|
|
|
|
autocompleteCollectionView.layer.cornerRadius = .defaultCornerRadius
|
|
|
|
autocompleteCollectionView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
|
2021-02-15 19:17:06 +00:00
|
|
|
autocompleteCollectionView.dataSource = autocompleteDataSource
|
|
|
|
autocompleteCollectionView.delegate = self
|
2021-02-15 08:47:30 +00: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 01:45:25 +00:00
|
|
|
|
2021-01-10 07:08:45 +00: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 00:49:59 +00:00
|
|
|
|
2021-01-10 07:08:45 +00: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 01:39:38 +00:00
|
|
|
|
2021-01-10 07:08:45 +00:00
|
|
|
self.parentViewModel.presentMediaPicker(viewModel: self.viewModel)
|
|
|
|
}
|
|
|
|
]
|
2021-01-03 22:37:06 +00:00
|
|
|
|
2021-01-01 23:31:39 +00:00
|
|
|
#if !IS_SHARE_EXTENSION
|
2021-01-10 07:08:45 +00: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-01 23:31:39 +00:00
|
|
|
|
2021-01-10 07:08:45 +00:00
|
|
|
self.parentViewModel.presentCamera(viewModel: self.viewModel)
|
|
|
|
},
|
|
|
|
at: 1)
|
2021-01-01 23:31:39 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
let attachmentButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "paperclip"),
|
|
|
|
menu: UIMenu(children: attachmentActions))
|
2021-02-01 19:53:47 +00:00
|
|
|
|
|
|
|
attachmentButton.accessibilityLabel =
|
|
|
|
NSLocalizedString("compose.attachments-button.accessibility-label", comment: "")
|
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
let pollButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "chart.bar.xaxis"),
|
|
|
|
primaryAction: UIAction { [weak self] _ in self?.viewModel.displayPoll.toggle() })
|
2021-02-01 19:53:47 +00:00
|
|
|
|
|
|
|
pollButton.accessibilityLabel = NSLocalizedString("compose.poll-button.accessibility-label", comment: "")
|
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
let visibilityButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: parentViewModel.visibility.systemImageName),
|
2021-01-28 23:32:08 +00:00
|
|
|
menu: visibilityMenu(selectedVisibility: parentViewModel.visibility))
|
2021-02-01 19:53:47 +00:00
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
let contentWarningButton = UIBarButtonItem(
|
|
|
|
title: NSLocalizedString("status.content-warning-abbreviation", comment: ""),
|
|
|
|
primaryAction: UIAction { [weak self] _ in self?.viewModel.displayContentWarning.toggle() })
|
2021-02-01 19:53:47 +00: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 08:24:11 +00:00
|
|
|
let emojiButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "face.smiling"),
|
|
|
|
primaryAction: UIAction { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2021-01-14 17:49:53 +00:00
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
self.parentViewModel.presentEmojiPicker(tag: self.tagForInputView)
|
|
|
|
})
|
2021-02-01 19:53:47 +00:00
|
|
|
|
|
|
|
emojiButton.accessibilityLabel = NSLocalizedString("compose.emoji-button", comment: "")
|
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
let addButton = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "plus.circle.fill"),
|
|
|
|
primaryAction: UIAction { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
2021-01-14 17:49:53 +00:00
|
|
|
|
2021-01-18 08:24:11 +00:00
|
|
|
self.parentViewModel.insert(after: self.viewModel)
|
|
|
|
})
|
2020-12-16 01:39:38 +00:00
|
|
|
|
2021-02-01 19:53:47 +00: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 08:47:30 +00:00
|
|
|
let charactersBarItem = UIBarButtonItem()
|
2021-01-18 08:24:11 +00:00
|
|
|
|
2021-02-15 08:47:30 +00:00
|
|
|
charactersBarItem.isEnabled = false
|
2021-01-18 08:24:11 +00:00
|
|
|
|
2021-02-15 08:47:30 +00:00
|
|
|
toolbar.items = [
|
2021-01-18 08:24:11 +00: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 00:49:59 +00:00
|
|
|
|
2021-01-10 07:08:45 +00:00
|
|
|
viewModel.$canAddAttachment
|
|
|
|
.sink { attachmentButton.isEnabled = $0 }
|
|
|
|
.store(in: &cancellables)
|
2021-01-04 01:15:50 +00:00
|
|
|
|
2021-01-11 00:06:20 +00:00
|
|
|
viewModel.$attachmentViewModels
|
|
|
|
.combineLatest(viewModel.$attachmentUpload)
|
|
|
|
.sink { pollButton.isEnabled = $0.isEmpty && $1 == nil }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-04 01:15:50 +00:00
|
|
|
viewModel.$remainingCharacters.sink {
|
2021-02-15 08:47:30 +00:00
|
|
|
charactersBarItem.title = String($0)
|
|
|
|
charactersBarItem.setTitleTextAttributes(
|
|
|
|
[.foregroundColor: $0 < 0 ? UIColor.systemRed : UIColor.label],
|
|
|
|
for: .disabled)
|
|
|
|
charactersBarItem.accessibilityHint = String.localizedStringWithFormat(
|
2021-02-01 19:53:47 +00:00
|
|
|
NSLocalizedString("compose.characters-remaining-accessibility-label-%ld", comment: ""),
|
|
|
|
$0)
|
2021-01-04 01:15:50 +00:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
2021-01-03 22:37:06 +00:00
|
|
|
|
2021-01-01 00:49:59 +00:00
|
|
|
viewModel.$isPostable
|
2021-01-18 08:24:11 +00:00
|
|
|
.sink { addButton.isEnabled = $0 }
|
2021-01-01 00:49:59 +00:00
|
|
|
.store(in: &cancellables)
|
2020-12-16 01:39:38 +00:00
|
|
|
|
2021-02-15 08:47:30 +00: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 02:28:30 +00:00
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-01 00:49:59 +00:00
|
|
|
parentViewModel.$visibility
|
2021-01-28 23:32:08 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
visibilityButton.image = UIImage(systemName: $0.systemImageName)
|
|
|
|
visibilityButton.menu = self?.visibilityMenu(selectedVisibility: $0)
|
2021-02-03 06:00:29 +00:00
|
|
|
visibilityButton.accessibilityLabel = String.localizedStringWithFormat(
|
|
|
|
NSLocalizedString("compose.visibility-button.accessibility-label-%@", comment: ""),
|
|
|
|
$0.title ?? "")
|
2021-01-28 23:32:08 +00:00
|
|
|
}
|
2021-01-01 00:49:59 +00:00
|
|
|
.store(in: &cancellables)
|
2020-12-16 01:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 23:32:08 +00:00
|
|
|
|
2021-02-15 19:17:06 +00:00
|
|
|
extension CompositionInputAccessoryView: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
2021-02-15 21:52:28 +00: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 19:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 23:32:08 +00:00
|
|
|
private extension CompositionInputAccessoryView {
|
2021-02-15 08:47:30 +00: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-28 23:32:08 +00: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 08:47:30 +00: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-28 23:32:08 +00:00
|
|
|
}
|