metatext/View Controllers/NewStatusViewController.swift

495 lines
20 KiB
Swift
Raw Normal View History

2020-12-06 03:10:27 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-01-01 23:31:39 +00:00
import AVFoundation
2020-12-10 02:44:06 +00:00
import Combine
import Mastodon
2020-12-16 01:39:38 +00:00
import PhotosUI
2021-01-10 01:26:51 +00:00
import SwiftUI
2021-01-01 23:31:39 +00:00
import UniformTypeIdentifiers
2020-12-06 03:10:27 +00:00
import ViewModels
2021-01-14 17:49:53 +00:00
// swiftlint:disable file_length
2021-01-01 00:49:59 +00:00
final class NewStatusViewController: UIViewController {
2020-12-06 03:10:27 +00:00
private let viewModel: NewStatusViewModel
2021-02-04 21:33:29 +00:00
private let rootViewModel: RootViewModel?
2021-01-01 00:49:59 +00:00
private let scrollView = UIScrollView()
private let stackView = UIStackView()
2021-01-01 20:18:10 +00:00
private let activityIndicatorView = UIActivityIndicatorView(style: .large)
2020-12-16 01:39:38 +00:00
private let postButton = UIBarButtonItem(
2021-01-31 01:43:48 +00:00
title: nil,
2020-12-16 01:39:38 +00:00
style: .done,
target: nil,
action: nil)
2021-01-01 00:49:59 +00:00
private let mediaSelections = PassthroughSubject<[PHPickerResult], Never>()
2021-01-01 23:31:39 +00:00
private let imagePickerResults = PassthroughSubject<[UIImagePickerController.InfoKey: Any]?, Never>()
2021-01-10 07:08:45 +00:00
private let documentPickerResuls = PassthroughSubject<[URL]?, Never>()
2020-12-10 02:44:06 +00:00
private var cancellables = Set<AnyCancellable>()
2020-12-06 03:10:27 +00:00
2021-02-04 21:33:29 +00:00
init(viewModel: NewStatusViewModel, rootViewModel: RootViewModel?) {
2020-12-06 03:10:27 +00:00
self.viewModel = viewModel
2021-02-04 21:33:29 +00:00
self.rootViewModel = rootViewModel
2020-12-10 02:44:06 +00:00
2021-01-01 00:49:59 +00:00
super.init(nibName: nil, bundle: nil)
2021-01-27 01:42:32 +00:00
NotificationCenter.default.publisher(for: UIResponder.keyboardDidChangeFrameNotification)
.merge(with: NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification))
.sink { [weak self] in self?.adjustContentInset(notification: $0) }
.store(in: &cancellables)
2020-12-06 03:10:27 +00:00
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2021-01-31 01:43:48 +00:00
// swiftlint:disable:next function_body_length
2020-12-06 03:10:27 +00:00
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
2021-01-01 00:49:59 +00:00
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.distribution = .equalSpacing
2021-01-01 20:18:10 +00:00
scrollView.addSubview(activityIndicatorView)
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
activityIndicatorView.hidesWhenStopped = true
2021-01-01 00:49:59 +00:00
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
2021-01-01 20:18:10 +00:00
stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
activityIndicatorView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
activityIndicatorView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor)
2021-01-01 00:49:59 +00:00
])
2021-01-27 01:42:32 +00:00
navigationItem.leftBarButtonItem = UIBarButtonItem(
systemItem: .cancel,
primaryAction: UIAction { [weak self] _ in self?.dismiss() })
navigationItem.rightBarButtonItem = postButton
2021-01-21 08:45:09 +00:00
let postActionTitle = self.postActionTitle(
statusWord: viewModel.identityContext.appPreferences.statusWord,
visibility: viewModel.visibility)
2021-01-31 01:43:48 +00:00
postButton.primaryAction = UIAction(title: postActionTitle) { [weak self] _ in
2020-12-16 01:39:38 +00:00
self?.viewModel.post()
}
2021-01-10 05:56:15 +00:00
#if !IS_SHARE_EXTENSION
if let inReplyToViewModel = viewModel.inReplyToViewModel {
let statusView = StatusView(configuration: .init(viewModel: inReplyToViewModel))
statusView.isUserInteractionEnabled = false
statusView.bodyView.alpha = 0.5
2021-02-01 22:11:53 +00:00
statusView.buttonsStackView.isHidden_stackViewSafe = true
2021-01-10 05:56:15 +00:00
stackView.addArrangedSubview(statusView)
}
#endif
2021-01-01 00:49:59 +00:00
setupViewModelBindings()
}
}
2020-12-10 02:44:06 +00:00
2021-01-29 02:41:41 +00:00
extension NewStatusViewController {
static let newStatusPostedNotification = Notification.Name("com.metabolist.metatext.new-status-posted-notification")
}
2021-01-01 00:49:59 +00:00
extension NewStatusViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
2021-03-05 07:22:21 +00:00
dismiss(animated: true) {
self.mediaSelections.send(results)
}
2021-01-01 00:49:59 +00:00
}
}
2020-12-16 01:39:38 +00:00
2021-01-01 23:31:39 +00:00
extension NewStatusViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
2021-03-05 07:22:21 +00:00
dismiss(animated: true) {
self.imagePickerResults.send(info)
}
2021-01-01 23:31:39 +00:00
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
2021-03-05 07:22:21 +00:00
dismiss(animated: true) {
self.imagePickerResults.send(nil)
}
2021-01-01 23:31:39 +00:00
}
}
2021-01-10 07:08:45 +00:00
extension NewStatusViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
documentPickerResuls.send(urls)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
documentPickerResuls.send(nil)
}
}
2021-01-14 17:49:53 +00:00
extension NewStatusViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController,
traitCollection: UITraitCollection) -> UIModalPresentationStyle {
.none
}
}
2021-01-01 23:31:39 +00:00
// Required by UIImagePickerController
extension NewStatusViewController: UINavigationControllerDelegate {}
2021-01-01 00:49:59 +00:00
private extension NewStatusViewController {
func handle(event: NewStatusViewModel.Event) {
switch event {
case let .presentMediaPicker(compositionViewModel):
presentMediaPicker(compositionViewModel: compositionViewModel)
2021-01-01 23:31:39 +00:00
case let .presentCamera(compositionViewModel):
#if !IS_SHARE_EXTENSION
presentCamera(compositionViewModel: compositionViewModel)
#endif
2021-01-10 07:08:45 +00:00
case let .presentDocumentPicker(compositionViewModel):
presentDocumentPicker(compositionViewModel: compositionViewModel)
2021-01-14 17:49:53 +00:00
case let .presentEmojiPicker(tag):
presentEmojiPicker(tag: tag)
2021-01-10 01:26:51 +00:00
case let .editAttachment(attachmentViewModel, compositionViewModel):
presentAttachmentEditor(
attachmentViewModel: attachmentViewModel,
compositionViewModel: compositionViewModel)
2021-01-27 01:42:32 +00:00
case let .changeIdentity(identity):
changeIdentity(identity)
2021-01-01 00:49:59 +00:00
}
}
2021-01-01 20:18:10 +00:00
func apply(postingState: NewStatusViewModel.PostingState) {
switch postingState {
case .composing:
activityIndicatorView.stopAnimating()
stackView.isUserInteractionEnabled = true
stackView.alpha = 1
case .posting:
activityIndicatorView.startAnimating()
stackView.isUserInteractionEnabled = false
stackView.alpha = 0.5
case .done:
2021-01-29 02:41:41 +00:00
NotificationCenter.default.post(.init(name: Self.newStatusPostedNotification))
2021-01-01 20:18:10 +00:00
dismiss()
}
}
func set(compositionViewModels: [CompositionViewModel]) {
2021-01-23 06:15:52 +00:00
let diff = compositionViewModels.map(\.id)
.difference(from: stackView.arrangedSubviews.compactMap { ($0 as? CompositionView)?.id })
2021-01-01 20:18:10 +00:00
for insertion in diff.insertions {
guard case let .insert(index, id, _) = insertion,
let compositionViewModel = compositionViewModels.first(where: { $0.id == id })
else { continue }
let compositionView = CompositionView(
viewModel: compositionViewModel,
parentViewModel: viewModel)
2021-01-10 05:56:15 +00:00
let adjustedIndex = viewModel.inReplyToViewModel == nil ? index : index + 1
stackView.insertArrangedSubview(compositionView, at: adjustedIndex)
2021-01-01 20:18:10 +00:00
compositionView.textView.becomeFirstResponder()
DispatchQueue.main.async {
self.scrollView.scrollRectToVisible(
self.scrollView.convert(compositionView.frame, from: self.stackView),
animated: true)
}
}
for removal in diff.removals {
2021-01-19 19:59:20 +00:00
guard case let .remove(_, id, _) = removal,
let index = stackView.arrangedSubviews.firstIndex(where: { ($0 as? CompositionView)?.id == id })
else { continue }
if (stackView.arrangedSubviews[index] as? CompositionView)?.textView.isFirstResponder ?? false {
if index > 0 {
(stackView.arrangedSubviews[index - 1] as? CompositionView)?.textView.becomeFirstResponder()
} else if stackView.arrangedSubviews.count > index {
(stackView.arrangedSubviews[index + 1] as? CompositionView)?.textView.becomeFirstResponder()
}
}
2021-01-01 20:18:10 +00:00
2021-01-19 19:59:20 +00:00
stackView.arrangedSubviews[index].removeFromSuperview()
2021-01-01 20:18:10 +00:00
}
2021-01-10 06:32:41 +00:00
for compositionView in stackView.arrangedSubviews.compactMap({ $0 as? CompositionView }) {
2021-02-01 22:11:53 +00:00
compositionView.removeButton.isHidden_stackViewSafe = compositionViewModels.count == 1
compositionView.inReplyToView.isHidden_stackViewSafe = compositionView === stackView.arrangedSubviews.first
2021-01-10 06:32:41 +00:00
&& viewModel.inReplyToViewModel == nil
2021-02-01 22:11:53 +00:00
compositionView.hasReplyFollowingView.isHidden_stackViewSafe =
compositionView === stackView.arrangedSubviews.last
2021-01-10 06:32:41 +00:00
}
2021-01-01 20:18:10 +00:00
}
2021-01-01 00:49:59 +00:00
func dismiss() {
if let extensionContext = extensionContext {
extensionContext.completeRequest(returningItems: nil)
} else {
2021-02-04 21:33:29 +00:00
rootViewModel?.navigationViewModel?.presentedNewStatusViewModel = nil
2021-01-01 00:49:59 +00:00
}
}
func setupViewModelBindings() {
2021-01-01 20:18:10 +00:00
viewModel.events
.sink { [weak self] in self?.handle(event: $0) }
.store(in: &cancellables)
viewModel.$canPost
.sink { [weak self] in self?.postButton.isEnabled = $0 }
.store(in: &cancellables)
viewModel.$compositionViewModels
.sink { [weak self] in self?.set(compositionViewModels: $0) }
.store(in: &cancellables)
viewModel.$postingState
.sink { [weak self] in self?.apply(postingState: $0) }
2021-01-01 00:49:59 +00:00
.store(in: &cancellables)
2020-12-16 01:39:38 +00:00
viewModel.$alertItem
.compactMap { $0 }
2021-01-15 10:13:10 +00:00
.sink { [weak self] alertItem in
2021-02-06 00:30:57 +00:00
guard let self = self else { return }
if self.presentedViewController != nil {
self.dismiss(animated: true) {
self.present(alertItem: alertItem)
}
} else {
self.present(alertItem: alertItem)
2021-01-15 10:13:10 +00:00
}
2021-01-14 17:49:53 +00:00
}
2020-12-16 01:39:38 +00:00
.store(in: &cancellables)
viewModel.$visibility.removeDuplicates().sink { [weak self] in
guard let self = self else { return }
let postActionTitle = self.postActionTitle(
statusWord: self.viewModel.identityContext.appPreferences.statusWord,
visibility: $0)
self.postButton.primaryAction = UIAction(title: postActionTitle) { [weak self] _ in
self?.viewModel.post()
}
}
.store(in: &cancellables)
2020-12-06 03:10:27 +00:00
}
2021-01-01 00:49:59 +00:00
func presentMediaPicker(compositionViewModel: CompositionViewModel) {
mediaSelections.first().sink { [weak self] results in
guard let self = self, let result = results.first else { return }
self.viewModel.attach(itemProvider: result.itemProvider, to: compositionViewModel)
2020-12-10 02:44:06 +00:00
}
2021-01-01 00:49:59 +00:00
.store(in: &cancellables)
2020-12-10 02:44:06 +00:00
2021-01-01 00:49:59 +00:00
var configuration = PHPickerConfiguration()
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
configuration.preferredAssetRepresentationMode = .current
2020-12-16 01:39:38 +00:00
2021-01-03 22:37:06 +00:00
if !compositionViewModel.canAddNonImageAttachment {
configuration.filter = .images
}
2021-01-01 00:49:59 +00:00
let picker = PHPickerViewController(configuration: configuration)
picker.modalPresentationStyle = .overFullScreen
picker.delegate = self
2021-02-06 00:30:57 +00:00
present(picker, animated: true)
2020-12-10 02:44:06 +00:00
}
2021-01-01 23:31:39 +00:00
#if !IS_SHARE_EXTENSION
func presentCamera(compositionViewModel: CompositionViewModel) {
if AVCaptureDevice.authorizationStatus(for: .video) == .denied {
let alertController = UIAlertController(
title: NSLocalizedString("camera-access.title", comment: ""),
message: NSLocalizedString("camera-access.description", comment: ""),
preferredStyle: .alert)
let openSystemSettingsAction = UIAlertAction(
title: NSLocalizedString("camera-access.open-system-settings", comment: ""),
style: .default) { _ in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
UIApplication.shared.open(settingsUrl)
}
let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel) { _ in }
alertController.addAction(openSystemSettingsAction)
alertController.addAction(cancelAction)
present(alertController, animated: true)
return
}
imagePickerResults.first().sink { [weak self] in
guard let self = self, let info = $0 else { return }
if let url = info[.mediaURL] as? URL, let itemProvider = NSItemProvider(contentsOf: url) {
self.viewModel.attach(itemProvider: itemProvider, to: compositionViewModel)
} else if let image = info[.editedImage] as? UIImage ?? info[.originalImage] as? UIImage {
self.viewModel.attach(itemProvider: NSItemProvider(object: image), to: compositionViewModel)
}
}
.store(in: &cancellables)
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.allowsEditing = true
picker.modalPresentationStyle = .overFullScreen
picker.delegate = self
2021-01-03 22:37:06 +00:00
if compositionViewModel.canAddNonImageAttachment {
picker.mediaTypes = [UTType.image.description, UTType.movie.description]
} else {
picker.mediaTypes = [UTType.image.description]
}
2021-02-06 00:30:57 +00:00
present(picker, animated: true)
2021-01-01 23:31:39 +00:00
}
#endif
2021-01-10 07:08:45 +00:00
func presentDocumentPicker(compositionViewModel: CompositionViewModel) {
documentPickerResuls.first().sink { [weak self] in
guard let self = self,
let result = $0?.first,
result.startAccessingSecurityScopedResource(),
let itemProvider = NSItemProvider(contentsOf: result)
else { return }
self.viewModel.attach(itemProvider: itemProvider, to: compositionViewModel)
result.stopAccessingSecurityScopedResource()
}
.store(in: &cancellables)
let documentPickerController = UIDocumentPickerViewController(forOpeningContentTypes: [.image, .movie, .audio])
documentPickerController.delegate = self
documentPickerController.allowsMultipleSelection = false
documentPickerController.modalPresentationStyle = .overFullScreen
2021-02-06 00:30:57 +00:00
present(documentPickerController, animated: true)
2021-01-10 07:08:45 +00:00
}
2021-01-14 17:49:53 +00:00
func presentEmojiPicker(tag: Int) {
guard let fromView = view.viewWithTag(tag) else { return }
2021-02-06 00:30:57 +00:00
if fromView.inputView == nil {
let emojiPickerViewModel = EmojiPickerViewModel(identityContext: viewModel.identityContext)
emojiPickerViewModel.$alertItem.assign(to: \.alertItem, on: viewModel).store(in: &cancellables)
let emojiPickerController =
EmojiPickerViewController(viewModel: emojiPickerViewModel) { [weak self] picker, emoji in
guard let textInput = fromView as? UITextInput,
let selectedTextRange = textInput.selectedTextRange
else { return }
textInput.replace(selectedTextRange, withText: emoji.escaped.appending(" "))
if (self?.presentedViewController as? UINavigationController)?.viewControllers.first === picker {
self?.dismiss(animated: true)
}
} deletionAction: { _ in
(fromView as? UITextInput)?.deleteBackward()
} searchPresentationAction: { [weak self] picker, navigation in
(fromView as? UITextView)?.inputView = nil
(fromView as? UITextField)?.inputView = nil
fromView.reloadInputViews()
navigation.removeFromParent()
navigation.preferredContentSize = CGSize(width: 100, height: 100)
picker.searchBar.becomeFirstResponder()
self?.present(navigation, animated: true)
}
2021-01-14 17:49:53 +00:00
2021-02-06 00:30:57 +00:00
let pickerNavigation = UINavigationController(rootViewController: emojiPickerController)
2021-01-14 17:49:53 +00:00
2021-02-06 00:30:57 +00:00
(fromView as? UITextView)?.inputView = pickerNavigation.view
(fromView as? UITextField)?.inputView = pickerNavigation.view
2021-01-17 04:58:59 +00:00
} else {
2021-02-06 00:30:57 +00:00
(fromView as? UITextView)?.inputView = nil
(fromView as? UITextField)?.inputView = nil
2021-01-14 17:49:53 +00:00
}
2021-02-06 00:30:57 +00:00
fromView.reloadInputViews()
2021-01-14 17:49:53 +00:00
}
2021-01-10 01:26:51 +00:00
func presentAttachmentEditor(attachmentViewModel: AttachmentViewModel, compositionViewModel: CompositionViewModel) {
let editAttachmentsView = EditAttachmentView { (attachmentViewModel, compositionViewModel) }
let editAttachmentViewController = UIHostingController(rootView: editAttachmentsView)
let navigationController = UINavigationController(rootViewController: editAttachmentViewController)
navigationController.modalPresentationStyle = .overFullScreen
2021-02-06 00:30:57 +00:00
present(navigationController, animated: true)
2021-01-10 01:26:51 +00:00
}
2021-01-27 01:12:03 +00:00
func changeIdentity(_ identity: Identity) {
if viewModel.compositionViewModels.contains(where: { !$0.attachmentViewModels.isEmpty }) {
let alertController = UIAlertController(
title: nil,
message: NSLocalizedString("compose.attachments-will-be-discarded", comment: ""),
preferredStyle: .alert)
let okAction = UIAlertAction(
title: NSLocalizedString("ok", comment: ""),
style: .destructive) { [weak self] _ in
guard let self = self else { return }
for compositionViewModel in self.viewModel.compositionViewModels {
compositionViewModel.discardAttachments()
}
self.viewModel.setIdentity(identity)
}
let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel) { _ in }
alertController.addAction(okAction)
alertController.addAction(cancelAction)
present(alertController, animated: true)
} else {
viewModel.setIdentity(identity)
}
}
2021-01-22 06:12:29 +00:00
func adjustContentInset(notification: Notification) {
guard let keyboardFrameEnd = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
else { return }
let convertedFrame = self.view.convert(keyboardFrameEnd, from: view.window)
let contentInsetBottom: CGFloat
if notification.name == UIResponder.keyboardWillHideNotification {
contentInsetBottom = 0
} else {
contentInsetBottom = convertedFrame.height - view.safeAreaInsets.bottom
}
self.scrollView.contentInset.bottom = contentInsetBottom
self.scrollView.verticalScrollIndicatorInsets.bottom = contentInsetBottom
}
func postActionTitle(statusWord: AppPreferences.StatusWord, visibility: Status.Visibility) -> String {
switch (statusWord, visibility) {
case (_, .direct):
return NSLocalizedString("send", comment: "")
case (.toot, _):
return NSLocalizedString("toot", comment: "")
case (.post, _):
return NSLocalizedString("post", comment: "")
}
}
2020-12-06 03:10:27 +00:00
}
2021-01-14 17:49:53 +00:00
// swiftlint:enable file_length