Files
Pulse/Sources/PulseUI/Features/Console/Views/ConsoleTaskCellViewModel.swift
T
2023-01-22 12:36:50 -05:00

44 lines
1.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 20202023 Alexander Grebenyuk (github.com/kean).
import SwiftUI
import Pulse
import Combine
import CoreData
final class ConsoleTaskCellViewModel: ObservableObject {
private(set) var pins: PinButtonViewModel?
private(set) var progress: ProgressViewModel?
private var task: NetworkTaskEntity?
private var cancellable: AnyCancellable?
func bind(_ task: NetworkTaskEntity) {
guard task !== self.task else {
return
}
self.task = task
cancellable = nil
progress = nil
if task.state == .pending {
cancellable = task.publisher(for: \.requestState).sink { [weak self] _ in
withAnimation {
self?.objectWillChange.send()
}
}
self.progress = ProgressViewModel(task: task)
}
pins = PinButtonViewModel(task)
}
#if os(iOS) || os(macOS)
func share(as output: ShareOutput) -> ShareItems {
task.map { ShareService.share($0, as: output) } ?? ShareItems([])
}
#endif
}