2020-12-10 02:44:06 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import Mastodon
|
|
|
|
import ServiceLayer
|
|
|
|
|
|
|
|
public final class CompositionViewModel: ObservableObject {
|
|
|
|
public let composition: Composition
|
2020-12-16 01:39:38 +00:00
|
|
|
@Published public private(set) var isPostable = false
|
2020-12-10 02:44:06 +00:00
|
|
|
@Published public private(set) var identification: Identification
|
|
|
|
|
2020-12-16 01:39:38 +00:00
|
|
|
private let eventsSubject: PassthroughSubject<Event, Never>
|
|
|
|
|
2020-12-10 02:44:06 +00:00
|
|
|
init(composition: Composition,
|
|
|
|
identification: Identification,
|
2020-12-16 01:39:38 +00:00
|
|
|
identificationPublisher: AnyPublisher<Identification, Never>,
|
|
|
|
eventsSubject: PassthroughSubject<Event, Never>) {
|
2020-12-10 02:44:06 +00:00
|
|
|
self.composition = composition
|
|
|
|
self.identification = identification
|
2020-12-16 01:39:38 +00:00
|
|
|
self.eventsSubject = eventsSubject
|
2020-12-10 02:44:06 +00:00
|
|
|
identificationPublisher.assign(to: &$identification)
|
2020-12-16 01:39:38 +00:00
|
|
|
composition.$text.map { !$0.isEmpty }.removeDuplicates().assign(to: &$isPostable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension CompositionViewModel {
|
|
|
|
enum Event {
|
|
|
|
case insertAfter(CompositionViewModel)
|
|
|
|
case presentMediaPicker(CompositionViewModel)
|
|
|
|
case attach(itemProvider: NSItemProvider, viewModel: CompositionViewModel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func presentMediaPicker() {
|
|
|
|
eventsSubject.send(.presentMediaPicker(self))
|
|
|
|
}
|
|
|
|
|
|
|
|
func insert() {
|
|
|
|
eventsSubject.send(.insertAfter(self))
|
|
|
|
}
|
|
|
|
|
|
|
|
func attach(itemProvider: NSItemProvider) {
|
|
|
|
eventsSubject.send(.attach(itemProvider: itemProvider, viewModel: self))
|
2020-12-10 02:44:06 +00:00
|
|
|
}
|
|
|
|
}
|