vds_ios/VDS/Components/Selector/SelectorGroupModelable.swift
Matt Bruce 844aa13277 refactored out selector base selector group base class
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-23 14:49:00 -05:00

51 lines
1.5 KiB
Swift

//
// 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
}
}
}