mastodon-ios/Mastodon/Diffiable/Section/ComposeStatusSection.swift

96 lines
2.9 KiB
Swift
Raw Normal View History

2021-03-11 08:41:27 +01:00
//
// 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
2021-03-11 08:41:27 +01:00
enum ComposeStatusSection: Equatable, Hashable {
case repliedTo
case status
case attachment
2021-03-23 11:47:21 +01:00
case poll
2021-03-11 08:41:27 +01:00
}
extension ComposeStatusSection {
enum ComposeKind {
2021-03-15 06:42:46 +01:00
case post
case hashtag(hashtag: String)
case mention(mastodonUserObjectID: NSManagedObjectID)
2021-03-15 06:42:46 +01:00
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.CombineLatest3(
attribute.displayName,
attribute.emojiDict,
attribute.username.eraseToAnyPublisher()
)
.receive(on: DispatchQueue.main)
.sink { displayName, emojiDict, username in
cell.statusView.nameLabel.configure(content: displayName ?? " ", emojiDict: emojiDict)
cell.statusView.usernameLabel.text = username.flatMap { "@" + $0 } ?? " "
}
.store(in: &cell.disposeBag)
}
2021-03-25 08:56:17 +01:00
}
protocol CustomEmojiReplaceableTextInput: UITextInput & UIResponder {
2021-03-25 08:56:17 +01:00
var inputView: UIView? { get set }
}
class CustomEmojiReplaceableTextInputReference {
weak var value: CustomEmojiReplaceableTextInput?
2021-03-25 08:56:17 +01:00
init(value: CustomEmojiReplaceableTextInput? = nil) {
2021-03-25 08:56:17 +01:00
self.value = value
}
}
extension UITextField: CustomEmojiReplaceableTextInput { }
extension UITextView: CustomEmojiReplaceableTextInput { }
2021-03-25 08:56:17 +01:00
extension ComposeStatusSection {
static func configureCustomEmojiPicker(
viewModel: CustomEmojiPickerInputViewModel?,
customEmojiReplaceableTextInput: CustomEmojiReplaceableTextInput,
2021-03-25 08:56:17 +01:00
disposeBag: inout Set<AnyCancellable>
) {
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)
2021-03-25 08:56:17 +01:00
}
.store(in: &disposeBag)
}
}