I have a following structure:
class FileUploadCellViewModel {
@Published var isUploaded: Bool = false
}
class FileUploadScreenViewModel: ObservableObject {
@Published var viewModels: [FileUploadCellViewModel] = []
@Published var isSendButtonEnabled: Bool = false
private var cancellables: Set<AnyCancellable> = []
init() {
let publisher = $viewModels
// ?? return a single publisher with `true` if all `isUploaded` are `true`
}
let isDateCorrectPublisher = ...
publisher
.combineLatest(isDateCorrectPublisher)
.sink {
isSendButtonEnabled = $0 && $1
}
.store(in: &cancellables)
}
}
let screen = FileUploadScreenViewModel()
let viewModel1 = FileUploadCellViewModel()
let viewModel2 = FileUploadCellViewModel()
screen.viewModels.append(viewModel1) // expected: isSendButtonEnabled: false
screen.viewModels.append(viewModel2) // expected: isSendButtonEnabled: false
viewModel1.isUploaded = false // expected: isSendButtonEnabled: false
viewModel2.isUploaded = true // expected: isSendButtonEnabled: false
viewModel1.isUploaded = true // expected: isSendButtonEnabled: true
How do I observe each insertion into the viewModels
array and after an element is inserted, observe its isUploaded
property?
I have found a convenience tool to collect an array of publishers, maybe it would be helpful here.