From 44905b0228a5714fb519f6a0896b02662f9c8f15 Mon Sep 17 00:00:00 2001 From: "Subramaniam, Ramya" Date: Mon, 13 Feb 2023 15:50:45 +0530 Subject: [PATCH] updating tilelet molecule as actionable --- MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift | 29 +++++++++++++++ .../Atomic/Atoms/Views/TileletModel.swift | 36 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift b/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift index bd50714f..75f8fd72 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift @@ -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, with event: UIEvent?) { + sendActions(for: .touchUpInside) + } } diff --git a/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift b/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift index b349eeb5..c6685364 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift @@ -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) + } }