From 2b2759c2ccd537700cc9cce81a6007d80ea5f09f Mon Sep 17 00:00:00 2001 From: CMK Date: Thu, 11 Mar 2021 19:26:10 +0800 Subject: [PATCH] feat: let text editor become first responder when compose scene appear --- .../Diffiable/Item/ComposeStatusItem.swift | 20 +------- .../Scene/Compose/ComposeViewController.swift | 30 +++++++++++ .../Compose/ComposeViewModel+Diffable.swift | 4 +- .../ComposeTootContentTableViewCell.swift | 51 +++++++++++++++++-- .../TableviewCell/StatusTableViewCell.swift | 1 - 5 files changed, 80 insertions(+), 26 deletions(-) diff --git a/Mastodon/Diffiable/Item/ComposeStatusItem.swift b/Mastodon/Diffiable/Item/ComposeStatusItem.swift index 35977f3d4..2e125f636 100644 --- a/Mastodon/Diffiable/Item/ComposeStatusItem.swift +++ b/Mastodon/Diffiable/Item/ComposeStatusItem.swift @@ -10,25 +10,7 @@ import CoreData enum ComposeStatusItem { case replyTo(tootObjectID: NSManagedObjectID) - case toot(attribute: InputAttribute) + case toot(replyToTootObjectID: NSManagedObjectID?) } extension ComposeStatusItem: Hashable { } - -extension ComposeStatusItem { - class InputAttribute: Hashable { - let hasReplyTo: Bool - - init(hasReplyTo: Bool) { - self.hasReplyTo = hasReplyTo - } - - func hash(into hasher: inout Hasher) { - hasher.combine(hasReplyTo) - } - - static func == (lhs: ComposeStatusItem.InputAttribute, rhs: ComposeStatusItem.InputAttribute) -> Bool { - return lhs.hasReplyTo == rhs.hasReplyTo - } - } -} diff --git a/Mastodon/Scene/Compose/ComposeViewController.swift b/Mastodon/Scene/Compose/ComposeViewController.swift index f5f2746d8..adab1dd91 100644 --- a/Mastodon/Scene/Compose/ComposeViewController.swift +++ b/Mastodon/Scene/Compose/ComposeViewController.swift @@ -59,6 +59,36 @@ extension ComposeViewController { } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + // Fix AutoLayout conflict issue + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + self.markTextViewEditorBecomeFirstResponser() + } + } + +} + +extension ComposeViewController { + private func markTextViewEditorBecomeFirstResponser() { + guard let diffableDataSource = viewModel.diffableDataSource else { return } + let items = diffableDataSource.snapshot().itemIdentifiers + for item in items { + switch item { + case .toot: + guard let indexPath = diffableDataSource.indexPath(for: item), + let cell = tableView.cellForRow(at: indexPath) as? ComposeTootContentTableViewCell else { + continue + } + cell.textEditorView.isEditing = true + return + default: + continue + } + } + } } extension ComposeViewController { diff --git a/Mastodon/Scene/Compose/ComposeViewModel+Diffable.swift b/Mastodon/Scene/Compose/ComposeViewModel+Diffable.swift index 65a608653..772bb97b6 100644 --- a/Mastodon/Scene/Compose/ComposeViewModel+Diffable.swift +++ b/Mastodon/Scene/Compose/ComposeViewModel+Diffable.swift @@ -30,9 +30,9 @@ extension ComposeViewModel { switch composeKind { case .replyToot(let tootObjectID): snapshot.appendItems([.replyTo(tootObjectID: tootObjectID)], toSection: .repliedTo) - snapshot.appendItems([.toot(attribute: ComposeStatusItem.InputAttribute(hasReplyTo: true))], toSection: .status) + snapshot.appendItems([.toot(replyToTootObjectID: tootObjectID)], toSection: .status) case .toot: - snapshot.appendItems([.toot(attribute: ComposeStatusItem.InputAttribute(hasReplyTo: false))], toSection: .status) + snapshot.appendItems([.toot(replyToTootObjectID: nil)], toSection: .status) } diffableDataSource.apply(snapshot, animatingDifferences: false) } diff --git a/Mastodon/Scene/Compose/TableViewCell/ComposeTootContentTableViewCell.swift b/Mastodon/Scene/Compose/TableViewCell/ComposeTootContentTableViewCell.swift index f26b19c62..6e7a2058c 100644 --- a/Mastodon/Scene/Compose/TableViewCell/ComposeTootContentTableViewCell.swift +++ b/Mastodon/Scene/Compose/TableViewCell/ComposeTootContentTableViewCell.swift @@ -6,10 +6,18 @@ // import UIKit +import TwitterTextEditor final class ComposeTootContentTableViewCell: UITableViewCell { let statusView = StatusView() + let textEditorView: TextEditorView = { + let textEditorView = TextEditorView() + textEditorView.font = .preferredFont(forTextStyle: .body) +// textEditorView.scrollView.isScrollEnabled = false + textEditorView.isScrollEnabled = false + return textEditorView + }() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) @@ -26,15 +34,50 @@ final class ComposeTootContentTableViewCell: UITableViewCell { extension ComposeTootContentTableViewCell { private func _init() { + selectionStyle = .none + statusView.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(statusView) NSLayoutConstraint.activate([ - statusView.topAnchor.constraint(equalTo: contentView.topAnchor), - statusView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), - statusView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), - statusView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), + statusView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20), + statusView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor), + statusView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor), ]) + statusView.statusContainerStackView.isHidden = true + statusView.actionToolbarContainer.isHidden = true + + textEditorView.translatesAutoresizingMaskIntoConstraints = false + contentView.addSubview(textEditorView) + NSLayoutConstraint.activate([ + textEditorView.topAnchor.constraint(equalTo: statusView.bottomAnchor, constant: 10), + textEditorView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), + textEditorView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), + contentView.bottomAnchor.constraint(equalTo: textEditorView.bottomAnchor, constant: 20), + textEditorView.heightAnchor.constraint(greaterThanOrEqualToConstant: 44).priority(.defaultHigh), + ]) + + // let containerStackView = UIStackView() + // containerStackView.axis = .vertical + // containerStackView.spacing = 8 + // containerStackView.translatesAutoresizingMaskIntoConstraints = false + // contentView.addSubview(containerStackView) + // NSLayoutConstraint.activate([ + // containerStackView.topAnchor.constraint(equalTo: statusView.bottomAnchor, constant: 10), + // containerStackView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor), + // containerStackView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor), + // contentView.bottomAnchor.constraint(equalTo: containerStackView.bottomAnchor, constant: 20), + // ]) + + // TODO: + } + + override func didMoveToWindow() { + super.didMoveToWindow() + } } +extension ComposeTootContentTableViewCell { + +} diff --git a/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift b/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift index 2f4000b95..230fe3dcd 100644 --- a/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift +++ b/Mastodon/Scene/Share/View/TableviewCell/StatusTableViewCell.swift @@ -20,7 +20,6 @@ protocol StatusTableViewCellDelegate: class { var playerViewControllerDelegate: AVPlayerViewControllerDelegate? { get } func statusTableViewCell(_ cell: StatusTableViewCell, playerViewControllerDidPressed playerViewController: AVPlayerViewController) - func statusTableViewCell(_ cell: StatusTableViewCell, statusView: StatusView, contentWarningActionButtonPressed button: UIButton) func statusTableViewCell(_ cell: StatusTableViewCell, mosaicImageViewContainer: MosaicImageViewContainer, didTapContentWarningVisualEffectView visualEffectView: UIVisualEffectView) func statusTableViewCell(_ cell: StatusTableViewCell, mosaicImageViewContainer: MosaicImageViewContainer, didTapImageView imageView: UIImageView, atIndex index: Int)