update to add subscribers

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-11 09:54:58 -05:00
parent ee4bea37ca
commit e6c55ef588

View File

@ -100,18 +100,38 @@ open class SelectorGroup<SelectorType, SelectorGroupType: SelectorGroupModel<Sel
if let inputId = selectorModel.inputId, let foundSelectorView = findSelectorView(inputId: inputId) { if let inputId = selectorModel.inputId, let foundSelectorView = findSelectorView(inputId: inputId) {
foundSelectorView.set(with: selectorModel) foundSelectorView.set(with: selectorModel)
} else { } else {
//create view //create view
let newSelectorView = SelectorHandlerType(with: selectorModel) let newSelectorView = SelectorHandlerType(with: selectorModel)
newSelectorView.didSelect = {[weak self] (inputId) in
guard let self else { return } //add the selectedPublisher for the change
self.didSelect(selected: selectorModel) newSelectorView.selectedPublisher().sink { [weak self] model in
} self?.didSelect(selected: model)
}.store(in: &cancellables)
newSelectorView.$model.sink { [weak self] model in
//should replace
if let cached = self?.getCachedSelector(viewModel: model), newSelectorView.shouldUpdateView(viewModel: cached) {
self?.replace(viewModel: model)
}
}.store(in: &cancellables)
self.selectorViews.append(newSelectorView) self.selectorViews.append(newSelectorView)
mainStackView.addArrangedSubview(newSelectorView) mainStackView.addArrangedSubview(newSelectorView)
} }
} }
} }
public func getCachedSelector(viewModel: SelectorType) -> SelectorType? {
if let index = model.selectors.firstIndex(where: { element in
return element.id == viewModel.id
}) {
return model.selectors[index]
} else {
return nil
}
}
public func replace(viewModel: SelectorType){ public func replace(viewModel: SelectorType){
if let index = model.selectors.firstIndex(where: { element in if let index = model.selectors.firstIndex(where: { element in