updating tilelet molecule as actionable

This commit is contained in:
Subramaniam, Ramya 2023-02-13 15:50:45 +05:30
parent ba13a9a8f4
commit 44905b0228
2 changed files with 65 additions and 0 deletions

View File

@ -21,6 +21,8 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol {
public var delegateObject: MVMCoreUIDelegateObject?
public var additionalData: [AnyHashable: Any]?
public var tapAction: ActionBlock?
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
@ -30,6 +32,21 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol {
open func updateView(_ size: CGFloat) {}
//--------------------------------------------------
// MARK: - Actions
//--------------------------------------------------
open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event)
tapAction?()
}
open override func sendActions(for controlEvents: UIControl.Event) {
super.sendActions(for: controlEvents)
tapAction?()
}
public func viewModelDidUpdate() {
color = viewModel.color
padding = viewModel.padding
@ -42,11 +59,23 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol {
badgeModel = viewModel.badge
descriptiveIconModel = viewModel.descriptiveIcon
directionalIconModel = viewModel.directionalIcon
if let action = viewModel.action {
tapAction = { [weak self] in
guard let self = self else { return }
MVMCoreUIActionHandler.performActionUnstructured(with: action, sourceModel: self.viewModel, additionalData: self.additionalData, delegateObject: self.delegateObject)
}
}
}
//since this is a class func, we can't reference it directly
public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
100
}
//--------------------------------------------------
// MARK: - Touch Event
//--------------------------------------------------
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sendActions(for: .touchUpInside)
}
}

View File

@ -27,7 +27,24 @@ open class TileletModel: MoleculeModelProtocol {
public var width: CGFloat?
public var textWidth: CGFloat?
public var textPercentage: CGFloat?
public var action: ActionModelProtocol?
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor
case color
case padding
case aspectRatio
case badge
case title
case subTitle
case descriptiveIcon
case directionalIcon
case width
case textWidth
case textPercentage
case action
}
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.backgroundColor = try container.decodeIfPresent(Color.self, forKey: .backgroundColor)
@ -42,6 +59,7 @@ open class TileletModel: MoleculeModelProtocol {
self.width = try container.decodeIfPresent(CGFloat.self, forKey: .width)
self.textWidth = try container.decodeIfPresent(CGFloat.self, forKey: .textWidth)
self.textPercentage = try container.decodeIfPresent(CGFloat.self, forKey: .textPercentage)
action = try container.decodeModelIfPresent(codingKey: .action)
}
public func titleModel(delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Tilelet.TitleModel? {
@ -64,4 +82,22 @@ open class TileletModel: MoleculeModelProtocol {
return .init(text: subTitle.text)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(padding, forKey: .padding)
try container.encodeIfPresent(aspectRatio, forKey: .aspectRatio)
try container.encodeIfPresent(badge, forKey: .badge)
try container.encodeIfPresent(title, forKey: .title)
try container.encodeIfPresent(subTitle, forKey: .subTitle)
try container.encodeIfPresent(descriptiveIcon, forKey: .descriptiveIcon)
try container.encodeIfPresent(directionalIcon, forKey: .directionalIcon)
try container.encodeIfPresent(width, forKey: .width)
try container.encodeIfPresent(textWidth, forKey: .textWidth)
try container.encodeIfPresent(textPercentage, forKey: .textPercentage)
try container.encodeModelIfPresent(action, forKey: .action)
}
}