Use UIToolbar for inputAccessoryView

This commit is contained in:
Justin Mazzocchi 2021-01-18 00:24:11 -08:00
parent a51d9aafae
commit b164a6265e
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -6,15 +6,11 @@ import Mastodon
import UIKit import UIKit
import ViewModels import ViewModels
final class CompositionInputAccessoryView: UIView { final class CompositionInputAccessoryView: UIToolbar {
let visibilityButton = UIButton()
let addButton = UIButton()
let contentWarningButton = UIButton(type: .system)
let tagForInputView = UUID().hashValue let tagForInputView = UUID().hashValue
private let viewModel: CompositionViewModel private let viewModel: CompositionViewModel
private let parentViewModel: NewStatusViewModel private let parentViewModel: NewStatusViewModel
private let stackView = UIStackView()
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
init(viewModel: CompositionViewModel, parentViewModel: NewStatusViewModel) { init(viewModel: CompositionViewModel, parentViewModel: NewStatusViewModel) {
@ -30,22 +26,11 @@ final class CompositionInputAccessoryView: UIView {
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
override var intrinsicContentSize: CGSize {
stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
}
} }
private extension CompositionInputAccessoryView { private extension CompositionInputAccessoryView {
// swiftlint:disable:next function_body_length // swiftlint:disable:next function_body_length
func initialSetup() { func initialSetup() {
autoresizingMask = .flexibleHeight
backgroundColor = .secondarySystemFill
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.spacing = .defaultSpacing
var attachmentActions = [ var attachmentActions = [
UIAction( UIAction(
title: NSLocalizedString("compose.browse", comment: ""), title: NSLocalizedString("compose.browse", comment: ""),
@ -74,75 +59,63 @@ private extension CompositionInputAccessoryView {
at: 1) at: 1)
#endif #endif
let attachmentButton = UIButton() let attachmentButton = UIBarButtonItem(
stackView.addArrangedSubview(attachmentButton) title: "hm",
attachmentButton.setImage( image: UIImage(systemName: "paperclip"),
UIImage( menu: UIMenu(children: attachmentActions))
systemName: "paperclip", let pollButton = UIBarButtonItem(
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)), image: UIImage(systemName: "chart.bar.xaxis"),
for: .normal) primaryAction: UIAction { [weak self] _ in self?.viewModel.displayPoll.toggle() })
attachmentButton.showsMenuAsPrimaryAction = true let visibilityButton = UIBarButtonItem(
attachmentButton.menu = UIMenu(children: attachmentActions) image: UIImage(systemName: parentViewModel.visibility.systemImageName),
menu: UIMenu(children: Status.Visibility.allCasesExceptUnknown.reversed().map { visibility in
UIAction(
title: visibility.title ?? "",
image: UIImage(systemName: visibility.systemImageName),
discoverabilityTitle: visibility.description) { [weak self] _ in
self?.parentViewModel.visibility = visibility
}
}))
let contentWarningButton = UIBarButtonItem(
title: NSLocalizedString("status.content-warning-abbreviation", comment: ""),
primaryAction: UIAction { [weak self] _ in self?.viewModel.displayContentWarning.toggle() })
let emojiButton = UIBarButtonItem(
image: UIImage(systemName: "face.smiling"),
primaryAction: UIAction { [weak self] _ in
guard let self = self else { return }
let pollButton = UIButton(primaryAction: UIAction { [weak self] _ in self?.viewModel.displayPoll.toggle() }) self.parentViewModel.presentEmojiPicker(tag: self.tagForInputView)
})
let addButton = UIBarButtonItem(
image: UIImage(systemName: "plus.circle.fill"),
primaryAction: UIAction { [weak self] _ in
guard let self = self else { return }
stackView.addArrangedSubview(pollButton) self.parentViewModel.insert(after: self.viewModel)
pollButton.setImage( })
UIImage(
systemName: "chart.bar.xaxis",
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)),
for: .normal)
stackView.addArrangedSubview(visibilityButton)
visibilityButton.showsMenuAsPrimaryAction = true
visibilityButton.menu = UIMenu(children: Status.Visibility.allCasesExceptUnknown.reversed().map { visibility in
UIAction(
title: visibility.title ?? "",
image: UIImage(systemName: visibility.systemImageName),
discoverabilityTitle: visibility.description) { [weak self] _ in
self?.parentViewModel.visibility = visibility
}
})
stackView.addArrangedSubview(contentWarningButton)
contentWarningButton.setTitle(
NSLocalizedString("status.content-warning-abbreviation", comment: ""),
for: .normal)
contentWarningButton.addAction(
UIAction { [weak self] _ in self?.viewModel.displayContentWarning.toggle() },
for: .touchUpInside)
let emojiButton = UIButton(primaryAction: UIAction { [weak self] _ in
guard let self = self else { return }
self.parentViewModel.presentEmojiPicker(tag: self.tagForInputView)
})
stackView.addArrangedSubview(emojiButton)
emojiButton.setImage(
UIImage(
systemName: "face.smiling",
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)),
for: .normal)
stackView.addArrangedSubview(UIView())
let charactersLabel = UILabel() let charactersLabel = UILabel()
stackView.addArrangedSubview(charactersLabel)
charactersLabel.font = .preferredFont(forTextStyle: .callout) charactersLabel.font = .preferredFont(forTextStyle: .callout)
charactersLabel.adjustsFontForContentSizeCategory = true
charactersLabel.adjustsFontSizeToFitWidth = true
stackView.addArrangedSubview(addButton) let charactersBarItem = UIBarButtonItem(customView: charactersLabel)
addButton.setImage(
UIImage(
systemName: "plus.circle.fill",
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)),
for: .normal)
addButton.addAction(UIAction { [weak self] _ in
guard let self = self else { return }
self.parentViewModel.insert(after: self.viewModel) items = [
}, for: .touchUpInside) attachmentButton,
UIBarButtonItem.fixedSpace(.defaultSpacing),
pollButton,
UIBarButtonItem.fixedSpace(.defaultSpacing),
visibilityButton,
UIBarButtonItem.fixedSpace(.defaultSpacing),
contentWarningButton,
UIBarButtonItem.fixedSpace(.defaultSpacing),
emojiButton,
UIBarButtonItem.flexibleSpace(),
charactersBarItem,
UIBarButtonItem.fixedSpace(.defaultSpacing),
addButton]
viewModel.$canAddAttachment viewModel.$canAddAttachment
.sink { attachmentButton.isEnabled = $0 } .sink { attachmentButton.isEnabled = $0 }
@ -160,25 +133,11 @@ private extension CompositionInputAccessoryView {
.store(in: &cancellables) .store(in: &cancellables)
viewModel.$isPostable viewModel.$isPostable
.sink { [weak self] in self?.addButton.isEnabled = $0 } .sink { addButton.isEnabled = $0 }
.store(in: &cancellables) .store(in: &cancellables)
parentViewModel.$visibility parentViewModel.$visibility
.sink { [weak self] in .sink { visibilityButton.image = UIImage(systemName: $0.systemImageName) }
self?.visibilityButton.setImage(UIImage(systemName: $0.systemImageName), for: .normal)
}
.store(in: &cancellables) .store(in: &cancellables)
for button in [attachmentButton, pollButton, visibilityButton, contentWarningButton, emojiButton, addButton] {
button.heightAnchor.constraint(greaterThanOrEqualToConstant: .minimumButtonDimension).isActive = true
button.widthAnchor.constraint(greaterThanOrEqualToConstant: .minimumButtonDimension).isActive = true
}
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
} }
} }