2022-09-30 13:28:09 +02:00
|
|
|
//
|
|
|
|
// ComposeContentViewModel.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 22/9/30.
|
|
|
|
//
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
import os.log
|
2022-10-28 13:06:18 +02:00
|
|
|
import UIKit
|
2022-10-11 12:31:40 +02:00
|
|
|
import Combine
|
2022-10-10 13:14:52 +02:00
|
|
|
import CoreDataStack
|
2022-10-11 12:31:40 +02:00
|
|
|
import Meta
|
2022-10-28 13:06:18 +02:00
|
|
|
import MetaTextKit
|
2022-10-31 13:41:19 +01:00
|
|
|
import MastodonMeta
|
|
|
|
import MastodonCore
|
|
|
|
import MastodonSDK
|
2022-09-30 13:28:09 +02:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public final class ComposeContentViewModel: NSObject, ObservableObject {
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
let logger = Logger(subsystem: "ComposeContentViewModel", category: "ViewModel")
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
// tableViewCell
|
|
|
|
let composeReplyToTableViewCell = ComposeReplyToTableViewCell()
|
2022-10-11 12:31:40 +02:00
|
|
|
let composeContentTableViewCell = ComposeContentTableViewCell()
|
2022-09-30 13:28:09 +02:00
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-10 13:14:52 +02:00
|
|
|
let kind: Kind
|
2022-10-11 12:31:40 +02:00
|
|
|
|
|
|
|
@Published var viewLayoutFrame = ViewLayoutFrame()
|
2022-10-31 13:41:19 +01:00
|
|
|
|
|
|
|
// author (me)
|
2022-10-11 12:31:40 +02:00
|
|
|
@Published var authContext: AuthContext
|
|
|
|
|
|
|
|
// output
|
|
|
|
|
2022-10-28 13:06:18 +02:00
|
|
|
// limit
|
|
|
|
@Published public var maxTextInputLimit = 500
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
// content
|
2022-10-28 13:06:18 +02:00
|
|
|
public weak var contentMetaText: MetaText? {
|
|
|
|
didSet {
|
|
|
|
// guard let textView = contentMetaText?.textView else { return }
|
|
|
|
// customEmojiPickerInputViewModel.configure(textInput: textView)
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 12:31:40 +02:00
|
|
|
@Published public var initialContent = ""
|
|
|
|
@Published public var content = ""
|
|
|
|
@Published public var contentWeightedLength = 0
|
|
|
|
@Published public var isContentEmpty = true
|
|
|
|
@Published public var isContentValid = true
|
|
|
|
@Published public var isContentEditing = false
|
|
|
|
|
2022-10-28 13:06:18 +02:00
|
|
|
// content warning
|
|
|
|
weak var contentWarningMetaText: MetaText? {
|
|
|
|
didSet {
|
|
|
|
//guard let textView = contentWarningMetaText?.textView else { return }
|
|
|
|
//customEmojiPickerInputViewModel.configure(textInput: textView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Published public var isContentWarningActive = false
|
|
|
|
@Published public var contentWarning = ""
|
|
|
|
@Published public var contentWarningWeightedLength = 0 // set 0 when not composing
|
|
|
|
@Published public var isContentWarningEditing = false
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
// author
|
|
|
|
@Published var avatarURL: URL?
|
|
|
|
@Published var name: MetaContent = PlaintextMetaContent(string: "")
|
|
|
|
@Published var username: String = ""
|
|
|
|
|
2022-10-31 13:41:19 +01:00
|
|
|
// attachment
|
|
|
|
@Published public var attachmentViewModels: [AttachmentViewModel] = []
|
|
|
|
@Published public var maxMediaAttachmentLimit = 4
|
|
|
|
// @Published public internal(set) var isMediaValid = true
|
|
|
|
|
2022-10-28 13:06:18 +02:00
|
|
|
// poll
|
|
|
|
@Published var isPollActive = false
|
|
|
|
@Published public var pollOptions: [PollComposeItem.Option] = {
|
|
|
|
// initial with 2 options
|
|
|
|
var options: [PollComposeItem.Option] = []
|
|
|
|
options.append(PollComposeItem.Option())
|
|
|
|
options.append(PollComposeItem.Option())
|
|
|
|
return options
|
|
|
|
}()
|
|
|
|
@Published public var pollExpireConfigurationOption: PollComposeItem.ExpireConfiguration.Option = .oneDay
|
2022-10-31 13:41:19 +01:00
|
|
|
@Published public var pollMultipleConfigurationOption: PollComposeItem.MultipleConfiguration.Option = false
|
|
|
|
|
2022-10-28 13:06:18 +02:00
|
|
|
@Published public var maxPollOptionLimit = 4
|
|
|
|
|
|
|
|
// emoji
|
|
|
|
@Published var isEmojiActive = false
|
|
|
|
|
2022-10-31 13:41:19 +01:00
|
|
|
// visibility
|
|
|
|
@Published var visibility: Mastodon.Entity.Status.Visibility
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
// UI & UX
|
|
|
|
@Published var replyToCellFrame: CGRect = .zero
|
|
|
|
@Published var contentCellFrame: CGRect = .zero
|
|
|
|
@Published var scrollViewState: ScrollViewState = .fold
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
public init(
|
|
|
|
context: AppContext,
|
2022-10-11 12:31:40 +02:00
|
|
|
authContext: AuthContext,
|
2022-10-10 13:14:52 +02:00
|
|
|
kind: Kind
|
|
|
|
) {
|
2022-09-30 13:28:09 +02:00
|
|
|
self.context = context
|
2022-10-11 12:31:40 +02:00
|
|
|
self.authContext = authContext
|
2022-10-10 13:14:52 +02:00
|
|
|
self.kind = kind
|
2022-10-31 13:41:19 +01:00
|
|
|
self.visibility = {
|
|
|
|
// default private when user locked
|
|
|
|
var visibility: Mastodon.Entity.Status.Visibility = {
|
|
|
|
guard let author = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.user else {
|
|
|
|
return .public
|
|
|
|
}
|
|
|
|
return author.locked ? .private : .public
|
|
|
|
}()
|
|
|
|
// set visibility for reply post
|
|
|
|
switch kind {
|
|
|
|
case .reply(let record):
|
|
|
|
context.managedObjectContext.performAndWait {
|
|
|
|
guard let status = record.object(in: context.managedObjectContext) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let repliedStatusVisibility = status.visibility
|
|
|
|
switch repliedStatusVisibility {
|
|
|
|
case .public, .unlisted:
|
|
|
|
// keep default
|
|
|
|
break
|
|
|
|
case .private:
|
|
|
|
visibility = .private
|
|
|
|
case .direct:
|
|
|
|
visibility = .direct
|
|
|
|
case ._other:
|
|
|
|
assertionFailure()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return visibility
|
|
|
|
}()
|
2022-10-10 13:14:52 +02:00
|
|
|
super.init()
|
|
|
|
// end init
|
2022-10-11 12:31:40 +02:00
|
|
|
|
|
|
|
// bind author
|
|
|
|
$authContext
|
|
|
|
.sink { [weak self] authContext in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let user = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user else { return }
|
|
|
|
self.avatarURL = user.avatarImageURL()
|
|
|
|
self.name = user.nameMetaContent ?? PlaintextMetaContent(string: user.displayNameWithFallback)
|
|
|
|
self.username = user.acctWithDomain
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-10-28 13:06:18 +02:00
|
|
|
|
|
|
|
// bind text
|
|
|
|
$content
|
|
|
|
.map { $0.count }
|
|
|
|
.assign(to: &$contentWeightedLength)
|
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
$contentWarning,
|
|
|
|
$isContentWarningActive
|
|
|
|
)
|
|
|
|
.map { $1 ? $0.count : 0 }
|
|
|
|
.assign(to: &$contentWarningWeightedLength)
|
|
|
|
|
|
|
|
Publishers.CombineLatest3(
|
|
|
|
$contentWeightedLength,
|
|
|
|
$contentWarningWeightedLength,
|
|
|
|
$maxTextInputLimit
|
|
|
|
)
|
|
|
|
.map { $0 + $1 <= $2 }
|
|
|
|
.assign(to: &$isContentValid)
|
2022-09-30 13:28:09 +02:00
|
|
|
}
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
}
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
extension ComposeContentViewModel {
|
|
|
|
public enum Kind {
|
|
|
|
case post
|
|
|
|
case hashtag(hashtag: String)
|
|
|
|
case mention(user: ManagedObjectRecord<MastodonUser>)
|
|
|
|
case reply(status: ManagedObjectRecord<Status>)
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
public enum ScrollViewState {
|
2022-10-10 13:14:52 +02:00
|
|
|
case fold // snap to input
|
|
|
|
case expand // snap to reply
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:12:44 +02:00
|
|
|
extension ComposeContentViewModel {
|
|
|
|
func createNewPollOptionIfCould() {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
|
|
|
|
guard pollOptions.count < maxPollOptionLimit else { return }
|
|
|
|
let option = PollComposeItem.Option()
|
|
|
|
option.shouldBecomeFirstResponder = true
|
|
|
|
pollOptions.append(option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 13:41:19 +01:00
|
|
|
extension ComposeContentViewModel {
|
|
|
|
public enum ComposeError: LocalizedError {
|
|
|
|
case pollHasEmptyOption
|
|
|
|
|
|
|
|
public var errorDescription: String? {
|
|
|
|
switch self {
|
|
|
|
case .pollHasEmptyOption:
|
|
|
|
return "The post poll is invalid" // TODO: i18n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var failureReason: String? {
|
|
|
|
switch self {
|
|
|
|
case .pollHasEmptyOption:
|
|
|
|
return "The poll has empty option" // TODO: i18n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func statusPublisher() throws -> StatusPublisher {
|
|
|
|
let authContext = self.authContext
|
|
|
|
|
|
|
|
// author
|
|
|
|
let managedObjectContext = self.context.managedObjectContext
|
|
|
|
var _author: ManagedObjectRecord<MastodonUser>?
|
|
|
|
managedObjectContext.performAndWait {
|
|
|
|
_author = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: managedObjectContext)?.user.asRecrod
|
|
|
|
}
|
|
|
|
guard let author = _author else {
|
|
|
|
throw AppError.badAuthentication
|
|
|
|
}
|
|
|
|
|
|
|
|
// poll
|
|
|
|
_ = try {
|
|
|
|
guard isPollActive else { return }
|
|
|
|
let isAllNonEmpty = pollOptions
|
|
|
|
.map { $0.text }
|
|
|
|
.allSatisfy { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
|
|
|
|
guard isAllNonEmpty else {
|
|
|
|
throw ComposeError.pollHasEmptyOption
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return MastodonStatusPublisher(
|
|
|
|
author: author,
|
|
|
|
replyTo: {
|
|
|
|
switch self.kind {
|
|
|
|
case .reply(let status): return status
|
|
|
|
default: return nil
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
isContentWarningComposing: isContentWarningActive,
|
|
|
|
contentWarning: contentWarning,
|
|
|
|
content: content,
|
|
|
|
isMediaSensitive: isContentWarningActive,
|
|
|
|
attachmentViewModels: attachmentViewModels,
|
|
|
|
isPollComposing: isPollActive,
|
|
|
|
pollOptions: pollOptions,
|
|
|
|
pollExpireConfigurationOption: pollExpireConfigurationOption,
|
|
|
|
pollMultipleConfigurationOption: pollMultipleConfigurationOption,
|
|
|
|
visibility: visibility
|
|
|
|
)
|
|
|
|
} // end func publisher()
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:06:18 +02:00
|
|
|
// MARK: - UITextViewDelegate
|
|
|
|
extension ComposeContentViewModel: UITextViewDelegate {
|
|
|
|
public func textViewDidBeginEditing(_ textView: UITextView) {
|
|
|
|
switch textView {
|
|
|
|
case contentMetaText?.textView:
|
|
|
|
isContentEditing = true
|
|
|
|
case contentWarningMetaText?.textView:
|
|
|
|
isContentWarningEditing = true
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func textViewDidEndEditing(_ textView: UITextView) {
|
|
|
|
switch textView {
|
|
|
|
case contentMetaText?.textView:
|
|
|
|
isContentEditing = false
|
|
|
|
case contentWarningMetaText?.textView:
|
|
|
|
isContentWarningEditing = false
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
|
switch textView {
|
|
|
|
case contentMetaText?.textView:
|
|
|
|
return true
|
|
|
|
case contentWarningMetaText?.textView:
|
|
|
|
let isReturn = text == "\n"
|
|
|
|
if isReturn {
|
|
|
|
setContentTextViewFirstResponderIfNeeds()
|
|
|
|
}
|
|
|
|
return !isReturn
|
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func insertContentText(text: String) {
|
|
|
|
guard let contentMetaText = self.contentMetaText else { return }
|
|
|
|
// FIXME: smart prefix and suffix
|
|
|
|
let string = contentMetaText.textStorage.string
|
|
|
|
let isEmpty = string.isEmpty
|
|
|
|
let hasPrefix = string.hasPrefix(" ")
|
|
|
|
if hasPrefix || isEmpty {
|
|
|
|
contentMetaText.textView.insertText(text)
|
|
|
|
} else {
|
|
|
|
contentMetaText.textView.insertText(" " + text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setContentTextViewFirstResponderIfNeeds() {
|
|
|
|
guard let contentMetaText = self.contentMetaText else { return }
|
|
|
|
guard !contentMetaText.textView.isFirstResponder else { return }
|
|
|
|
contentMetaText.textView.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setContentWarningTextViewFirstResponderIfNeeds() {
|
|
|
|
guard let contentWarningMetaText = self.contentWarningMetaText else { return }
|
|
|
|
guard !contentWarningMetaText.textView.isFirstResponder else { return }
|
|
|
|
contentWarningMetaText.textView.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:12:44 +02:00
|
|
|
// MARK: - DeleteBackwardResponseTextFieldRelayDelegate
|
|
|
|
extension ComposeContentViewModel: DeleteBackwardResponseTextFieldRelayDelegate {
|
|
|
|
|
|
|
|
func deleteBackwardResponseTextFieldDidReturn(_ textField: DeleteBackwardResponseTextField) {
|
|
|
|
let index = textField.tag
|
|
|
|
if index + 1 == pollOptions.count {
|
|
|
|
createNewPollOptionIfCould()
|
|
|
|
} else if index < pollOptions.count {
|
|
|
|
pollOptions[index + 1].textField?.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteBackwardResponseTextField(_ textField: DeleteBackwardResponseTextField, textBeforeDelete: String?) {
|
|
|
|
guard (textBeforeDelete ?? "").isEmpty else {
|
|
|
|
// do nothing when not empty
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = textField.tag
|
|
|
|
guard index > 0 else {
|
|
|
|
// do nothing at first row
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func optionBeforeRemoved() -> PollComposeItem.Option? {
|
|
|
|
guard index > 0 else { return nil }
|
|
|
|
let indexBeforeRemoved = pollOptions.index(before: index)
|
|
|
|
let itemBeforeRemoved = pollOptions[indexBeforeRemoved]
|
|
|
|
return itemBeforeRemoved
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func optionAfterRemoved() -> PollComposeItem.Option? {
|
|
|
|
guard index < pollOptions.count - 1 else { return nil }
|
|
|
|
let indexAfterRemoved = pollOptions.index(after: index)
|
|
|
|
let itemAfterRemoved = pollOptions[indexAfterRemoved]
|
|
|
|
return itemAfterRemoved
|
|
|
|
}
|
|
|
|
|
|
|
|
// move first responder
|
|
|
|
let _option = optionBeforeRemoved() ?? optionAfterRemoved()
|
|
|
|
_option?.textField?.becomeFirstResponder()
|
|
|
|
|
|
|
|
guard pollOptions.count > 2 else {
|
|
|
|
// remove item when more then 2 options
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pollOptions.remove(at: index)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|