2022-09-30 13:28:09 +02:00
|
|
|
//
|
|
|
|
// ComposeContentViewModel.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 22/9/30.
|
|
|
|
//
|
|
|
|
|
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-11-13 15:08:26 +01:00
|
|
|
import MastodonLocalization
|
2022-09-30 13:28:09 +02:00
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
public protocol ComposeContentViewModelDelegate: AnyObject {
|
|
|
|
func composeContentViewModel(_ viewModel: ComposeContentViewModel, handleAutoComplete info: ComposeContentViewModel.AutoCompleteInfo) -> Bool
|
|
|
|
}
|
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public final class ComposeContentViewModel: NSObject, ObservableObject {
|
2023-03-02 11:06:13 +01:00
|
|
|
|
|
|
|
public enum ComposeContext {
|
|
|
|
case composeStatus
|
|
|
|
case editStatus(status: Status, statusSource: Mastodon.Entity.StatusSource)
|
|
|
|
}
|
2022-10-10 13:14:52 +02:00
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
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
|
2023-03-02 11:06:13 +01:00
|
|
|
let composeContext: ComposeContext
|
2022-12-03 19:25:07 +01:00
|
|
|
let destination: Destination
|
2022-11-13 12:42:50 +01:00
|
|
|
weak var delegate: ComposeContentViewModelDelegate?
|
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
|
|
|
|
|
2022-11-13 09:04:29 +01:00
|
|
|
// auto-complete info
|
|
|
|
@Published var autoCompleteRetryLayoutTimes = 0
|
|
|
|
@Published var autoCompleteInfo: AutoCompleteInfo? = nil
|
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
// emoji
|
|
|
|
var customEmojiPickerDiffableDataSource: UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem>?
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
// 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 {
|
2022-11-13 12:42:50 +01:00
|
|
|
guard let textView = contentMetaText?.textView else { return }
|
|
|
|
customEmojiPickerInputViewModel.configure(textInput: textView)
|
2022-10-28 13:06:18 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 19:25:07 +01:00
|
|
|
// allow dismissing the compose view without confirmation if content == intialContent
|
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 {
|
2022-11-13 12:42:50 +01:00
|
|
|
guard let textView = contentWarningMetaText?.textView else { return }
|
|
|
|
customEmojiPickerInputViewModel.configure(textInput: textView)
|
2022-10-28 13:06:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
@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
|
2022-11-22 08:58:31 +01:00
|
|
|
@Published public internal(set) var maxImageMediaSizeLimitInByte = 10 * 1024 * 1024 // 10 MiB
|
2022-10-31 13:41:19 +01:00
|
|
|
|
2022-10-28 13:06:18 +02:00
|
|
|
// poll
|
2022-11-14 12:43:32 +01:00
|
|
|
@Published public var isPollActive = false
|
2022-10-28 13:06:18 +02:00
|
|
|
@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-11-13 12:42:50 +01:00
|
|
|
let customEmojiViewModel: EmojiService.CustomEmojiViewModel?
|
|
|
|
let customEmojiPickerInputViewModel = CustomEmojiPickerInputViewModel()
|
|
|
|
@Published var isLoadingCustomEmoji = false
|
2022-10-28 13:06:18 +02:00
|
|
|
|
2022-10-31 13:41:19 +01:00
|
|
|
// visibility
|
2022-11-14 12:43:32 +01:00
|
|
|
@Published public var visibility: Mastodon.Entity.Status.Visibility
|
2023-03-02 11:06:13 +01:00
|
|
|
@Published public var isVisibilityButtonEnabled = false
|
|
|
|
|
2023-01-24 01:50:10 +01:00
|
|
|
// language
|
|
|
|
@Published public var language: String
|
|
|
|
@Published public private(set) var recentLanguages: [String]
|
|
|
|
|
2022-10-11 12:31:40 +02:00
|
|
|
// UI & UX
|
|
|
|
@Published var replyToCellFrame: CGRect = .zero
|
|
|
|
@Published var contentCellFrame: CGRect = .zero
|
2022-11-13 09:04:29 +01:00
|
|
|
@Published var contentTextViewFrame: CGRect = .zero
|
2022-10-11 12:31:40 +02:00
|
|
|
@Published var scrollViewState: ScrollViewState = .fold
|
2022-11-13 15:08:26 +01:00
|
|
|
|
|
|
|
@Published var characterCount: Int = 0
|
|
|
|
|
|
|
|
@Published public private(set) var isPublishBarButtonItemEnabled = true
|
|
|
|
@Published var isAttachmentButtonEnabled = false
|
|
|
|
@Published var isPollButtonEnabled = false
|
|
|
|
|
|
|
|
@Published public private(set) var shouldDismiss = true
|
2022-11-22 08:58:31 +01:00
|
|
|
|
|
|
|
// size limit
|
|
|
|
public var sizeLimit: AttachmentViewModel.SizeLimit {
|
|
|
|
AttachmentViewModel.SizeLimit(
|
|
|
|
image: maxImageMediaSizeLimitInByte,
|
|
|
|
video: nil
|
|
|
|
)
|
|
|
|
}
|
2022-10-11 12:31:40 +02:00
|
|
|
|
2022-10-10 13:14:52 +02:00
|
|
|
public init(
|
|
|
|
context: AppContext,
|
2022-10-11 12:31:40 +02:00
|
|
|
authContext: AuthContext,
|
2023-03-02 11:06:13 +01:00
|
|
|
composeContext: ComposeContext,
|
2022-12-03 19:25:07 +01:00
|
|
|
destination: Destination,
|
|
|
|
initialContent: String
|
2022-10-10 13:14:52 +02:00
|
|
|
) {
|
2022-09-30 13:28:09 +02:00
|
|
|
self.context = context
|
2022-10-11 12:31:40 +02:00
|
|
|
self.authContext = authContext
|
2022-12-03 19:25:07 +01:00
|
|
|
self.destination = destination
|
2023-03-02 11:06:13 +01:00
|
|
|
self.composeContext = composeContext
|
2022-10-31 13:41:19 +01:00
|
|
|
self.visibility = {
|
|
|
|
// default private when user locked
|
|
|
|
var visibility: Mastodon.Entity.Status.Visibility = {
|
2023-05-25 16:26:17 +02:00
|
|
|
guard let author = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) else {
|
2022-10-31 13:41:19 +01:00
|
|
|
return .public
|
|
|
|
}
|
|
|
|
return author.locked ? .private : .public
|
|
|
|
}()
|
|
|
|
// set visibility for reply post
|
2022-12-03 19:25:07 +01:00
|
|
|
if case .reply(let record) = destination {
|
2022-10-31 13:41:19 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return visibility
|
|
|
|
}()
|
2023-03-02 11:06:13 +01:00
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
self.customEmojiViewModel = context.emojiService.dequeueCustomEmojiViewModel(
|
|
|
|
for: authContext.mastodonAuthenticationBox.domain
|
|
|
|
)
|
2023-01-24 01:50:10 +01:00
|
|
|
|
2023-03-02 11:06:13 +01:00
|
|
|
if case let ComposeContext.editStatus(status, _) = composeContext {
|
|
|
|
if status.isContentSensitive {
|
|
|
|
isContentWarningActive = true
|
|
|
|
contentWarning = status.spoilerText ?? ""
|
|
|
|
}
|
|
|
|
if let poll = status.poll {
|
|
|
|
isPollActive = !poll.expired
|
|
|
|
pollMultipleConfigurationOption = poll.multiple
|
|
|
|
if let pollExpiresAt = poll.expiresAt {
|
|
|
|
pollExpireConfigurationOption = .init(closestDateToExpiry: pollExpiresAt)
|
|
|
|
}
|
|
|
|
pollOptions = poll.options.sortedByIndex().map {
|
|
|
|
let option = PollComposeItem.Option()
|
|
|
|
option.text = $0.title
|
|
|
|
return option
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 01:50:10 +01:00
|
|
|
let recentLanguages = context.settingService.currentSetting.value?.recentLanguages ?? []
|
|
|
|
self.recentLanguages = recentLanguages
|
|
|
|
self.language = recentLanguages.first ?? Locale.current.languageCode ?? "en"
|
2022-10-10 13:14:52 +02:00
|
|
|
super.init()
|
|
|
|
// end init
|
2022-10-11 12:31:40 +02:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
// setup initial value
|
2022-12-03 19:25:07 +01:00
|
|
|
let initialContentWithSpace = initialContent.isEmpty ? "" : initialContent + " "
|
|
|
|
switch destination {
|
2022-11-13 15:08:26 +01:00
|
|
|
case .reply(let record):
|
|
|
|
context.managedObjectContext.performAndWait {
|
|
|
|
guard let status = record.object(in: context.managedObjectContext) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
2023-05-25 16:26:17 +02:00
|
|
|
let author = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext)
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
var mentionAccts: [String] = []
|
|
|
|
if author?.id != status.author.id {
|
|
|
|
mentionAccts.append("@" + status.author.acct)
|
|
|
|
}
|
|
|
|
let mentions = status.mentions
|
|
|
|
.filter { author?.id != $0.id }
|
|
|
|
for mention in mentions {
|
|
|
|
let acct = "@" + mention.acct
|
|
|
|
guard !mentionAccts.contains(acct) else { continue }
|
|
|
|
mentionAccts.append(acct)
|
|
|
|
}
|
|
|
|
for acct in mentionAccts {
|
|
|
|
UITextChecker.learnWord(acct)
|
|
|
|
}
|
|
|
|
if let spoilerText = status.spoilerText, !spoilerText.isEmpty {
|
|
|
|
self.isContentWarningActive = true
|
|
|
|
self.contentWarning = spoilerText
|
|
|
|
}
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
let initialComposeContent = mentionAccts.joined(separator: " ")
|
2022-12-03 19:25:07 +01:00
|
|
|
let preInsertedContent = initialComposeContent.isEmpty ? "" : initialComposeContent + " "
|
|
|
|
self.initialContent = preInsertedContent + initialContentWithSpace
|
|
|
|
self.content = preInsertedContent + initialContentWithSpace
|
2022-11-13 15:08:26 +01:00
|
|
|
}
|
2022-12-03 19:25:07 +01:00
|
|
|
case .topLevel:
|
|
|
|
self.initialContent = initialContentWithSpace
|
|
|
|
self.content = initialContentWithSpace
|
2022-11-13 15:08:26 +01:00
|
|
|
}
|
2022-12-03 19:25:07 +01:00
|
|
|
|
2022-11-14 12:14:46 +01:00
|
|
|
// set limit
|
|
|
|
let _configuration: Mastodon.Entity.Instance.Configuration? = {
|
|
|
|
var configuration: Mastodon.Entity.Instance.Configuration? = nil
|
|
|
|
context.managedObjectContext.performAndWait {
|
2023-05-25 16:26:17 +02:00
|
|
|
let authentication = authContext.mastodonAuthenticationBox.authentication
|
|
|
|
configuration = authentication.instance(in: context.managedObjectContext)?.configuration
|
2022-11-14 12:14:46 +01:00
|
|
|
}
|
|
|
|
return configuration
|
|
|
|
}()
|
|
|
|
if let configuration = _configuration {
|
|
|
|
// set character limit
|
|
|
|
if let maxCharacters = configuration.statuses?.maxCharacters {
|
|
|
|
maxTextInputLimit = maxCharacters
|
|
|
|
}
|
|
|
|
// set media limit
|
|
|
|
if let maxMediaAttachments = configuration.statuses?.maxMediaAttachments {
|
|
|
|
maxMediaAttachmentLimit = maxMediaAttachments
|
|
|
|
}
|
|
|
|
// set poll option limit
|
|
|
|
if let maxOptions = configuration.polls?.maxOptions {
|
|
|
|
maxPollOptionLimit = maxOptions
|
|
|
|
}
|
2022-11-22 08:58:31 +01:00
|
|
|
// set photo attachment limit
|
|
|
|
if let imageSizeLimit = configuration.mediaAttachments?.imageSizeLimit {
|
|
|
|
maxImageMediaSizeLimitInByte = imageSizeLimit
|
|
|
|
}
|
2022-11-14 12:14:46 +01:00
|
|
|
// TODO: more limit
|
|
|
|
}
|
|
|
|
|
2023-03-02 11:06:13 +01:00
|
|
|
switch composeContext {
|
|
|
|
case .composeStatus:
|
|
|
|
self.isVisibilityButtonEnabled = true
|
|
|
|
case let .editStatus(status, _):
|
|
|
|
if let visibility = Mastodon.Entity.Status.Visibility(rawValue: status.visibility.rawValue) {
|
|
|
|
self.visibility = visibility
|
|
|
|
}
|
|
|
|
self.isVisibilityButtonEnabled = false
|
|
|
|
self.attachmentViewModels = status.attachments.compactMap {
|
|
|
|
guard let assetURL = $0.assetURL, let url = URL(string: assetURL) else { return nil }
|
|
|
|
let attachmentViewModel = AttachmentViewModel(
|
|
|
|
api: context.apiService,
|
|
|
|
authContext: authContext,
|
|
|
|
input: .mastodonAssetUrl(url, $0.id),
|
|
|
|
sizeLimit: sizeLimit,
|
|
|
|
delegate: self
|
|
|
|
)
|
|
|
|
attachmentViewModel.caption = $0.altDescription ?? ""
|
|
|
|
return attachmentViewModel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
bind()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeContentViewModel {
|
|
|
|
private func bind() {
|
2022-10-11 12:31:40 +02:00
|
|
|
// bind author
|
|
|
|
$authContext
|
|
|
|
.sink { [weak self] authContext in
|
|
|
|
guard let self = self else { return }
|
2023-05-25 16:26:17 +02:00
|
|
|
guard let user = authContext.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) else { return }
|
2022-10-11 12:31:40 +02:00
|
|
|
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-11-11 11:10:13 +01:00
|
|
|
|
|
|
|
// bind attachment
|
|
|
|
$attachmentViewModels
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
Task {
|
|
|
|
try await self.uploadMediaInQueue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-11-13 12:42:50 +01:00
|
|
|
|
|
|
|
// bind emoji inputView
|
|
|
|
$isEmojiActive.assign(to: &customEmojiPickerInputViewModel.$isCustomEmojiComposing)
|
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
// bind toolbar
|
|
|
|
Publishers.CombineLatest3(
|
|
|
|
$isPollActive,
|
|
|
|
$attachmentViewModels,
|
|
|
|
$maxMediaAttachmentLimit
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] isPollActive, attachmentViewModels, maxMediaAttachmentLimit in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let shouldMediaDisable = isPollActive || attachmentViewModels.count >= maxMediaAttachmentLimit
|
|
|
|
let shouldPollDisable = attachmentViewModels.count > 0
|
|
|
|
|
|
|
|
self.isAttachmentButtonEnabled = !shouldMediaDisable
|
|
|
|
self.isPollButtonEnabled = !shouldPollDisable
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
// bind status content character count
|
|
|
|
Publishers.CombineLatest3(
|
|
|
|
$contentWeightedLength,
|
|
|
|
$contentWarningWeightedLength,
|
|
|
|
$isContentWarningActive
|
|
|
|
)
|
|
|
|
.map { contentWeightedLength, contentWarningWeightedLength, isContentWarningActive -> Int in
|
|
|
|
var count = contentWeightedLength
|
|
|
|
if isContentWarningActive {
|
|
|
|
count += contentWarningWeightedLength
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
.assign(to: &$characterCount)
|
|
|
|
|
|
|
|
// bind compose bar button item UI state
|
|
|
|
let isComposeContentEmpty = $content
|
|
|
|
.map { $0.isEmpty }
|
|
|
|
let isComposeContentValid = Publishers.CombineLatest(
|
|
|
|
$characterCount,
|
|
|
|
$maxTextInputLimit
|
|
|
|
)
|
|
|
|
.map { characterCount, maxTextInputLimit in
|
|
|
|
characterCount <= maxTextInputLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
let isMediaEmpty = $attachmentViewModels
|
|
|
|
.map { $0.isEmpty }
|
|
|
|
let isMediaUploadAllSuccess = $attachmentViewModels
|
|
|
|
.map { attachmentViewModels in
|
|
|
|
return Publishers.MergeMany(attachmentViewModels.map { $0.$uploadState })
|
2022-11-17 13:41:55 +01:00
|
|
|
.delay(for: 0.3, scheduler: DispatchQueue.main) // convert to outputs with delay. Due to @Published emit before changes
|
2022-11-13 15:08:26 +01:00
|
|
|
.map { _ in attachmentViewModels.map { $0.uploadState } }
|
|
|
|
}
|
|
|
|
.switchToLatest()
|
|
|
|
.map { outputs in
|
|
|
|
guard outputs.allSatisfy({ $0 == .finish }) else { return false }
|
|
|
|
return true
|
|
|
|
}
|
2022-11-17 13:41:55 +01:00
|
|
|
.prepend(true)
|
2022-11-13 15:08:26 +01:00
|
|
|
|
|
|
|
let isPollOptionsAllValid = $pollOptions
|
|
|
|
.map { options in
|
|
|
|
return Publishers.MergeMany(options.map { $0.$text })
|
2022-11-17 13:41:55 +01:00
|
|
|
.delay(for: 0.3, scheduler: DispatchQueue.main) // convert to outputs with delay. Due to @Published emit before changes
|
2022-11-13 15:08:26 +01:00
|
|
|
.map { _ in options.map { $0.text } }
|
|
|
|
}
|
|
|
|
.switchToLatest()
|
|
|
|
.map { outputs in
|
|
|
|
return outputs.allSatisfy { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
|
|
|
|
}
|
2022-11-17 13:41:55 +01:00
|
|
|
.prepend(true)
|
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
let isPublishBarButtonItemEnabledPrecondition1 = Publishers.CombineLatest4(
|
|
|
|
isComposeContentEmpty,
|
|
|
|
isComposeContentValid,
|
|
|
|
isMediaEmpty,
|
|
|
|
isMediaUploadAllSuccess
|
|
|
|
)
|
|
|
|
.map { isComposeContentEmpty, isComposeContentValid, isMediaEmpty, isMediaUploadAllSuccess -> Bool in
|
|
|
|
if isMediaEmpty {
|
|
|
|
return isComposeContentValid && !isComposeContentEmpty
|
|
|
|
} else {
|
|
|
|
return isComposeContentValid && isMediaUploadAllSuccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2022-11-17 13:41:55 +01:00
|
|
|
let isPublishBarButtonItemEnabledPrecondition2 = Publishers.CombineLatest(
|
2022-11-13 15:08:26 +01:00
|
|
|
$isPollActive,
|
|
|
|
isPollOptionsAllValid
|
|
|
|
)
|
2022-11-17 13:41:55 +01:00
|
|
|
.map { isPollActive, isPollOptionsAllValid -> Bool in
|
|
|
|
if isPollActive {
|
|
|
|
return isPollOptionsAllValid
|
2022-11-13 15:08:26 +01:00
|
|
|
} else {
|
2022-11-17 13:41:55 +01:00
|
|
|
return true
|
2022-11-13 15:08:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2022-10-11 12:31:40 +02:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
isPublishBarButtonItemEnabledPrecondition1,
|
|
|
|
isPublishBarButtonItemEnabledPrecondition2
|
|
|
|
)
|
|
|
|
.map { $0 && $1 }
|
|
|
|
.assign(to: &$isPublishBarButtonItemEnabled)
|
|
|
|
|
|
|
|
// bind modal dismiss state
|
2023-06-30 23:44:44 +02:00
|
|
|
Publishers.CombineLatest4(
|
|
|
|
$contentWarning,
|
|
|
|
$content,
|
|
|
|
$isPollActive,
|
|
|
|
$attachmentViewModels
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.map { contentWarning, content, hasPoll, attachments in
|
|
|
|
let trimmedContent = content.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
let initialContent = self.initialContent.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
let canDiscardContent = trimmedContent.isEmpty || trimmedContent == initialContent
|
|
|
|
|
|
|
|
let canDiscardPoll = !hasPoll
|
|
|
|
|
|
|
|
let canDiscardAttachments = attachments.isEmpty
|
|
|
|
|
|
|
|
return canDiscardContent && canDiscardPoll && canDiscardAttachments
|
|
|
|
}
|
|
|
|
.assign(to: &$shouldDismiss)
|
2023-01-24 01:50:10 +01:00
|
|
|
|
|
|
|
// languages
|
|
|
|
context.settingService.currentSetting
|
|
|
|
.flatMap { settings in
|
|
|
|
if let settings {
|
|
|
|
return settings.publisher(for: \.recentLanguages, options: .initial).eraseToAnyPublisher()
|
|
|
|
} else if let code = Locale.current.languageCode {
|
|
|
|
return Just([code]).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
return Just([]).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.assign(to: &$recentLanguages)
|
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
}
|
2022-09-30 13:28:09 +02:00
|
|
|
}
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
extension ComposeContentViewModel {
|
2022-12-03 19:25:07 +01:00
|
|
|
public enum Destination {
|
|
|
|
case topLevel
|
|
|
|
case reply(parent: ManagedObjectRecord<Status>)
|
2022-10-10 13:14:52 +02:00
|
|
|
}
|
2022-11-13 09:04:29 +01:00
|
|
|
|
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-11-13 09:04:29 +01:00
|
|
|
extension ComposeContentViewModel {
|
2022-11-13 12:42:50 +01:00
|
|
|
public struct AutoCompleteInfo {
|
2022-11-13 09:04:29 +01:00
|
|
|
// model
|
|
|
|
let inputText: Substring
|
|
|
|
// range
|
|
|
|
let symbolRange: Range<String.Index>
|
|
|
|
let symbolString: Substring
|
|
|
|
let toCursorRange: Range<String.Index>
|
|
|
|
let toCursorString: Substring
|
|
|
|
let toHighlightEndRange: Range<String.Index>
|
|
|
|
let toHighlightEndString: Substring
|
|
|
|
// geometry
|
|
|
|
var textBoundingRect: CGRect = .zero
|
|
|
|
var symbolBoundingRect: CGRect = .zero
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:12:44 +02:00
|
|
|
extension ComposeContentViewModel {
|
|
|
|
func createNewPollOptionIfCould() {
|
|
|
|
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:
|
2022-11-14 18:44:28 +01:00
|
|
|
return L10n.Scene.Compose.Poll.thePollIsInvalid
|
2022-10-31 13:41:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var failureReason: String? {
|
|
|
|
switch self {
|
|
|
|
case .pollHasEmptyOption:
|
2022-11-14 18:44:28 +01:00
|
|
|
return L10n.Scene.Compose.Poll.thePollHasEmptyOption
|
2022-10-31 13:41:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func statusPublisher() throws -> StatusPublisher {
|
|
|
|
let authContext = self.authContext
|
|
|
|
|
|
|
|
// author
|
|
|
|
let managedObjectContext = self.context.managedObjectContext
|
|
|
|
var _author: ManagedObjectRecord<MastodonUser>?
|
|
|
|
managedObjectContext.performAndWait {
|
2023-05-25 16:26:17 +02:00
|
|
|
_author = authContext.mastodonAuthenticationBox.authentication.user(in: managedObjectContext)?.asRecord
|
2022-10-31 13:41:19 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-24 01:50:10 +01:00
|
|
|
// save language to recent languages
|
|
|
|
if let settings = context.settingService.currentSetting.value {
|
2023-02-23 21:56:18 +01:00
|
|
|
settings.managedObjectContext?.performAndWait {
|
|
|
|
settings.recentLanguages = [language] + settings.recentLanguages.filter { $0 != language }
|
2023-01-24 01:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-23 21:56:18 +01:00
|
|
|
|
2022-10-31 13:41:19 +01:00
|
|
|
return MastodonStatusPublisher(
|
|
|
|
author: author,
|
|
|
|
replyTo: {
|
2022-12-03 19:25:07 +01:00
|
|
|
if case .reply(let status) = destination {
|
|
|
|
return status
|
2022-10-31 13:41:19 +01:00
|
|
|
}
|
2022-12-03 19:25:07 +01:00
|
|
|
return nil
|
2022-10-31 13:41:19 +01:00
|
|
|
}(),
|
|
|
|
isContentWarningComposing: isContentWarningActive,
|
|
|
|
contentWarning: contentWarning,
|
|
|
|
content: content,
|
|
|
|
isMediaSensitive: isContentWarningActive,
|
|
|
|
attachmentViewModels: attachmentViewModels,
|
|
|
|
isPollComposing: isPollActive,
|
|
|
|
pollOptions: pollOptions,
|
|
|
|
pollExpireConfigurationOption: pollExpireConfigurationOption,
|
|
|
|
pollMultipleConfigurationOption: pollMultipleConfigurationOption,
|
2023-01-24 01:50:10 +01:00
|
|
|
visibility: visibility,
|
|
|
|
language: language
|
2022-10-31 13:41:19 +01:00
|
|
|
)
|
2023-03-02 11:06:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MastodonEditStatusPublisher
|
|
|
|
public func statusEditPublisher() throws -> StatusPublisher? {
|
|
|
|
let authContext = self.authContext
|
|
|
|
guard case let .editStatus(status, _) = composeContext else { return nil }
|
|
|
|
|
|
|
|
// author
|
|
|
|
let managedObjectContext = self.context.managedObjectContext
|
|
|
|
var _author: ManagedObjectRecord<MastodonUser>?
|
|
|
|
managedObjectContext.performAndWait {
|
2023-05-25 16:26:17 +02:00
|
|
|
_author = authContext.mastodonAuthenticationBox.authentication.user(in: managedObjectContext)?.asRecord
|
2023-03-02 11:06:13 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// save language to recent languages
|
|
|
|
if let settings = context.settingService.currentSetting.value {
|
|
|
|
settings.managedObjectContext?.performAndWait {
|
|
|
|
settings.recentLanguages = [language] + settings.recentLanguages.filter { $0 != language }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MastodonEditStatusPublisher(statusID: status.id,
|
|
|
|
author: author,
|
|
|
|
isContentWarningComposing: isContentWarningActive,
|
|
|
|
contentWarning: contentWarning,
|
|
|
|
content: content,
|
|
|
|
isMediaSensitive: isContentWarningActive,
|
|
|
|
attachmentViewModels: attachmentViewModels,
|
|
|
|
isPollComposing: isPollActive,
|
|
|
|
pollOptions: pollOptions,
|
|
|
|
pollExpireConfigurationOption: pollExpireConfigurationOption,
|
|
|
|
pollMultipleConfigurationOption: pollMultipleConfigurationOption,
|
|
|
|
visibility: visibility,
|
|
|
|
language: language)
|
|
|
|
}
|
|
|
|
|
2022-10-31 13:41:19 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
extension ComposeContentViewModel {
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
public enum AttachmentPrecondition: Error, LocalizedError {
|
|
|
|
case videoAttachWithPhoto
|
|
|
|
case moreThanOneVideo
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
public var errorDescription: String? {
|
|
|
|
return L10n.Common.Alerts.PublishPostFailure.title
|
|
|
|
}
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
public var failureReason: String? {
|
|
|
|
switch self {
|
|
|
|
case .videoAttachWithPhoto:
|
|
|
|
return L10n.Common.Alerts.PublishPostFailure.AttachmentsMessage.videoAttachWithPhoto
|
|
|
|
case .moreThanOneVideo:
|
|
|
|
return L10n.Common.Alerts.PublishPostFailure.AttachmentsMessage.moreThanOneVideo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
// check exclusive limit:
|
|
|
|
// - up to 1 video
|
|
|
|
// - up to N photos
|
|
|
|
public func checkAttachmentPrecondition() throws {
|
|
|
|
let attachmentViewModels = self.attachmentViewModels
|
|
|
|
guard !attachmentViewModels.isEmpty else { return }
|
2022-11-13 15:40:36 +01:00
|
|
|
|
2022-11-13 15:08:26 +01:00
|
|
|
var photoAttachmentViewModels: [AttachmentViewModel] = []
|
|
|
|
var videoAttachmentViewModels: [AttachmentViewModel] = []
|
|
|
|
attachmentViewModels.forEach { attachmentViewModel in
|
|
|
|
guard let output = attachmentViewModel.output else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch output {
|
|
|
|
case .image:
|
|
|
|
photoAttachmentViewModels.append(attachmentViewModel)
|
|
|
|
case .video:
|
|
|
|
videoAttachmentViewModels.append(attachmentViewModel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !videoAttachmentViewModels.isEmpty {
|
|
|
|
guard videoAttachmentViewModels.count == 1 else {
|
|
|
|
throw AttachmentPrecondition.moreThanOneVideo
|
|
|
|
}
|
|
|
|
guard photoAttachmentViewModels.isEmpty else {
|
|
|
|
throw AttachmentPrecondition.videoAttachWithPhoto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-11-11 11:10:13 +01:00
|
|
|
|
|
|
|
// MARK: - AttachmentViewModelDelegate
|
|
|
|
extension ComposeContentViewModel: AttachmentViewModelDelegate {
|
|
|
|
|
|
|
|
public func attachmentViewModel(
|
|
|
|
_ viewModel: AttachmentViewModel,
|
|
|
|
uploadStateValueDidChange state: AttachmentViewModel.UploadState
|
|
|
|
) {
|
|
|
|
Task {
|
|
|
|
try await uploadMediaInQueue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
func uploadMediaInQueue() async throws {
|
2023-09-21 13:10:17 +02:00
|
|
|
for (_, attachmentViewModel) in attachmentViewModels.enumerated() {
|
2022-11-11 11:10:13 +01:00
|
|
|
switch attachmentViewModel.uploadState {
|
|
|
|
case .none:
|
|
|
|
return
|
2022-11-11 14:28:19 +01:00
|
|
|
case .compressing:
|
|
|
|
return
|
2022-11-11 11:10:13 +01:00
|
|
|
case .ready:
|
|
|
|
try await attachmentViewModel.upload()
|
|
|
|
return
|
|
|
|
case .uploading:
|
|
|
|
return
|
|
|
|
case .fail:
|
|
|
|
return
|
|
|
|
case .finish:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func attachmentViewModel(
|
|
|
|
_ viewModel: AttachmentViewModel,
|
|
|
|
actionButtonDidPressed action: AttachmentViewModel.Action
|
|
|
|
) {
|
|
|
|
switch action {
|
|
|
|
case .retry:
|
|
|
|
Task {
|
|
|
|
try await viewModel.upload(isRetry: true)
|
|
|
|
}
|
|
|
|
case .remove:
|
|
|
|
attachmentViewModels.removeAll(where: { $0 === viewModel })
|
|
|
|
Task {
|
|
|
|
try await uploadMediaInQueue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|