mirror of
https://github.com/metabolist/metatext.git
synced 2024-09-26 13:30:02 +00:00
204 lines
7.7 KiB
Swift
204 lines
7.7 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Combine
|
|
import Foundation
|
|
import Mastodon
|
|
import ServiceLayer
|
|
|
|
public final class CompositionViewModel: AttachmentsRenderingViewModel, ObservableObject, Identifiable {
|
|
public let id = Id()
|
|
public var isPosted = false
|
|
@Published public var text: String
|
|
@Published public var contentWarning: String
|
|
@Published public var displayContentWarning: Bool
|
|
@Published public var sensitive: Bool
|
|
@Published public var displayPoll: Bool
|
|
@Published public var pollMultipleChoice: Bool
|
|
@Published public var pollExpiresIn = PollExpiry.oneDay
|
|
@Published public private(set) var pollOptions: [PollOption]
|
|
@Published public private(set) var attachmentViewModels: [AttachmentViewModel]
|
|
@Published public private(set) var attachmentUpload: AttachmentUpload?
|
|
@Published public private(set) var isPostable = false
|
|
@Published public private(set) var canAddAttachment = true
|
|
@Published public private(set) var canAddNonImageAttachment = true
|
|
@Published public private(set) var remainingCharacters = CompositionViewModel.maxCharacters
|
|
public let canRemoveAttachments = true
|
|
|
|
private let eventsSubject: PassthroughSubject<Event, Never>
|
|
private var attachmentUploadCancellable: AnyCancellable?
|
|
|
|
init(eventsSubject: PassthroughSubject<Event, Never>,
|
|
redraft: (status: Status, identification: Identification)? = nil) {
|
|
self.eventsSubject = eventsSubject
|
|
text = redraft?.status.text ?? ""
|
|
contentWarning = redraft?.status.spoilerText ?? ""
|
|
displayContentWarning = !(redraft?.status.spoilerText.isEmpty ?? true)
|
|
sensitive = redraft?.status.sensitive ?? false
|
|
displayPoll = redraft?.status.poll != nil
|
|
pollMultipleChoice = redraft?.status.poll?.multiple ?? false
|
|
pollOptions = redraft?.status.poll?.options.map { PollOption(text: $0.title) }
|
|
?? [PollOption(text: ""), PollOption(text: "")]
|
|
if let redraft = redraft {
|
|
attachmentViewModels = redraft.status.mediaAttachments.map {
|
|
AttachmentViewModel(attachment: $0, identification: redraft.identification)
|
|
}
|
|
} else {
|
|
attachmentViewModels = [AttachmentViewModel]()
|
|
}
|
|
|
|
$text.map { !$0.isEmpty }
|
|
.removeDuplicates()
|
|
.combineLatest($attachmentViewModels.map { !$0.isEmpty })
|
|
.map { textPresent, attachmentPresent in
|
|
textPresent || attachmentPresent
|
|
}
|
|
.assign(to: &$isPostable)
|
|
$attachmentViewModels
|
|
.combineLatest($attachmentUpload, $displayPoll)
|
|
.map { $0.count < Self.maxAttachmentCount && $1 == nil && !$2 }
|
|
.assign(to: &$canAddAttachment)
|
|
$attachmentViewModels.map(\.isEmpty).assign(to: &$canAddNonImageAttachment)
|
|
$text.map {
|
|
let tokens = $0.components(separatedBy: " ")
|
|
|
|
return tokens.map(\.countShorteningIfURL).reduce(tokens.count - 1, +)
|
|
}
|
|
.combineLatest($displayContentWarning, $contentWarning)
|
|
.map { Self.maxCharacters - ($0 + ($1 ? $2.count : 0)) }
|
|
.assign(to: &$remainingCharacters)
|
|
$displayContentWarning.filter { $0 }.assign(to: &$sensitive)
|
|
}
|
|
|
|
public func attachmentSelected(viewModel: AttachmentViewModel) {
|
|
eventsSubject.send(.editAttachment(viewModel, self))
|
|
}
|
|
|
|
public func removeAttachment(viewModel: AttachmentViewModel) {
|
|
attachmentViewModels.removeAll { $0 === viewModel }
|
|
}
|
|
}
|
|
|
|
public extension CompositionViewModel {
|
|
static let maxCharacters = 500
|
|
static let minPollOptionCount = 2
|
|
static let maxPollOptionCount = 4
|
|
|
|
enum Event {
|
|
case editAttachment(AttachmentViewModel, CompositionViewModel)
|
|
case updateAttachment(AnyPublisher<Never, Error>)
|
|
}
|
|
|
|
enum PollExpiry: Int, CaseIterable {
|
|
case fiveMinutes = 300
|
|
case thirtyMinutes = 1800
|
|
case oneHour = 3600
|
|
case sixHours = 21600
|
|
case oneDay = 86400
|
|
case threeDays = 259200
|
|
case sevenDays = 604800
|
|
}
|
|
|
|
class PollOption: ObservableObject {
|
|
public let id = Id()
|
|
@Published public var text: String
|
|
@Published public private(set) var remainingCharacters = CompositionViewModel.maxCharacters
|
|
|
|
public init(text: String) {
|
|
self.text = text
|
|
$text.map { Self.maxCharacters - $0.count }.assign(to: &$remainingCharacters)
|
|
}
|
|
}
|
|
|
|
typealias Id = UUID
|
|
|
|
func components(inReplyToId: Status.Id?, visibility: Status.Visibility) -> StatusComponents {
|
|
StatusComponents(
|
|
inReplyToId: inReplyToId,
|
|
text: text,
|
|
spoilerText: displayContentWarning ? contentWarning : "",
|
|
mediaIds: attachmentViewModels.map(\.attachment.id),
|
|
visibility: visibility,
|
|
sensitive: sensitive,
|
|
pollOptions: displayPoll ? pollOptions.map(\.text) : [],
|
|
pollExpiresIn: pollExpiresIn.rawValue,
|
|
pollMultipleChoice: pollMultipleChoice)
|
|
}
|
|
|
|
func addPollOption() {
|
|
pollOptions.append(PollOption(text: ""))
|
|
}
|
|
|
|
func remove(pollOption: PollOption) {
|
|
pollOptions.removeAll { $0 === pollOption }
|
|
}
|
|
|
|
func cancelUpload() {
|
|
attachmentUploadCancellable?.cancel()
|
|
}
|
|
|
|
func update(attachmentViewModel: AttachmentViewModel) {
|
|
let publisher = attachmentViewModel.updated()
|
|
.receive(on: DispatchQueue.main)
|
|
.handleEvents(receiveOutput: { [weak self] updatedAttachmentViewModel in
|
|
guard let self = self,
|
|
let index = self.attachmentViewModels.firstIndex(
|
|
where: { $0.attachment.id == updatedAttachmentViewModel.attachment.id })
|
|
else { return }
|
|
|
|
self.attachmentViewModels[index] = updatedAttachmentViewModel
|
|
})
|
|
.ignoreOutput()
|
|
.eraseToAnyPublisher()
|
|
|
|
eventsSubject.send(.updateAttachment(publisher))
|
|
}
|
|
}
|
|
|
|
public extension CompositionViewModel.PollOption {
|
|
static let maxCharacters = 25
|
|
|
|
typealias Id = UUID
|
|
}
|
|
|
|
extension CompositionViewModel {
|
|
func attach(itemProvider: NSItemProvider, parentViewModel: NewStatusViewModel) {
|
|
attachmentUploadCancellable = MediaProcessingService.dataAndMimeType(itemProvider: itemProvider)
|
|
.flatMap { [weak self] data, mimeType -> AnyPublisher<Attachment, Error> in
|
|
guard let self = self else { return Empty().eraseToAnyPublisher() }
|
|
|
|
let progress = Progress(totalUnitCount: 1)
|
|
|
|
DispatchQueue.main.async {
|
|
self.attachmentUpload = AttachmentUpload(progress: progress, data: data, mimeType: mimeType)
|
|
}
|
|
|
|
return parentViewModel.identification.service.uploadAttachment(
|
|
data: data,
|
|
mimeType: mimeType,
|
|
progress: progress)
|
|
}
|
|
.receive(on: DispatchQueue.main)
|
|
.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(
|
|
AttachmentViewModel(
|
|
attachment: $0,
|
|
identification: parentViewModel.identification))
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension CompositionViewModel {
|
|
static let maxAttachmentCount = 4
|
|
}
|
|
|
|
private extension String {
|
|
static let urlCharacterCount = 23
|
|
|
|
var countShorteningIfURL: Int {
|
|
starts(with: "http://") || starts(with: "https://") ? Self.urlCharacterCount : count
|
|
}
|
|
}
|