feat: let text editor become first responder when compose scene appear

This commit is contained in:
CMK 2021-03-11 19:26:10 +08:00
parent 97ecbb1bfb
commit 2b2759c2cc
5 changed files with 80 additions and 26 deletions

View File

@ -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
}
}
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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 {
}

View File

@ -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)