2021-07-19 11:12:45 +02:00
|
|
|
//
|
|
|
|
// ComposeViewModel.swift
|
|
|
|
// ShareActionExtension
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-16.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
import Combine
|
2021-07-20 10:40:04 +02:00
|
|
|
import CoreDataStack
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
class ComposeViewModel: ObservableObject {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-07-20 10:40:04 +02:00
|
|
|
@Published var authentication: MastodonAuthentication?
|
|
|
|
|
2021-07-20 13:24:24 +02:00
|
|
|
@Published var backgroundColor: UIColor = .clear
|
2021-07-19 11:12:45 +02:00
|
|
|
@Published var toolbarHeight: CGFloat = 0
|
2021-07-20 10:40:04 +02:00
|
|
|
@Published var viewDidAppear = false
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
@Published var avatarImageURL: URL?
|
|
|
|
@Published var authorName: String = ""
|
|
|
|
@Published var authorUsername: String = ""
|
|
|
|
|
|
|
|
@Published var statusContent = ""
|
|
|
|
@Published var statusPlaceholder = ""
|
|
|
|
@Published var statusContentAttributedString = NSAttributedString()
|
|
|
|
|
|
|
|
@Published var isContentWarningComposing = false
|
|
|
|
@Published var contentWarningBackgroundColor = Color.secondary
|
|
|
|
@Published var contentWarningPlaceholder = ""
|
|
|
|
@Published var contentWarningContent = ""
|
|
|
|
|
|
|
|
@Published private(set) var attachmentViewModels: [StatusAttachmentViewModel] = []
|
|
|
|
|
|
|
|
@Published var characterCount = 0
|
|
|
|
|
|
|
|
public init() {
|
|
|
|
$statusContent
|
|
|
|
.map { NSAttributedString(string: $0) }
|
|
|
|
.assign(to: &$statusContentAttributedString)
|
|
|
|
|
|
|
|
Publishers.CombineLatest3(
|
|
|
|
$statusContent,
|
|
|
|
$isContentWarningComposing,
|
|
|
|
$contentWarningContent
|
|
|
|
)
|
|
|
|
.map { statusContent, isContentWarningComposing, contentWarningContent in
|
|
|
|
var count = statusContent.count
|
|
|
|
if isContentWarningComposing {
|
|
|
|
count += contentWarningContent.count
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
.assign(to: &$characterCount)
|
|
|
|
|
2021-07-20 10:40:04 +02:00
|
|
|
// setup attribute updater
|
|
|
|
$attachmentViewModels
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.debounce(for: 0.3, scheduler: DispatchQueue.main)
|
|
|
|
.sink { attachmentViewModels in
|
|
|
|
// drive upload state
|
|
|
|
// make image upload in the queue
|
|
|
|
for attachmentViewModel in attachmentViewModels {
|
|
|
|
// skip when prefix N task when task finish OR fail OR uploading
|
|
|
|
guard let currentState = attachmentViewModel.uploadStateMachine.currentState else { break }
|
|
|
|
if currentState is StatusAttachmentViewModel.UploadState.Fail {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if currentState is StatusAttachmentViewModel.UploadState.Finish {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if currentState is StatusAttachmentViewModel.UploadState.Uploading {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// trigger uploading one by one
|
|
|
|
if currentState is StatusAttachmentViewModel.UploadState.Initial {
|
|
|
|
attachmentViewModel.uploadStateMachine.enter(StatusAttachmentViewModel.UploadState.Uploading.self)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-07-19 11:12:45 +02:00
|
|
|
#if DEBUG
|
2021-07-20 10:40:04 +02:00
|
|
|
// avatarImageURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif")
|
|
|
|
// authorName = "Alice"
|
|
|
|
// authorUsername = "alice"
|
2021-07-19 11:12:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeViewModel {
|
|
|
|
func setupAttachmentViewModels(_ viewModels: [StatusAttachmentViewModel]) {
|
|
|
|
attachmentViewModels = viewModels
|
|
|
|
for viewModel in viewModels {
|
2021-07-20 10:40:04 +02:00
|
|
|
// set delegate
|
|
|
|
viewModel.delegate = self
|
|
|
|
// set observed
|
2021-07-19 11:12:45 +02:00
|
|
|
viewModel.objectWillChange.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.objectWillChange.send()
|
|
|
|
}
|
|
|
|
.store(in: &viewModel.disposeBag)
|
2021-07-20 10:40:04 +02:00
|
|
|
// bind authentication
|
|
|
|
$authentication
|
|
|
|
.assign(to: \.value, on: viewModel.authentication)
|
|
|
|
.store(in: &viewModel.disposeBag)
|
2021-07-19 11:12:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeAttachmentViewModel(_ viewModel: StatusAttachmentViewModel) {
|
|
|
|
if let index = attachmentViewModels.firstIndex(where: { $0 === viewModel }) {
|
|
|
|
attachmentViewModels.remove(at: index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-20 10:40:04 +02:00
|
|
|
|
|
|
|
// MARK: - StatusAttachmentViewModelDelegate
|
|
|
|
extension ComposeViewModel: StatusAttachmentViewModelDelegate {
|
|
|
|
func statusAttachmentViewModel(_ viewModel: StatusAttachmentViewModel, uploadStateDidChange state: StatusAttachmentViewModel.UploadState?) {
|
|
|
|
// trigger event update
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.attachmentViewModels = self.attachmentViewModels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|