2021-03-11 08:41:27 +01:00
|
|
|
//
|
|
|
|
// ComposeStatusSection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-11.
|
|
|
|
//
|
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-03-12 12:25:28 +01:00
|
|
|
import TwitterTextEditor
|
2021-03-18 08:16:35 +01:00
|
|
|
import AlamofireImage
|
2021-03-11 08:41:27 +01:00
|
|
|
|
|
|
|
enum ComposeStatusSection: Equatable, Hashable {
|
|
|
|
case repliedTo
|
|
|
|
case status
|
2021-03-17 11:09:38 +01:00
|
|
|
case attachment
|
2021-03-23 11:47:21 +01:00
|
|
|
case poll
|
2021-03-11 08:41:27 +01:00
|
|
|
}
|
2021-03-12 07:18:07 +01:00
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
|
|
|
enum ComposeKind {
|
2021-03-15 06:42:46 +01:00
|
|
|
case post
|
2021-04-07 11:10:58 +02:00
|
|
|
case hashtag(hashtag: String)
|
2021-04-02 12:50:08 +02:00
|
|
|
case mention(mastodonUserObjectID: NSManagedObjectID)
|
2021-03-15 06:42:46 +01:00
|
|
|
case reply(repliedToStatusObjectID: NSManagedObjectID)
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
2021-03-22 10:48:35 +01:00
|
|
|
static func collectionViewDiffableDataSource(
|
|
|
|
for collectionView: UICollectionView,
|
2021-03-12 07:18:07 +01:00
|
|
|
dependency: NeedsDependency,
|
|
|
|
managedObjectContext: NSManagedObjectContext,
|
2021-03-12 12:25:28 +01:00
|
|
|
composeKind: ComposeKind,
|
2021-04-15 06:10:43 +02:00
|
|
|
repliedToCellFrameSubscriber: CurrentValueSubject<CGRect, Never>,
|
2021-03-25 08:56:17 +01:00
|
|
|
customEmojiPickerInputViewModel: CustomEmojiPickerInputViewModel,
|
2021-03-18 08:16:35 +01:00
|
|
|
textEditorViewTextAttributesDelegate: TextEditorViewTextAttributesDelegate,
|
2021-03-23 11:47:21 +01:00
|
|
|
composeStatusAttachmentTableViewCellDelegate: ComposeStatusAttachmentCollectionViewCellDelegate,
|
|
|
|
composeStatusPollOptionCollectionViewCellDelegate: ComposeStatusPollOptionCollectionViewCellDelegate,
|
2021-03-24 07:49:27 +01:00
|
|
|
composeStatusNewPollOptionCollectionViewCellDelegate: ComposeStatusPollOptionAppendEntryCollectionViewCellDelegate,
|
|
|
|
composeStatusPollExpiresOptionCollectionViewCellDelegate: ComposeStatusPollExpiresOptionCollectionViewCellDelegate
|
2021-03-22 10:48:35 +01:00
|
|
|
) -> UICollectionViewDiffableDataSource<ComposeStatusSection, ComposeStatusItem> {
|
2021-03-26 07:50:23 +01:00
|
|
|
UICollectionViewDiffableDataSource(collectionView: collectionView) { [
|
|
|
|
weak customEmojiPickerInputViewModel,
|
|
|
|
weak textEditorViewTextAttributesDelegate,
|
|
|
|
weak composeStatusAttachmentTableViewCellDelegate,
|
|
|
|
weak composeStatusPollOptionCollectionViewCellDelegate,
|
|
|
|
weak composeStatusNewPollOptionCollectionViewCellDelegate,
|
|
|
|
weak composeStatusPollExpiresOptionCollectionViewCellDelegate
|
|
|
|
] collectionView, indexPath, item -> UICollectionViewCell? in
|
2021-03-12 07:18:07 +01:00
|
|
|
switch item {
|
2021-04-14 09:24:54 +02:00
|
|
|
case .replyTo(let replyToStatusObjectID):
|
2021-04-01 08:39:15 +02:00
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: ComposeRepliedToStatusContentCollectionViewCell.self), for: indexPath) as! ComposeRepliedToStatusContentCollectionViewCell
|
2021-04-14 09:24:54 +02:00
|
|
|
managedObjectContext.perform {
|
|
|
|
guard let replyTo = managedObjectContext.object(with: replyToStatusObjectID) as? Status else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let status = replyTo.reblog ?? replyTo
|
2021-04-14 09:59:29 +02:00
|
|
|
|
|
|
|
// set avatar
|
2021-04-14 09:24:54 +02:00
|
|
|
cell.statusView.configure(with: AvatarConfigurableViewConfiguration(avatarImageURL: status.author.avatarImageURL()))
|
2021-04-14 09:59:29 +02:00
|
|
|
// set name username
|
|
|
|
cell.statusView.nameLabel.text = {
|
|
|
|
let author = status.author
|
|
|
|
return author.displayName.isEmpty ? author.username : author.displayName
|
|
|
|
}()
|
|
|
|
cell.statusView.usernameLabel.text = "@" + (status.reblog ?? status).author.acct
|
|
|
|
// set text
|
2021-05-07 12:25:57 +02:00
|
|
|
//status.emoji
|
|
|
|
cell.statusView.activeTextLabel.configure(content: status.content, emojiDict: [:])
|
2021-04-14 09:59:29 +02:00
|
|
|
// set date
|
|
|
|
cell.statusView.dateLabel.text = status.createdAt.shortTimeAgoSinceNow
|
2021-04-15 06:10:43 +02:00
|
|
|
|
|
|
|
cell.framePublisher.assign(to: \.value, on: repliedToCellFrameSubscriber).store(in: &cell.disposeBag)
|
2021-04-14 09:24:54 +02:00
|
|
|
}
|
2021-03-12 07:18:07 +01:00
|
|
|
return cell
|
2021-04-01 08:39:15 +02:00
|
|
|
case .input(let replyToStatusObjectID, let attribute):
|
2021-03-22 10:48:35 +01:00
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: ComposeStatusContentCollectionViewCell.self), for: indexPath) as! ComposeStatusContentCollectionViewCell
|
2021-04-19 13:13:20 +02:00
|
|
|
cell.statusContentWarningEditorView.textView.text = attribute.contentWarningContent.value
|
2021-03-18 10:33:07 +01:00
|
|
|
cell.textEditorView.text = attribute.composeContent.value ?? ""
|
2021-03-12 07:18:07 +01:00
|
|
|
managedObjectContext.perform {
|
2021-04-01 08:39:15 +02:00
|
|
|
guard let replyToStatusObjectID = replyToStatusObjectID,
|
|
|
|
let replyTo = managedObjectContext.object(with: replyToStatusObjectID) as? Status else {
|
2021-04-19 11:50:58 +02:00
|
|
|
cell.statusView.headerContainerView.isHidden = true
|
2021-03-12 07:18:07 +01:00
|
|
|
return
|
|
|
|
}
|
2021-04-19 11:50:58 +02:00
|
|
|
cell.statusView.headerContainerView.isHidden = false
|
2021-04-14 09:24:54 +02:00
|
|
|
cell.statusView.headerIconLabel.attributedText = StatusView.iconAttributedString(image: StatusView.replyIconImage)
|
|
|
|
cell.statusView.headerInfoLabel.text = L10n.Scene.Compose.replyingToUser(replyTo.author.displayNameWithFallback)
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
2021-04-14 09:24:54 +02:00
|
|
|
ComposeStatusSection.configureStatusContent(cell: cell, attribute: attribute)
|
2021-03-12 12:25:28 +01:00
|
|
|
cell.textEditorView.textAttributesDelegate = textEditorViewTextAttributesDelegate
|
2021-03-12 07:18:07 +01:00
|
|
|
cell.composeContent
|
2021-03-18 10:33:07 +01:00
|
|
|
.removeDuplicates()
|
2021-03-12 07:18:07 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { text in
|
2021-03-23 11:47:21 +01:00
|
|
|
// self size input cell
|
2021-04-14 12:11:59 +02:00
|
|
|
// needs restore content offset to resolve issue #83
|
|
|
|
let oldContentOffset = collectionView.contentOffset
|
2021-03-22 10:48:35 +01:00
|
|
|
collectionView.collectionViewLayout.invalidateLayout()
|
2021-04-14 12:11:59 +02:00
|
|
|
collectionView.layoutIfNeeded()
|
|
|
|
collectionView.contentOffset = oldContentOffset
|
|
|
|
|
2021-03-18 10:33:07 +01:00
|
|
|
// bind input data
|
|
|
|
attribute.composeContent.value = text
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-25 11:17:05 +01:00
|
|
|
attribute.isContentWarningComposing
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { isContentWarningComposing in
|
|
|
|
// self size input cell
|
|
|
|
collectionView.collectionViewLayout.invalidateLayout()
|
|
|
|
cell.statusContentWarningEditorView.containerView.isHidden = !isContentWarningComposing
|
|
|
|
cell.statusContentWarningEditorView.alpha = 0
|
|
|
|
UIView.animate(withDuration: 0.33, delay: 0, options: [.curveEaseOut]) {
|
|
|
|
cell.statusContentWarningEditorView.alpha = 1
|
|
|
|
} completion: { _ in
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
cell.contentWarningContent
|
|
|
|
.removeDuplicates()
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { text in
|
|
|
|
// self size input cell
|
|
|
|
collectionView.collectionViewLayout.invalidateLayout()
|
|
|
|
// bind input data
|
|
|
|
attribute.contentWarningContent.value = text
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-25 08:56:17 +01:00
|
|
|
ComposeStatusSection.configureCustomEmojiPicker(viewModel: customEmojiPickerInputViewModel, customEmojiReplacableTextInput: cell.textEditorView, disposeBag: &cell.disposeBag)
|
2021-03-25 12:34:30 +01:00
|
|
|
ComposeStatusSection.configureCustomEmojiPicker(viewModel: customEmojiPickerInputViewModel, customEmojiReplacableTextInput: cell.statusContentWarningEditorView.textView, disposeBag: &cell.disposeBag)
|
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
return cell
|
2021-03-17 11:09:38 +01:00
|
|
|
case .attachment(let attachmentService):
|
2021-03-22 10:48:35 +01:00
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: ComposeStatusAttachmentCollectionViewCell.self), for: indexPath) as! ComposeStatusAttachmentCollectionViewCell
|
2021-03-18 10:33:07 +01:00
|
|
|
cell.attachmentContainerView.descriptionTextView.text = attachmentService.description.value
|
2021-03-18 08:16:35 +01:00
|
|
|
cell.delegate = composeStatusAttachmentTableViewCellDelegate
|
|
|
|
attachmentService.imageData
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { imageData in
|
2021-03-22 10:48:35 +01:00
|
|
|
let size = cell.attachmentContainerView.previewImageView.frame.size != .zero ? cell.attachmentContainerView.previewImageView.frame.size : CGSize(width: 1, height: 1)
|
2021-03-18 08:16:35 +01:00
|
|
|
guard let imageData = imageData,
|
|
|
|
let image = UIImage(data: imageData) else {
|
|
|
|
let placeholder = UIImage.placeholder(
|
2021-03-22 10:48:35 +01:00
|
|
|
size: size,
|
2021-03-18 08:16:35 +01:00
|
|
|
color: Asset.Colors.Background.systemGroupedBackground.color
|
|
|
|
)
|
|
|
|
.af.imageRounded(
|
|
|
|
withCornerRadius: AttachmentContainerView.containerViewCornerRadius
|
|
|
|
)
|
|
|
|
cell.attachmentContainerView.previewImageView.image = placeholder
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cell.attachmentContainerView.previewImageView.image = image
|
2021-03-22 10:48:35 +01:00
|
|
|
.af.imageAspectScaled(toFill: size)
|
2021-03-18 08:16:35 +01:00
|
|
|
.af.imageRounded(withCornerRadius: AttachmentContainerView.containerViewCornerRadius)
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-19 12:49:48 +01:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
attachmentService.uploadStateMachineSubject.eraseToAnyPublisher(),
|
|
|
|
attachmentService.error.eraseToAnyPublisher()
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { uploadState, error in
|
|
|
|
cell.attachmentContainerView.emptyStateView.isHidden = error == nil
|
2021-03-22 10:48:35 +01:00
|
|
|
cell.attachmentContainerView.descriptionBackgroundView.isHidden = error != nil
|
2021-03-19 12:49:48 +01:00
|
|
|
if let _ = error {
|
2021-03-18 08:16:35 +01:00
|
|
|
cell.attachmentContainerView.activityIndicatorView.stopAnimating()
|
2021-03-19 12:49:48 +01:00
|
|
|
} else {
|
|
|
|
guard let uploadState = uploadState else { return }
|
|
|
|
switch uploadState {
|
|
|
|
case is MastodonAttachmentService.UploadState.Finish,
|
|
|
|
is MastodonAttachmentService.UploadState.Fail:
|
|
|
|
cell.attachmentContainerView.activityIndicatorView.stopAnimating()
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2021-03-18 08:16:35 +01:00
|
|
|
}
|
2021-03-19 12:49:48 +01:00
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-18 10:33:07 +01:00
|
|
|
NotificationCenter.default.publisher(
|
|
|
|
for: UITextView.textDidChangeNotification,
|
|
|
|
object: cell.attachmentContainerView.descriptionTextView
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { notification in
|
|
|
|
guard let textField = notification.object as? UITextView else { return }
|
|
|
|
let text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
attachmentService.description.value = text
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-17 11:09:38 +01:00
|
|
|
return cell
|
2021-03-24 07:49:27 +01:00
|
|
|
case .pollOption(let attribute):
|
2021-03-23 11:47:21 +01:00
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: ComposeStatusPollOptionCollectionViewCell.self), for: indexPath) as! ComposeStatusPollOptionCollectionViewCell
|
|
|
|
cell.pollOptionView.optionTextField.text = attribute.option.value
|
2021-04-14 12:11:59 +02:00
|
|
|
cell.pollOptionView.optionTextField.placeholder = L10n.Scene.Compose.Poll.optionNumber(indexPath.item + 1)
|
2021-03-23 11:47:21 +01:00
|
|
|
cell.pollOption
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.assign(to: \.value, on: attribute.option)
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
cell.delegate = composeStatusPollOptionCollectionViewCellDelegate
|
2021-03-25 08:56:17 +01:00
|
|
|
ComposeStatusSection.configureCustomEmojiPicker(viewModel: customEmojiPickerInputViewModel, customEmojiReplacableTextInput: cell.pollOptionView.optionTextField, disposeBag: &cell.disposeBag)
|
2021-03-23 11:47:21 +01:00
|
|
|
return cell
|
2021-03-24 07:49:27 +01:00
|
|
|
case .pollOptionAppendEntry:
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: ComposeStatusPollOptionAppendEntryCollectionViewCell.self), for: indexPath) as! ComposeStatusPollOptionAppendEntryCollectionViewCell
|
2021-03-23 11:47:21 +01:00
|
|
|
cell.delegate = composeStatusNewPollOptionCollectionViewCellDelegate
|
|
|
|
return cell
|
2021-03-24 07:49:27 +01:00
|
|
|
case .pollExpiresOption(let attribute):
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: ComposeStatusPollExpiresOptionCollectionViewCell.self), for: indexPath) as! ComposeStatusPollExpiresOptionCollectionViewCell
|
|
|
|
cell.durationButton.setTitle(L10n.Scene.Compose.Poll.durationTime(attribute.expiresOption.value.title), for: .normal)
|
|
|
|
attribute.expiresOption
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { expiresOption in
|
|
|
|
cell.durationButton.setTitle(L10n.Scene.Compose.Poll.durationTime(expiresOption.title), for: .normal)
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
cell.delegate = composeStatusPollExpiresOptionCollectionViewCellDelegate
|
|
|
|
return cell
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
2021-03-25 08:56:17 +01:00
|
|
|
|
2021-04-14 09:24:54 +02:00
|
|
|
static func configureStatusContent(
|
2021-03-22 10:48:35 +01:00
|
|
|
cell: ComposeStatusContentCollectionViewCell,
|
2021-03-16 04:23:19 +01:00
|
|
|
attribute: ComposeStatusItem.ComposeStatusAttribute
|
2021-03-12 07:18:07 +01:00
|
|
|
) {
|
2021-03-12 08:57:58 +01:00
|
|
|
// set avatar
|
2021-03-12 07:18:07 +01:00
|
|
|
attribute.avatarURL
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { avatarURL in
|
|
|
|
cell.statusView.configure(with: AvatarConfigurableViewConfiguration(avatarImageURL: avatarURL))
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-12 08:57:58 +01:00
|
|
|
// set display name and username
|
2021-03-12 07:18:07 +01:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
attribute.displayName.eraseToAnyPublisher(),
|
|
|
|
attribute.username.eraseToAnyPublisher()
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { displayName, username in
|
|
|
|
cell.statusView.nameLabel.text = displayName
|
2021-03-26 07:50:23 +01:00
|
|
|
cell.statusView.usernameLabel.text = username.flatMap { "@" + $0 } ?? " "
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-12 08:57:58 +01:00
|
|
|
|
|
|
|
// bind compose content
|
|
|
|
cell.composeContent
|
|
|
|
.map { $0 as String? }
|
|
|
|
.assign(to: \.value, on: attribute.composeContent)
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|
2021-03-25 08:56:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol CustomEmojiReplacableTextInput: AnyObject {
|
|
|
|
var inputView: UIView? { get set }
|
|
|
|
func reloadInputViews()
|
|
|
|
|
|
|
|
// UIKeyInput
|
|
|
|
func insertText(_ text: String)
|
|
|
|
// UIResponder
|
|
|
|
var isFirstResponder: Bool { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
class CustomEmojiReplacableTextInputReference {
|
|
|
|
weak var value: CustomEmojiReplacableTextInput?
|
|
|
|
|
|
|
|
init(value: CustomEmojiReplacableTextInput? = nil) {
|
|
|
|
self.value = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TextEditorView: CustomEmojiReplacableTextInput {
|
|
|
|
func insertText(_ text: String) {
|
|
|
|
try? updateByReplacing(range: selectedRange, with: text, selectedRange: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
public override var isFirstResponder: Bool {
|
|
|
|
return isEditing
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
extension UITextField: CustomEmojiReplacableTextInput { }
|
|
|
|
extension UITextView: CustomEmojiReplacableTextInput { }
|
|
|
|
|
|
|
|
extension ComposeStatusSection {
|
|
|
|
|
|
|
|
static func configureCustomEmojiPicker(
|
|
|
|
viewModel: CustomEmojiPickerInputViewModel?,
|
|
|
|
customEmojiReplacableTextInput: CustomEmojiReplacableTextInput,
|
|
|
|
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 }
|
|
|
|
customEmojiReplacableTextInput.inputView = isCustomEmojiComposing ? viewModel.customEmojiPickerInputView : nil
|
|
|
|
customEmojiReplacableTextInput.reloadInputViews()
|
|
|
|
viewModel.append(customEmojiReplacableTextInput: customEmojiReplacableTextInput)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
}
|