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

103 lines
3.2 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
2021-07-22 13:34:24 +02:00
import MetaTextKit
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,
2021-07-23 13:10:27 +02:00
attribute.emojiMeta,
attribute.username
)
.receive(on: DispatchQueue.main)
2021-07-23 13:10:27 +02:00
.sink { displayName, emojiMeta, username in
do {
let mastodonContent = MastodonContent(content: displayName ?? " ", emojis: emojiMeta)
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
cell.statusView.nameLabel.configure(content: metaContent)
} catch {
let metaContent = PlaintextMetaContent(string: " ")
cell.statusView.nameLabel.configure(content: metaContent)
}
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)
}
}