metatext/ViewModels/Sources/ViewModels/CompositionViewModel.swift

96 lines
3.5 KiB
Swift
Raw Normal View History

2020-12-10 02:44:06 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
2021-01-01 00:49:59 +00:00
public final class CompositionViewModel: ObservableObject, Identifiable {
2020-12-18 00:17:17 +00:00
public let id = Id()
2020-12-19 06:30:19 +00:00
public var isPosted = false
2020-12-18 00:17:17 +00:00
@Published public var text = ""
2021-01-01 00:49:59 +00:00
@Published public var contentWarning = ""
@Published public var displayContentWarning = false
2020-12-19 06:30:19 +00:00
@Published public private(set) var attachmentViewModels = [CompositionAttachmentViewModel]()
2020-12-17 06:48:06 +00:00
@Published public private(set) var attachmentUpload: AttachmentUpload?
2021-01-03 22:37:06 +00:00
@Published public private(set) var isPostable = false
@Published public private(set) var canAddAttachment = true
@Published public private(set) var canAddNonImageAttachment = true
2020-12-10 02:44:06 +00:00
2021-01-03 23:57:40 +00:00
private var attachmentUploadCancellable: AnyCancellable?
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
init() {
$text.map { !$0.isEmpty }
.removeDuplicates()
.combineLatest($attachmentViewModels.map { !$0.isEmpty })
.map { textPresent, attachmentPresent in
textPresent || attachmentPresent
}
.assign(to: &$isPostable)
2021-01-03 22:37:06 +00:00
$attachmentViewModels
.combineLatest($attachmentUpload)
.map { $0.count < Self.maxAttachmentCount && $1 == nil }
.assign(to: &$canAddAttachment)
$attachmentViewModels.map(\.isEmpty).assign(to: &$canAddNonImageAttachment)
2020-12-16 01:39:38 +00:00
}
}
public extension CompositionViewModel {
2020-12-18 00:17:17 +00:00
typealias Id = UUID
2020-12-16 01:39:38 +00:00
enum Event {
case insertAfter(CompositionViewModel)
case presentMediaPicker(CompositionViewModel)
2020-12-17 06:48:06 +00:00
case error(Error)
}
2021-01-01 00:49:59 +00:00
func components(inReplyToId: Status.Id?, visibility: Status.Visibility) -> StatusComponents {
2020-12-19 06:30:19 +00:00
StatusComponents(
inReplyToId: inReplyToId,
text: text,
2021-01-01 00:49:59 +00:00
spoilerText: displayContentWarning ? contentWarning : "",
mediaIds: attachmentViewModels.map(\.attachment.id),
visibility: visibility)
2020-12-16 01:39:38 +00:00
}
2021-01-03 01:22:17 +00:00
func remove(attachmentViewModel: CompositionAttachmentViewModel) {
attachmentViewModels.removeAll { $0 === attachmentViewModel }
2020-12-16 01:39:38 +00:00
}
2021-01-03 23:57:40 +00:00
func cancelUpload() {
attachmentUploadCancellable?.cancel()
}
2021-01-01 00:49:59 +00:00
}
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
extension CompositionViewModel {
2021-01-03 23:57:40 +00:00
func attach(itemProvider: NSItemProvider, parentViewModel: NewStatusViewModel) {
attachmentUploadCancellable = MediaProcessingService.dataAndMimeType(itemProvider: itemProvider)
2020-12-17 06:48:06 +00:00
.flatMap { [weak self] data, mimeType -> AnyPublisher<Attachment, Error> in
guard let self = self else { return Empty().eraseToAnyPublisher() }
2021-01-01 00:49:59 +00:00
let progress = Progress(totalUnitCount: 1)
2020-12-17 06:48:06 +00:00
DispatchQueue.main.async {
self.attachmentUpload = AttachmentUpload(progress: progress, data: data, mimeType: mimeType)
}
2021-01-03 23:57:40 +00:00
return parentViewModel.identification.service.uploadAttachment(
data: data,
mimeType: mimeType,
progress: progress)
2020-12-17 06:48:06 +00:00
}
2020-12-19 06:30:19 +00:00
.receive(on: DispatchQueue.main)
2021-01-03 23:57:40 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: parentViewModel)
.handleEvents(receiveCancel: { [weak self] in self?.attachmentUpload = nil })
.sink { [weak self] _ in
self?.attachmentUpload = nil
} receiveValue: { [weak self] in
self?.attachmentViewModels.append(CompositionAttachmentViewModel(attachment: $0))
}
2020-12-19 06:30:19 +00:00
}
2020-12-10 02:44:06 +00:00
}
2021-01-03 22:37:06 +00:00
private extension CompositionViewModel {
static let maxAttachmentCount = 4
}