metatext/ViewModels/Sources/ViewModels/NewStatusViewModel.swift

142 lines
5.2 KiB
Swift
Raw Normal View History

2020-12-06 03:10:27 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
import ServiceLayer
public final class NewStatusViewModel: ObservableObject {
2021-01-01 00:49:59 +00:00
@Published public var visibility: Status.Visibility
@Published public private(set) var compositionViewModels = [CompositionViewModel()]
2020-12-10 02:44:06 +00:00
@Published public private(set) var identification: Identification
@Published public private(set) var authenticatedIdentities = [Identity]()
2020-12-16 01:39:38 +00:00
@Published public var canPost = false
2020-12-10 02:44:06 +00:00
@Published public var canChangeIdentity = true
@Published public var alertItem: AlertItem?
2021-01-01 20:18:10 +00:00
@Published public private(set) var postingState = PostingState.composing
2021-01-01 00:49:59 +00:00
public let events: AnyPublisher<Event, Never>
2020-12-06 03:10:27 +00:00
2020-12-10 02:44:06 +00:00
private let allIdentitiesService: AllIdentitiesService
private let environment: AppEnvironment
2021-01-01 00:49:59 +00:00
private let eventsSubject = PassthroughSubject<Event, Never>()
2020-12-16 01:39:38 +00:00
private let itemEventsSubject = PassthroughSubject<CompositionViewModel.Event, Never>()
2020-12-10 02:44:06 +00:00
private var cancellables = Set<AnyCancellable>()
public init(allIdentitiesService: AllIdentitiesService,
identification: Identification,
environment: AppEnvironment) {
self.allIdentitiesService = allIdentitiesService
self.identification = identification
self.environment = environment
2020-12-16 01:39:38 +00:00
events = eventsSubject.eraseToAnyPublisher()
2021-01-01 00:49:59 +00:00
visibility = identification.identity.preferences.postingDefaultVisibility
2020-12-10 02:44:06 +00:00
allIdentitiesService.authenticatedIdentitiesPublisher()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$authenticatedIdentities)
2020-12-16 01:39:38 +00:00
$compositionViewModels.flatMap { Publishers.MergeMany($0.map(\.$isPostable)) }
.receive(on: DispatchQueue.main) // hack to punt to next run loop, consider refactoring
.compactMap { [weak self] _ in self?.compositionViewModels.allSatisfy(\.isPostable) }
2021-01-01 20:18:10 +00:00
.combineLatest($postingState)
.map { $0 && $1 == .composing }
2020-12-16 01:39:38 +00:00
.assign(to: &$canPost)
2020-12-10 02:44:06 +00:00
}
}
public extension NewStatusViewModel {
2021-01-01 00:49:59 +00:00
enum Event {
case presentMediaPicker(CompositionViewModel)
2021-01-01 23:31:39 +00:00
case presentCamera(CompositionViewModel)
2020-12-10 02:44:06 +00:00
}
2021-01-01 20:18:10 +00:00
enum PostingState {
case composing
case posting
case done
}
2020-12-10 02:44:06 +00:00
func setIdentity(_ identity: Identity) {
let identityService: IdentityService
do {
identityService = try allIdentitiesService.identityService(id: identity.id)
} catch {
alertItem = AlertItem(error: error)
return
}
identification = Identification(
identity: identity,
publisher: identityService.identityPublisher(immediate: false)
.assignErrorsToAlertItem(to: \.alertItem, on: self),
service: identityService,
environment: environment)
2020-12-06 03:10:27 +00:00
}
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
func presentMediaPicker(viewModel: CompositionViewModel) {
eventsSubject.send(.presentMediaPicker(viewModel))
2020-12-16 01:39:38 +00:00
}
2021-01-01 23:31:39 +00:00
func presentCamera(viewModel: CompositionViewModel) {
eventsSubject.send(.presentCamera(viewModel))
}
2021-01-01 00:49:59 +00:00
func insert(after: CompositionViewModel) {
guard let index = compositionViewModels.firstIndex(where: { $0 === after })
else { return }
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
let newViewModel = CompositionViewModel()
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
newViewModel.contentWarning = after.contentWarning
newViewModel.displayContentWarning = after.displayContentWarning
2020-12-16 01:39:38 +00:00
2021-01-01 00:49:59 +00:00
if index >= compositionViewModels.count - 1 {
compositionViewModels.append(newViewModel)
} else {
compositionViewModels.insert(newViewModel, at: index + 1)
2020-12-16 01:39:38 +00:00
}
}
2020-12-19 06:30:19 +00:00
2021-01-01 00:49:59 +00:00
func attach(itemProvider: NSItemProvider, to compositionViewModel: CompositionViewModel) {
2021-01-03 23:57:40 +00:00
compositionViewModel.attach(itemProvider: itemProvider, parentViewModel: self)
2021-01-01 00:49:59 +00:00
}
func post() {
guard let unposted = compositionViewModels.first(where: { !$0.isPosted }) else { return }
post(viewModel: unposted, inReplyToId: nil)
}
}
private extension NewStatusViewModel {
2020-12-19 06:30:19 +00:00
func post(viewModel: CompositionViewModel, inReplyToId: Status.Id?) {
2021-01-01 20:18:10 +00:00
postingState = .posting
2021-01-01 00:49:59 +00:00
identification.service.post(statusComponents: viewModel.components(
inReplyToId: inReplyToId,
visibility: visibility))
2020-12-19 06:30:19 +00:00
.receive(on: DispatchQueue.main)
.sink { [weak self] in
guard let self = self else { return }
switch $0 {
case .finished:
2021-01-01 20:18:10 +00:00
if self.compositionViewModels.allSatisfy(\.isPosted) {
self.postingState = .done
}
2020-12-19 06:30:19 +00:00
case let .failure(error):
self.alertItem = AlertItem(error: error)
2021-01-01 20:18:10 +00:00
self.postingState = .composing
2020-12-19 06:30:19 +00:00
}
} receiveValue: { [weak self] in
guard let self = self else { return }
viewModel.isPosted = true
if let unposted = self.compositionViewModels.first(where: { !$0.isPosted }) {
self.post(viewModel: unposted, inReplyToId: $0)
}
}
.store(in: &cancellables)
}
2020-12-06 03:10:27 +00:00
}