// // SelectorGroupModel.swift // VDS // // Created by Matt Bruce on 8/11/22. // import Foundation public protocol SelectorGroupModelable: Modelable, FormFieldable, Errorable { associatedtype SelectorModelType: Modelable where SelectorModelType: FormFieldable var selectors: [SelectorModelType] { get set } } public protocol SelectorGroupHandlerable: ModelHandlerable, Disabling, Surfaceable where ModelType: SelectorGroupModelable { associatedtype ModelHandlerType: ModelHandlerable where ModelType.SelectorModelType == ModelHandlerType.ModelType var selectorViews: [ModelHandlerType] { get set } } extension SelectorGroupHandlerable { public func updateSelectors(){ let selectors = model.selectors.compactMap { existing in return existing.copyWith { $0.disabled = disabled $0.surface = surface } } model.selectors = selectors } public func getCachedSelector(viewModel: ModelHandlerType.ModelType) -> ModelHandlerType.ModelType? { 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: ModelHandlerType.ModelType){ if let index = model.selectors.firstIndex(where: { element in return element.id == viewModel.id }) { model.selectors[index] = viewModel } } }