// // SelectorGroupHandlerBase.swift // VDS // // Created by Matt Bruce on 9/8/22. // import Foundation import UIKit public class SelectorGroupHandlerBase>: Control, Changable { //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- public var selectorViews: [ModelHandlerType] = [] public var onChange: Blocks.ActionBlock? //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- override public var disabled: Bool { didSet { selectorViews.forEach { handler in handler.disabled = disabled } } } override public var surface: Surface { didSet { selectorViews.forEach { handler in handler.surface = surface } } } public func findSelectorView(inputId: String?) -> ModelHandlerType? { return selectorViews.first(where: { existingSelectorView in return existingSelectorView.model.inputId == inputId }) } public func getCachedSelector(viewModel: ModelHandlerType.ModelType) -> ModelHandlerType.ModelType? { if let index = model.selectors.firstIndex(where: { element in return element.inputId == viewModel.inputId }) { return model.selectors[index] } else { return nil } } public func replace(viewModel: ModelHandlerType.ModelType){ if let index = model.selectors.firstIndex(where: { element in return element.inputId == viewModel.inputId }) { model.selectors[index] = viewModel } } public func createModelHandler(selector: ModelHandlerType.ModelType) -> ModelHandlerType { //create view let newSelectorView = ModelHandlerType(with: selector) //add model update to the subscribers newSelectorView .modelPublisher .sink { [weak self] model in self?.replace(viewModel: model) } .store(in: &subscribers) //add the selectedPublisher for the change newSelectorView .publisher(for: .touchUpInside) .sink { [weak self] control in self?.didSelect(control) } .store(in: &subscribers) return newSelectorView } open func didSelect(_ selectedControl: ModelHandlerType) { fatalError("Must override didSelect") } } public class SelectorGroupSelectedHandlerBase>: SelectorGroupHandlerBase where GroupModelType.SelectorModelType == ModelHandlerType.ModelType { public var selectedModel: ModelHandlerType.ModelType? { return model.selectedModel } }