mastodon-ios/Mastodon/Scene/Compose/ComposeViewModel.swift

343 lines
15 KiB
Swift
Raw Normal View History

2021-03-11 08:41:27 +01:00
//
// ComposeViewModel.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-11.
//
import os.log
2021-03-11 08:41:27 +01:00
import UIKit
import Combine
import CoreData
import CoreDataStack
2021-03-18 10:33:07 +01:00
import GameplayKit
import MastodonSDK
2021-03-11 08:41:27 +01:00
final class ComposeViewModel {
static let composeContentLimit: Int = 500
var disposeBag = Set<AnyCancellable>()
2021-03-11 08:41:27 +01:00
// input
let context: AppContext
let composeKind: ComposeStatusSection.ComposeKind
2021-03-16 04:23:19 +01:00
let composeStatusAttribute = ComposeStatusItem.ComposeStatusAttribute()
2021-03-23 11:47:21 +01:00
let isPollComposing = CurrentValueSubject<Bool, Never>(false)
2021-03-25 08:56:17 +01:00
let isCustomEmojiComposing = CurrentValueSubject<Bool, Never>(false)
let isContentWarningComposing = CurrentValueSubject<Bool, Never>(false)
let selectedStatusVisibility = CurrentValueSubject<ComposeToolbarView.VisibilitySelectionType, Never>(.public)
let activeAuthentication: CurrentValueSubject<MastodonAuthentication?, Never>
2021-03-18 10:33:07 +01:00
let activeAuthenticationBox: CurrentValueSubject<AuthenticationService.MastodonAuthenticationBox?, Never>
2021-03-11 08:41:27 +01:00
// output
var diffableDataSource: UICollectionViewDiffableDataSource<ComposeStatusSection, ComposeStatusItem>!
2021-03-25 08:56:17 +01:00
var customEmojiPickerDiffableDataSource: UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem>!
2021-03-18 10:33:07 +01:00
private(set) lazy var publishStateMachine: GKStateMachine = {
// exclude timeline middle fetcher state
let stateMachine = GKStateMachine(states: [
PublishState.Initial(viewModel: self),
PublishState.Publishing(viewModel: self),
PublishState.Fail(viewModel: self),
PublishState.Discard(viewModel: self),
2021-03-18 10:33:07 +01:00
PublishState.Finish(viewModel: self),
])
stateMachine.enter(PublishState.Initial.self)
return stateMachine
}()
private(set) lazy var publishStateMachinePublisher = CurrentValueSubject<PublishState?, Never>(nil)
private(set) var publishDate = Date() // update it when enter Publishing state
// UI & UX
2021-03-11 08:41:27 +01:00
let title: CurrentValueSubject<String, Never>
let shouldDismiss = CurrentValueSubject<Bool, Never>(true)
2021-03-18 12:42:26 +01:00
let isPublishBarButtonItemEnabled = CurrentValueSubject<Bool, Never>(false)
2021-03-23 11:47:21 +01:00
let isMediaToolbarButtonEnabled = CurrentValueSubject<Bool, Never>(true)
let isPollToolbarButtonEnabled = CurrentValueSubject<Bool, Never>(true)
let characterCount = CurrentValueSubject<Int, Never>(0)
2021-03-11 08:41:27 +01:00
// custom emojis
2021-03-25 08:56:17 +01:00
var customEmojiViewModelSubscription: AnyCancellable?
let customEmojiViewModel = CurrentValueSubject<EmojiService.CustomEmojiViewModel?, Never>(nil)
2021-03-25 08:56:17 +01:00
let customEmojiPickerInputViewModel = CustomEmojiPickerInputViewModel()
let isLoadingCustomEmoji = CurrentValueSubject<Bool, Never>(false)
// attachment
let attachmentServices = CurrentValueSubject<[MastodonAttachmentService], Never>([])
2021-03-23 11:47:21 +01:00
// polls
2021-03-24 08:46:40 +01:00
let pollOptionAttributes = CurrentValueSubject<[ComposeStatusItem.ComposePollOptionAttribute], Never>([])
let pollExpiresOptionAttribute = ComposeStatusItem.ComposePollExpiresOptionAttribute()
2021-03-23 11:47:21 +01:00
2021-03-11 08:41:27 +01:00
init(
context: AppContext,
composeKind: ComposeStatusSection.ComposeKind
2021-03-11 08:41:27 +01:00
) {
self.context = context
self.composeKind = composeKind
switch composeKind {
2021-03-15 06:42:46 +01:00
case .post: self.title = CurrentValueSubject(L10n.Scene.Compose.Title.newPost)
case .reply: self.title = CurrentValueSubject(L10n.Scene.Compose.Title.newReply)
2021-03-11 08:41:27 +01:00
}
self.activeAuthentication = CurrentValueSubject(context.authenticationService.activeMastodonAuthentication.value)
2021-03-18 10:33:07 +01:00
self.activeAuthenticationBox = CurrentValueSubject(context.authenticationService.activeMastodonAuthenticationBox.value)
2021-03-11 08:41:27 +01:00
// end init
2021-03-25 08:56:17 +01:00
isCustomEmojiComposing
.assign(to: \.value, on: customEmojiPickerInputViewModel.isCustomEmojiComposing)
.store(in: &disposeBag)
isContentWarningComposing
.assign(to: \.value, on: composeStatusAttribute.isContentWarningComposing)
.store(in: &disposeBag)
// bind active authentication
context.authenticationService.activeMastodonAuthentication
.assign(to: \.value, on: activeAuthentication)
.store(in: &disposeBag)
2021-03-18 10:33:07 +01:00
context.authenticationService.activeMastodonAuthenticationBox
.assign(to: \.value, on: activeAuthenticationBox)
.store(in: &disposeBag)
// bind avatar and names
activeAuthentication
.sink { [weak self] mastodonAuthentication in
guard let self = self else { return }
let mastodonUser = mastodonAuthentication?.user
let username = mastodonUser?.username ?? " "
2021-03-11 08:41:27 +01:00
self.composeStatusAttribute.avatarURL.value = mastodonUser?.avatarImageURL()
self.composeStatusAttribute.displayName.value = {
guard let displayName = mastodonUser?.displayName, !displayName.isEmpty else {
return username
}
return displayName
}()
self.composeStatusAttribute.username.value = username
}
.store(in: &disposeBag)
// bind character count
Publishers.CombineLatest3(
composeStatusAttribute.composeContent.eraseToAnyPublisher(),
composeStatusAttribute.isContentWarningComposing.eraseToAnyPublisher(),
composeStatusAttribute.contentWarningContent.eraseToAnyPublisher()
)
.map { composeContent, isContentWarningComposing, contentWarningContent -> Int in
let composeContent = composeContent ?? ""
var count = composeContent.count
if isContentWarningComposing {
count += contentWarningContent.count
}
return count
}
.assign(to: \.value, on: characterCount)
.store(in: &disposeBag)
// bind compose bar button item UI state
2021-03-18 12:42:26 +01:00
let isComposeContentEmpty = composeStatusAttribute.composeContent
.map { ($0 ?? "").isEmpty }
let isComposeContentValid = composeStatusAttribute.composeContent
.map { composeContent -> Bool in
let composeContent = composeContent ?? ""
return composeContent.count <= ComposeViewModel.composeContentLimit
}
2021-03-18 12:42:26 +01:00
let isMediaEmpty = attachmentServices
.map { $0.isEmpty }
let isMediaUploadAllSuccess = attachmentServices
.map { services in
services.allSatisfy { $0.uploadStateMachineSubject.value is MastodonAttachmentService.UploadState.Finish }
}
2021-03-24 08:46:40 +01:00
let isPollAttributeAllValid = pollOptionAttributes
.map { pollAttributes in
pollAttributes.allSatisfy { attribute -> Bool in
!attribute.option.value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}
let isPublishBarButtonItemEnabledPrecondition1 = Publishers.CombineLatest4(
2021-03-18 12:42:26 +01:00
isComposeContentEmpty.eraseToAnyPublisher(),
isComposeContentValid.eraseToAnyPublisher(),
isMediaEmpty.eraseToAnyPublisher(),
isMediaUploadAllSuccess.eraseToAnyPublisher()
)
.map { isComposeContentEmpty, isComposeContentValid, isMediaEmpty, isMediaUploadAllSuccess -> Bool in
2021-03-18 12:42:26 +01:00
if isMediaEmpty {
return isComposeContentValid && !isComposeContentEmpty
} else {
return isComposeContentValid && isMediaUploadAllSuccess
}
}
.eraseToAnyPublisher()
let isPublishBarButtonItemEnabledPrecondition2 = Publishers.CombineLatest4(
isComposeContentEmpty.eraseToAnyPublisher(),
isComposeContentValid.eraseToAnyPublisher(),
isPollComposing.eraseToAnyPublisher(),
isPollAttributeAllValid.eraseToAnyPublisher()
)
.map { isComposeContentEmpty, isComposeContentValid, isPollComposing, isPollAttributeAllValid -> Bool in
if isPollComposing {
return isComposeContentValid && !isComposeContentEmpty && isPollAttributeAllValid
} else {
return isComposeContentValid && !isComposeContentEmpty
}
}
.eraseToAnyPublisher()
Publishers.CombineLatest(
isPublishBarButtonItemEnabledPrecondition1,
isPublishBarButtonItemEnabledPrecondition2
)
.map { $0 && $1 }
2021-03-18 12:42:26 +01:00
.assign(to: \.value, on: isPublishBarButtonItemEnabled)
.store(in: &disposeBag)
// bind modal dismiss state
composeStatusAttribute.composeContent
.receive(on: DispatchQueue.main)
.map { content in
let content = content ?? ""
return content.isEmpty
}
.assign(to: \.value, on: shouldDismiss)
.store(in: &disposeBag)
// bind custom emojis
context.authenticationService.activeMastodonAuthenticationBox
.receive(on: DispatchQueue.main)
.sink { [weak self] activeMastodonAuthenticationBox in
guard let self = self else { return }
guard let activeMastodonAuthenticationBox = activeMastodonAuthenticationBox else { return }
let domain = activeMastodonAuthenticationBox.domain
// trigger dequeue to preload emojis
self.customEmojiViewModel.value = self.context.emojiService.dequeueCustomEmojiViewModel(for: domain)
}
.store(in: &disposeBag)
2021-03-23 11:47:21 +01:00
// bind snapshot
Publishers.CombineLatest3(
attachmentServices.eraseToAnyPublisher(),
isPollComposing.eraseToAnyPublisher(),
2021-03-24 08:46:40 +01:00
pollOptionAttributes.eraseToAnyPublisher()
2021-03-23 11:47:21 +01:00
)
.receive(on: DispatchQueue.main)
.sink { [weak self] attachmentServices, isPollComposing, pollAttributes in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
var snapshot = diffableDataSource.snapshot()
snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .attachment))
var attachmentItems: [ComposeStatusItem] = []
for attachmentService in attachmentServices {
let item = ComposeStatusItem.attachment(attachmentService: attachmentService)
attachmentItems.append(item)
}
snapshot.appendItems(attachmentItems, toSection: .attachment)
snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .poll))
if isPollComposing {
var pollItems: [ComposeStatusItem] = []
for pollAttribute in pollAttributes {
let item = ComposeStatusItem.pollOption(attribute: pollAttribute)
2021-03-23 11:47:21 +01:00
pollItems.append(item)
}
2021-03-23 11:47:21 +01:00
snapshot.appendItems(pollItems, toSection: .poll)
if pollAttributes.count < 4 {
snapshot.appendItems([ComposeStatusItem.pollOptionAppendEntry], toSection: .poll)
}
snapshot.appendItems([ComposeStatusItem.pollExpiresOption(attribute: self.pollExpiresOptionAttribute)], toSection: .poll)
}
2021-03-23 11:47:21 +01:00
diffableDataSource.apply(snapshot)
// drive service upload state
// make image upload in the queue
for attachmentService in attachmentServices {
// skip when prefix N task when task finish OR fail OR uploading
guard let currentState = attachmentService.uploadStateMachine.currentState else { break }
if currentState is MastodonAttachmentService.UploadState.Fail {
continue
}
if currentState is MastodonAttachmentService.UploadState.Finish {
continue
}
if currentState is MastodonAttachmentService.UploadState.Uploading {
break
}
// trigger uploading one by one
if currentState is MastodonAttachmentService.UploadState.Initial {
attachmentService.uploadStateMachine.enter(MastodonAttachmentService.UploadState.Uploading.self)
break
}
}
}
.store(in: &disposeBag)
// bind delegate
attachmentServices
.sink { [weak self] attachmentServices in
guard let self = self else { return }
attachmentServices.forEach { $0.delegate = self }
}
.store(in: &disposeBag)
2021-03-24 08:46:40 +01:00
pollOptionAttributes
.sink { [weak self] pollAttributes in
guard let self = self else { return }
pollAttributes.forEach { $0.delegate = self }
}
.store(in: &disposeBag)
// bind compose toolbar UI state
2021-03-23 11:47:21 +01:00
Publishers.CombineLatest(
isPollComposing.eraseToAnyPublisher(),
attachmentServices.eraseToAnyPublisher()
)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] isPollComposing, attachmentServices in
guard let self = self else { return }
let shouldMediaDisable = isPollComposing || attachmentServices.count >= 4
let shouldPollDisable = attachmentServices.count > 0
self.isMediaToolbarButtonEnabled.value = !shouldMediaDisable
self.isPollToolbarButtonEnabled.value = !shouldPollDisable
})
.store(in: &disposeBag)
2021-03-11 08:41:27 +01:00
}
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
2021-03-11 08:41:27 +01:00
}
2021-03-18 12:42:26 +01:00
2021-03-23 11:47:21 +01:00
extension ComposeViewModel {
func createNewPollOptionIfPossible() {
2021-03-24 08:46:40 +01:00
guard pollOptionAttributes.value.count < 4 else { return }
2021-03-23 11:47:21 +01:00
let attribute = ComposeStatusItem.ComposePollOptionAttribute()
2021-03-24 08:46:40 +01:00
pollOptionAttributes.value = pollOptionAttributes.value + [attribute]
2021-03-23 11:47:21 +01:00
}
func updatePublishDate() {
publishDate = Date()
}
2021-03-23 11:47:21 +01:00
}
2021-03-18 12:42:26 +01:00
// MARK: - MastodonAttachmentServiceDelegate
extension ComposeViewModel: MastodonAttachmentServiceDelegate {
func mastodonAttachmentService(_ service: MastodonAttachmentService, uploadStateDidChange state: MastodonAttachmentService.UploadState?) {
// trigger new output event
attachmentServices.value = attachmentServices.value
}
}
// MARK: - ComposePollAttributeDelegate
extension ComposeViewModel: ComposePollAttributeDelegate {
func composePollAttribute(_ attribute: ComposeStatusItem.ComposePollOptionAttribute, pollOptionDidChange: String?) {
// trigger update
// pollOptionAttributes.value = pollOptionAttributes.value
}
}