updated with comments

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-10-12 15:39:31 -05:00
parent 394f59b0c0
commit 096b834aed
5 changed files with 23 additions and 165 deletions

View File

@ -275,9 +275,9 @@
D2B1E3FB22F4A6930065F95C /* Assets.xcassets */,
D2B1E3FD22F4A6930065F95C /* LaunchScreen.storyboard */,
D2B1E40022F4A6930065F95C /* Info.plist */,
EA1B7BBC2893459E006AF0BC /* DecodableDefaults+VDS.swift */,
EA3362332891F5AB0071C351 /* TestToggleModel.swift */,
EA3361C0288B37FB0071C351 /* TestToggle.swift */,
EA1B7BBC2893459E006AF0BC /* DecodableDefaults+VDS.swift */,
EAF7F0902899825D00B287F5 /* TestLabelToggle.swift */,
EAA5EEFF28F74C43003B3210 /* VDSToggleVM.swift */,
EAA5EF0128F74CE5003B3210 /* TestToggleVM.swift */,

View File

@ -132,7 +132,6 @@ extension AppDelegate {
ModelRegistry.register(TestModel.self)
ModelRegistry.register(handler: TestLabelToggle.self, for: TestLabelToggleModel.self)
ModelRegistry.register(handler: TestToggle.self, for: TestToggleModel.self)
ModelRegistry.register(handler: TestToggle2.self, for: TestToggleModel2.self)
ModelRegistry.register(handler: TestToggle3.self, for: TestToggleModel3.self)
ModelRegistry.register(handler: TextEntryField.self, for: TextEntryField64Model.self)
ModelRegistry.register(handler: EmailVerifyField.self, for: EmailVerifyModel.self)

View File

@ -11,6 +11,9 @@ import MVMCoreUI
import UIKit
import VDS
///-----------------------------------------------------------------------------
///MARK: -- VDSMoleculeViewProtocol (Contract between VDS -> Atomic)
///-----------------------------------------------------------------------------
public protocol VDSMoleculeViewProtocol: MoleculeViewProtocol, MVMCoreViewProtocol {
associatedtype ViewModel: MoleculeModelProtocol
var viewModel: ViewModel! { get set }
@ -28,7 +31,11 @@ extension VDSMoleculeViewProtocol {
}
}
///-----------------------------------------------------------------------------
///MARK: -- TestToggleModel Extension
///-----------------------------------------------------------------------------
extension TestToggleModel {
///Copy properties into a model the then drive the VDS Toggle
public func getVDSModel() -> DefaultToggleModel {
return DefaultToggleModel().copyWith {
$0.disabled = !enabled && readOnly
@ -41,6 +48,14 @@ extension TestToggleModel {
}
}
///-----------------------------------------------------------------------------
///MARK: -- TestToggle
///-----------------------------------------------------------------------------
/// Class that will subclass VDS Toggle, however since our Atomic Toggle Model is a class (Due to EnableableProtocol)
/// we have to split up the models, using ours and the DefaultToggleModel (internal to VDS control)
/// - issuses: we have to then sync values from the internal DefaultToggleModel to the Atomic model since this
/// is the model that talks to the validator
open class TestToggle: ToggleBase<DefaultToggleModel>, VDSMoleculeViewProtocol {
//--------------------------------------------------
// MARK: - Properties
@ -135,70 +150,3 @@ extension TestToggle: MVMCoreUIViewConstrainingProtocol {
public func horizontalAlignment() -> UIStackView.Alignment { .trailing }
}
open class TestToggle2: GenericMolecule<TestToggleModel2>, MVMCoreUIViewConstrainingProtocol {
private var toggle = Toggle()
open override func initialSetup() {
super.initialSetup()
//handle valueChanged
toggle.publisher(for: .valueChanged)
.sink { [weak self] control in
self?.valueDidChange(isOn: control.isOn)
}.store(in: &toggle.subscribers)
}
@Proxy(\.toggle.isOn)
open var isOn: Bool
@Proxy(\.toggle.isEnabled)
open var isEnabled: Bool
open override func setupView(){
super.setupView()
addSubview(toggle)
NSLayoutConstraint.constraintPinSubview(toSuperview: toggle)
}
open override func viewModelUpdate(model: TestToggleModel2) {
toggle.isEnabled = model.enabled
toggle.isOn = model.selected
isUserInteractionEnabled = model.enabled && !model.readOnly
additionalData = additionalData.dictionaryAdding(key: KeySourceModel, value: model)
FormValidator.setupValidation(for: model, delegate: delegateObject?.formHolderDelegate)
}
open func valueDidChange(isOn: Bool) {
print("Toggle valueDidChange: \(isOn)")
if model.action != nil || model.alternateAction != nil {
var action: ActionModelProtocol?
if isOn {
action = model.action
} else {
action = model.alternateAction ?? model.action
}
if let action {
MVMCoreUIActionHandler.performActionUnstructured(with: action,
sourceModel: model,
additionalData: additionalData,
delegateObject: delegateObject)
}
}
}
//MARK - MoleculeViewProtocol
public override static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 44
}
//MARK - MVMCoreUIViewConstrainingProtocol
public func needsToBeConstrained() -> Bool { true }
public func horizontalAlignment() -> UIStackView.Alignment { .trailing }
}

View File

@ -11,6 +11,8 @@ import MVMCore
import MVMCoreUI
import VDS
/// Stripped all UI properties since this is coming from the VDS Toggle itself
public class TestToggleModel: MoleculeModelProtocol, FormFieldProtocol {
//--------------------------------------------------
// MARK: - Properties
@ -108,99 +110,3 @@ public class TestToggleModel: MoleculeModelProtocol, FormFieldProtocol {
try container.encode(readOnly, forKey: .readOnly)
}
}
public class TestToggleModel2: MoleculeModelProtocol, FormFieldProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "testToggle2"
public var backgroundColor: Color? //not used
public var selected: Bool = true
public var enabled: Bool = true
public var readOnly: Bool = false
public var action: ActionModelProtocol?
public var alternateAction: ActionModelProtocol?
public var accessibilityText: String?
public var fieldKey: String?
public var groupName: String = FormValidator.defaultGroupName
public var baseValue: AnyHashable?
//--------------------------------------------------
// MARK: - Keys
//--------------------------------------------------
private enum CodingKeys: String, CodingKey {
case moleculeName
case state
case enabled
case readOnly
case action
case accessibilityIdentifier
case alternateAction
case accessibilityText
case fieldKey
case groupName
}
//--------------------------------------------------
// MARK: - Form Valdiation
//--------------------------------------------------
public func formFieldValue() -> AnyHashable? {
guard enabled else { return nil }
return selected
}
//--------------------------------------------------
// MARK: - Server Value
//--------------------------------------------------
open func formFieldServerValue() -> AnyHashable? {
return formFieldValue()
}
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
public init(_ state: Bool) {
selected = state
baseValue = state
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) {
selected = state
}
action = try typeContainer.decodeModelIfPresent(codingKey: .action)
alternateAction = try typeContainer.decodeModelIfPresent(codingKey: .alternateAction)
accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
baseValue = selected
fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey)
if let gName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
groupName = gName
}
enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
try container.encodeModelIfPresent(action, forKey: .action)
try container.encodeModelIfPresent(alternateAction, forKey: .alternateAction)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encode(selected, forKey: .state)
try container.encode(enabled, forKey: .enabled)
try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
try container.encodeIfPresent(groupName, forKey: .groupName)
try container.encode(readOnly, forKey: .readOnly)
}
}

View File

@ -11,6 +11,9 @@ import MVMCore
import MVMCoreUI
import VDS
/// This is a mixed model of Atomic + VDS.ToggleModel that will be used with the Control
/// There is no requirement for syncing since it is 1 model
public class TestToggleModel3: MoleculeModelProtocol, FormFieldProtocol, VDS.ToggleModel {
//ToggleModel
public var id = UUID()
@ -159,6 +162,8 @@ public class TestToggleModel3: MoleculeModelProtocol, FormFieldProtocol, VDS.Tog
}
}
/// The is a Sample of using the ViewModelHandler which is a an intermediate class between the View and the model
/// Since we are using a viewModel there is more legwork upfront, however you can use classes or structs as the model
open class TestToggle3: ToggleViewModelHandlerBase<ToggleViewModelBase<TestToggleModel3>>, VDSVMMoleculeViewProtocol {
//--------------------------------------------------