// // ComposeStatusSection.swift // Mastodon // // Created by MainasuK Cirno on 2021-3-11. // import UIKit import Combine import CoreData import CoreDataStack import MetaTextView import MastodonMeta import AlamofireImage enum ComposeStatusSection: Equatable, Hashable { case repliedTo case status case attachment case poll } extension ComposeStatusSection { enum ComposeKind { case post case hashtag(hashtag: String) case mention(mastodonUserObjectID: NSManagedObjectID) case reply(repliedToStatusObjectID: NSManagedObjectID) } } extension ComposeStatusSection { static func configureStatusContent( cell: ComposeStatusContentTableViewCell, attribute: ComposeStatusItem.ComposeStatusAttribute ) { // set avatar attribute.avatarURL .receive(on: DispatchQueue.main) .sink { avatarURL in cell.statusView.configure(with: AvatarConfigurableViewConfiguration(avatarImageURL: avatarURL)) } .store(in: &cell.disposeBag) // set display name and username Publishers.CombineLatest( attribute.displayName.eraseToAnyPublisher(), attribute.username.eraseToAnyPublisher() ) .receive(on: DispatchQueue.main) .sink { displayName, username in cell.statusView.nameLabel.text = displayName cell.statusView.usernameLabel.text = username.flatMap { "@" + $0 } ?? " " } .store(in: &cell.disposeBag) } } protocol CustomEmojiReplaceableTextInput: AnyObject { var inputView: UIView? { get set } func reloadInputViews() // UIKeyInput func insertText(_ text: String) // UIResponder var isFirstResponder: Bool { get } } class CustomEmojiReplaceableTextInputReference { weak var value: CustomEmojiReplaceableTextInput? init(value: CustomEmojiReplaceableTextInput? = nil) { self.value = value } } extension UITextField: CustomEmojiReplaceableTextInput { } extension UITextView: CustomEmojiReplaceableTextInput { } extension ComposeStatusSection { static func configureCustomEmojiPicker( viewModel: CustomEmojiPickerInputViewModel?, customEmojiReplaceableTextInput: CustomEmojiReplaceableTextInput, disposeBag: inout Set ) { guard let viewModel = viewModel else { return } viewModel.isCustomEmojiComposing .receive(on: DispatchQueue.main) .sink { [weak viewModel] isCustomEmojiComposing in guard let viewModel = viewModel else { return } customEmojiReplaceableTextInput.inputView = isCustomEmojiComposing ? viewModel.customEmojiPickerInputView : nil customEmojiReplaceableTextInput.reloadInputViews() viewModel.append(customEmojiReplaceableTextInput: customEmojiReplaceableTextInput) } .store(in: &disposeBag) } }