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 checkWidth = 2
checkAndBypassAnimations(selected: false) checkAndBypassAnimations(selected: false)
} }
public override func updateView(_ size: CGFloat) { public override func updateView(_ size: CGFloat) {
super.updateView(size) 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]?) { public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData) super.set(with: model, delegateObject, additionalData)
self.delegateObject = delegateObject self.delegateObject = delegateObject
@ -422,10 +428,16 @@ import MVMCore
isEnabled = model.enabled isEnabled = model.enabled
if let action = model.action { if (model.action != nil || model.offAction != nil) {
actionBlock = { actionBlock = { [weak self] in
if let actionMap = action.toJSON() { guard let self = self else { return }
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
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 invertedColor: Color = Color(uiColor: .mvmWhite)
public var invertedBackgroundColor: Color = Color(uiColor: .mvmBlack) public var invertedBackgroundColor: Color = Color(uiColor: .mvmBlack)
public var action: ActionModelProtocol? public var action: ActionModelProtocol?
public var offAction: ActionModelProtocol?
public var fieldKey: String? public var fieldKey: String?
public var groupName: String = FormValidator.defaultGroupName public var groupName: String = FormValidator.defaultGroupName
@ -61,6 +62,7 @@ import Foundation
case action case action
case fieldKey case fieldKey
case groupName case groupName
case offAction
} }
//-------------------------------------------------- //--------------------------------------------------
@ -155,6 +157,7 @@ import Foundation
if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
self.groupName = groupName self.groupName = groupName
} }
offAction = try typeContainer.decodeModelIfPresent(codingKey: .offAction)
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
@ -179,5 +182,6 @@ import Foundation
try container.encodeIfPresent(enabled, forKey: .enabled) try container.encodeIfPresent(enabled, forKey: .enabled)
try container.encodeModelIfPresent(action, forKey: .action) try container.encodeModelIfPresent(action, forKey: .action)
try container.encodeIfPresent(groupName, forKey: .groupName) try container.encodeIfPresent(groupName, forKey: .groupName)
try container.encodeModelIfPresent(offAction, forKey: .offAction)
} }
} }

View File

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

View File

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

View File

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

View File

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