metatext-app-ios-iphone-ipad/Views/CompositionView.swift

198 lines
8.0 KiB
Swift
Raw Normal View History

2020-12-10 03:44:06 +01:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Kingfisher
import UIKit
2021-01-01 01:49:59 +01:00
import ViewModels
2020-12-10 03:44:06 +01:00
2020-12-19 07:30:19 +01:00
final class CompositionView: UIView {
2020-12-10 03:44:06 +01:00
let avatarImageView = UIImageView()
2021-01-01 01:49:59 +01:00
let spoilerTextField = UITextField()
2020-12-10 03:44:06 +01:00
let textView = UITextView()
2021-01-03 22:55:04 +01:00
let textViewPlaceholder = UILabel()
2020-12-19 07:30:19 +01:00
let attachmentsCollectionView: UICollectionView
2021-01-04 00:57:40 +01:00
let attachmentUploadView: AttachmentUploadView
2020-12-10 03:44:06 +01:00
2021-01-01 01:49:59 +01:00
private let viewModel: CompositionViewModel
private let parentViewModel: NewStatusViewModel
2020-12-10 03:44:06 +01:00
private var cancellables = Set<AnyCancellable>()
2020-12-19 07:30:19 +01:00
private lazy var attachmentsDataSource: CompositionAttachmentsDataSource = {
2021-01-03 02:22:17 +01:00
let vm = viewModel
return .init(collectionView: attachmentsCollectionView) {
(vm.attachmentViewModels[$0.item], vm)
2021-01-01 01:49:59 +01:00
}
2020-12-19 07:30:19 +01:00
}()
2021-01-01 01:49:59 +01:00
init(viewModel: CompositionViewModel, parentViewModel: NewStatusViewModel) {
self.viewModel = viewModel
self.parentViewModel = parentViewModel
2020-12-10 03:44:06 +01:00
2020-12-19 07:30:19 +01:00
let itemSize = NSCollectionLayoutSize(
2021-01-03 02:22:17 +01:00
widthDimension: .estimated(Self.attachmentCollectionViewHeight),
heightDimension: .fractionalHeight(1))
2020-12-19 07:30:19 +01:00
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(
2021-01-03 02:22:17 +01:00
widthDimension: .estimated(Self.attachmentCollectionViewHeight),
heightDimension: .fractionalHeight(1))
2020-12-19 07:30:19 +01:00
let group = NSCollectionLayoutGroup.horizontal(
layoutSize: groupSize,
subitems: [item])
2021-01-03 02:22:17 +01:00
let section = NSCollectionLayoutSection(group: group)
2020-12-19 07:30:19 +01:00
2021-01-03 02:22:17 +01:00
section.interGroupSpacing = .defaultSpacing
let configuration = UICollectionViewCompositionalLayoutConfiguration()
configuration.scrollDirection = .horizontal
let attachmentsLayout = UICollectionViewCompositionalLayout(section: section, configuration: configuration)
2020-12-19 07:30:19 +01:00
attachmentsCollectionView = UICollectionView(frame: .zero, collectionViewLayout: attachmentsLayout)
2021-01-04 00:57:40 +01:00
attachmentUploadView = AttachmentUploadView(viewModel: viewModel)
2020-12-19 07:30:19 +01:00
2020-12-10 03:44:06 +01:00
super.init(frame: .zero)
initialSetup()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
2021-01-01 01:49:59 +01:00
extension CompositionView {
var id: CompositionViewModel.Id { viewModel.id }
2020-12-10 03:44:06 +01:00
}
2020-12-16 02:39:38 +01:00
extension CompositionView: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
2021-01-01 01:49:59 +01:00
viewModel.text = textView.text
2020-12-16 02:39:38 +01:00
}
}
2020-12-10 03:44:06 +01:00
private extension CompositionView {
2021-01-03 02:22:17 +01:00
static let attachmentCollectionViewHeight: CGFloat = 200
2020-12-17 07:48:06 +01:00
2021-01-01 01:49:59 +01:00
// swiftlint:disable:next function_body_length
2020-12-10 03:44:06 +01:00
func initialSetup() {
2021-01-01 01:49:59 +01:00
tag = viewModel.id.hashValue
2020-12-10 03:44:06 +01:00
addSubview(avatarImageView)
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.layer.cornerRadius = .avatarDimension / 2
avatarImageView.clipsToBounds = true
let stackView = UIStackView()
2021-01-01 01:49:59 +01:00
let inputAccessoryView = CompositionInputAccessoryView(viewModel: viewModel, parentViewModel: parentViewModel)
2020-12-10 03:44:06 +01:00
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
2021-01-01 01:49:59 +01:00
stackView.spacing = .defaultSpacing
stackView.addArrangedSubview(spoilerTextField)
2021-01-03 22:55:04 +01:00
spoilerTextField.borderStyle = .roundedRect
2021-01-01 01:49:59 +01:00
spoilerTextField.adjustsFontForContentSizeCategory = true
spoilerTextField.font = .preferredFont(forTextStyle: .body)
spoilerTextField.placeholder = NSLocalizedString("status.spoiler-text-placeholder", comment: "")
spoilerTextField.inputAccessoryView = inputAccessoryView
spoilerTextField.addAction(
UIAction { [weak self] _ in
guard let self = self, let text = self.spoilerTextField.text else { return }
self.viewModel.contentWarning = text
},
for: .editingChanged)
2020-12-10 03:44:06 +01:00
2021-01-03 22:55:04 +01:00
let textViewFont = UIFont.preferredFont(forTextStyle: .body)
2020-12-10 03:44:06 +01:00
stackView.addArrangedSubview(textView)
textView.isScrollEnabled = false
textView.adjustsFontForContentSizeCategory = true
2021-01-03 22:55:04 +01:00
textView.font = textViewFont
textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0
2021-01-01 01:49:59 +01:00
textView.inputAccessoryView = inputAccessoryView
2020-12-16 02:39:38 +01:00
textView.inputAccessoryView?.sizeToFit()
textView.delegate = self
2021-01-03 22:55:04 +01:00
textView.addSubview(textViewPlaceholder)
textViewPlaceholder.translatesAutoresizingMaskIntoConstraints = false
textViewPlaceholder.adjustsFontForContentSizeCategory = true
textViewPlaceholder.font = .preferredFont(forTextStyle: .body)
textViewPlaceholder.textColor = .secondaryLabel
textViewPlaceholder.text = NSLocalizedString("compose.prompt", comment: "")
2020-12-10 03:44:06 +01:00
2020-12-19 07:30:19 +01:00
stackView.addArrangedSubview(attachmentsCollectionView)
attachmentsCollectionView.dataSource = attachmentsDataSource
2021-01-03 02:22:17 +01:00
attachmentsCollectionView.backgroundColor = .clear
2020-12-17 07:48:06 +01:00
stackView.addArrangedSubview(attachmentUploadView)
2021-01-01 01:49:59 +01:00
textView.text = viewModel.text
spoilerTextField.text = viewModel.contentWarning
2020-12-12 01:41:37 +01:00
2021-01-03 22:55:04 +01:00
let textViewBaselineConstraint = textView.topAnchor.constraint(
lessThanOrEqualTo: avatarImageView.centerYAnchor,
constant: -textViewFont.lineHeight / 2)
viewModel.$text.map(\.isEmpty)
.sink { [weak self] in self?.textViewPlaceholder.isHidden = !$0 }
.store(in: &cancellables)
2021-01-01 01:49:59 +01:00
viewModel.$displayContentWarning
.sink { [weak self] in
guard let self = self else { return }
2020-12-12 01:41:37 +01:00
2021-01-01 01:49:59 +01:00
if self.spoilerTextField.isHidden && self.textView.isFirstResponder && $0 {
self.spoilerTextField.becomeFirstResponder()
} else if !self.spoilerTextField.isHidden && self.spoilerTextField.isFirstResponder && !$0 {
self.textView.becomeFirstResponder()
}
2020-12-20 00:05:14 +01:00
2021-01-01 01:49:59 +01:00
self.spoilerTextField.isHidden = !$0
2021-01-03 22:55:04 +01:00
textViewBaselineConstraint.isActive = !$0
2021-01-01 01:49:59 +01:00
}
.store(in: &cancellables)
2020-12-10 03:44:06 +01:00
2021-01-01 01:49:59 +01:00
parentViewModel.$identification.map(\.identity.image)
2020-12-10 03:44:06 +01:00
.sink { [weak self] in self?.avatarImageView.kf.setImage(with: $0) }
.store(in: &cancellables)
2020-12-17 07:48:06 +01:00
2021-01-01 01:49:59 +01:00
viewModel.$attachmentViewModels
2020-12-19 07:30:19 +01:00
.sink { [weak self] in
2021-01-01 21:18:10 +01:00
self?.attachmentsDataSource.apply($0.map(\.attachment).snapshot())
2020-12-19 07:30:19 +01:00
self?.attachmentsCollectionView.isHidden = $0.isEmpty
}
.store(in: &cancellables)
2021-01-01 01:49:59 +01:00
let guide = UIDevice.current.userInterfaceIdiom == .pad ? readableContentGuide : layoutMarginsGuide
let constraints = [
avatarImageView.heightAnchor.constraint(equalToConstant: .avatarDimension),
avatarImageView.widthAnchor.constraint(equalToConstant: .avatarDimension),
avatarImageView.topAnchor.constraint(equalTo: guide.topAnchor),
avatarImageView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
avatarImageView.bottomAnchor.constraint(lessThanOrEqualTo: guide.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: .defaultSpacing),
2021-01-03 22:55:04 +01:00
stackView.topAnchor.constraint(greaterThanOrEqualTo: guide.topAnchor),
2021-01-01 01:49:59 +01:00
stackView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
stackView.bottomAnchor.constraint(lessThanOrEqualTo: guide.bottomAnchor),
2021-01-03 22:55:04 +01:00
textViewPlaceholder.leadingAnchor.constraint(equalTo: textView.leadingAnchor),
textViewPlaceholder.topAnchor.constraint(equalTo: textView.topAnchor),
textViewPlaceholder.trailingAnchor.constraint(equalTo: textView.trailingAnchor),
2021-01-03 02:22:17 +01:00
attachmentsCollectionView.heightAnchor.constraint(equalToConstant: Self.attachmentCollectionViewHeight)
2021-01-01 01:49:59 +01:00
]
if UIDevice.current.userInterfaceIdiom == .pad {
for constraint in constraints {
constraint.priority = .justBelowMax
}
}
NSLayoutConstraint.activate(constraints)
2020-12-10 03:44:06 +01:00
}
}