added action support for selectors

This commit is contained in:
Damodaram 2020-09-15 16:39:41 +05:30
parent 185452e801
commit 34256698c0
6 changed files with 43 additions and 7 deletions

View File

@ -381,7 +381,7 @@ import MVMCore
checkWidth = 2
checkAndBypassAnimations(selected: false)
}
public override func updateView(_ size: CGFloat) {
super.updateView(size)
@ -391,6 +391,12 @@ import MVMCore
}
}
private func performCheckboxAction(with actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
if let actionMap = actionModel.toJSON() {
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
}
}
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData)
self.delegateObject = delegateObject
@ -422,10 +428,16 @@ import MVMCore
isEnabled = model.enabled
if let action = model.action {
actionBlock = {
if let actionMap = action.toJSON() {
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
if (model.action != nil || model.offAction != nil) {
actionBlock = { [weak self] in
guard let self = self else { return }
if let offAction = model.offAction, !self.isSelected {
self.performCheckboxAction(with: offAction, delegateObject: delegateObject, additionalData: additionalData)
return
}
if let action = model.action {
self.performCheckboxAction(with: action, delegateObject: delegateObject, additionalData: additionalData)
}
}
}

View File

@ -32,6 +32,7 @@ import Foundation
public var invertedColor: Color = Color(uiColor: .mvmWhite)
public var invertedBackgroundColor: Color = Color(uiColor: .mvmBlack)
public var action: ActionModelProtocol?
public var offAction: ActionModelProtocol?
public var fieldKey: String?
public var groupName: String = FormValidator.defaultGroupName
@ -61,6 +62,7 @@ import Foundation
case action
case fieldKey
case groupName
case offAction
}
//--------------------------------------------------
@ -155,6 +157,7 @@ import Foundation
if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
self.groupName = groupName
}
offAction = try typeContainer.decodeModelIfPresent(codingKey: .offAction)
}
public func encode(to encoder: Encoder) throws {
@ -179,5 +182,6 @@ import Foundation
try container.encodeIfPresent(enabled, forKey: .enabled)
try container.encodeModelIfPresent(action, forKey: .action)
try container.encodeIfPresent(groupName, forKey: .groupName)
try container.encodeModelIfPresent(offAction, forKey: .offAction)
}
}

View File

@ -8,7 +8,7 @@
import Foundation
open class RadioBox: Control {
open class RadioBox: Control, MFButtonProtocol {
public let label = Label(fontStyle: .RegularBodySmall)
public let subTextLabel = Label(fontStyle: .RegularMicro)
public var isOutOfStock = false
@ -22,6 +22,8 @@ open class RadioBox: Control {
public var subTextLabelHeightConstraint: NSLayoutConstraint?
private var delegateObject: MVMCoreUIDelegateObject?
public var radioBoxModel: RadioBoxModel? {
return model as? RadioBoxModel
}
@ -77,6 +79,7 @@ open class RadioBox: Control {
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData)
guard let model = model as? RadioBoxModel else { return }
self.delegateObject = delegateObject
label.text = model.text
subTextLabel.text = model.subText
isOutOfStock = model.strikethrough
@ -135,6 +138,9 @@ open class RadioBox: Control {
guard isEnabled else { return }
isSelected = true
radioBoxModel?.selected = isSelected
if let actionModel = radioBoxModel?.action {
Button.performButtonAction(with: actionModel, button: self, delegateObject: delegateObject, additionalData: nil)
}
layer.setNeedsDisplay()
}

View File

@ -17,6 +17,7 @@ import Foundation
public var enabled: Bool = true
public var strikethrough: Bool = false
public var fieldValue: String?
public var action: ActionModelProtocol?
private enum CodingKeys: String, CodingKey {
case moleculeName
@ -28,6 +29,7 @@ import Foundation
case enabled
case strikethrough
case fieldValue
case action
}
required public init(from decoder: Decoder) throws {
@ -47,6 +49,7 @@ import Foundation
}
fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
action = try typeContainer.decodeModelIfPresent(codingKey: .action)
}
public func encode(to encoder: Encoder) throws {
@ -60,5 +63,6 @@ import Foundation
try container.encode(enabled, forKey: .enabled)
try container.encode(strikethrough, forKey: .strikethrough)
try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
try container.encodeModelIfPresent(action, forKey: .action)
}
}

View File

@ -9,7 +9,7 @@
import UIKit
open class RadioSwatch: Control {
open class RadioSwatch: Control, MFButtonProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
@ -20,6 +20,8 @@ open class RadioSwatch: Control {
private var strikeLayer: CALayer?
private var maskLayer: CALayer?
private var delegateObject: MVMCoreUIDelegateObject?
public var radioSwatchModel: RadioSwatchModel? {
return model as? RadioSwatchModel
}
@ -57,6 +59,7 @@ open class RadioSwatch: Control {
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData)
guard let model = model as? RadioSwatchModel else { return }
self.delegateObject = delegateObject
bottomText.text = model.text
isSelected = model.selected
isEnabled = model.enabled
@ -117,6 +120,9 @@ open class RadioSwatch: Control {
guard isEnabled else { return }
isSelected = true
radioSwatchModel?.selected = isSelected
if let actionModel = radioSwatchModel?.action {
Button.performButtonAction(with: actionModel, button: self, delegateObject: delegateObject, additionalData: nil)
}
layer.setNeedsDisplay()
}

View File

@ -17,6 +17,7 @@ import Foundation
public var enabled: Bool = true
public var strikethrough: Bool = false
public var fieldValue: String?
public var action: ActionModelProtocol?
private enum CodingKeys: String, CodingKey {
case moleculeName
@ -27,6 +28,7 @@ import Foundation
case enabled
case strikethrough
case fieldValue
case action
}
required public init(from decoder: Decoder) throws {
@ -46,6 +48,7 @@ import Foundation
self.strikethrough = strikethrough
}
fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
action = try typeContainer.decodeModelIfPresent(codingKey: .action)
}
public func encode(to encoder: Encoder) throws {
@ -58,6 +61,7 @@ import Foundation
try container.encode(enabled, forKey: .enabled)
try container.encode(strikethrough, forKey: .strikethrough)
try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
try container.encodeModelIfPresent(action, forKey: .action)
}
}