// // ActionDetailWithImage.swift // MVMCoreUI // // Created by Christiano, Kevin on 6/20/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit @objcMembers open class ActionDetailWithImage: ViewConstrainingView { //------------------------------------------------------ // MARK: - Outlets //------------------------------------------------------ let header = HeadlineBody(frame: .zero) let button = PrimaryButton.primaryTinyButton(false)! let imageLoader = MFLoadImageView(pinnedEdges: .all) let leftContainer = ViewConstrainingView.empty() //------------------------------------------------------ // MARK: - Properties //------------------------------------------------------ var buttonHeaderPadding: CGFloat = PaddingTwo //------------------------------------------------------ // MARK: - Constraints //------------------------------------------------------ var imageLeadingConstraint: NSLayoutConstraint? var buttonTopConstraint: NSLayoutConstraint? //------------------------------------------------------ // MARK: - Initialization //------------------------------------------------------ public init() { super.init(frame: .zero) } public override init(frame: CGRect) { super.init(frame: frame) } required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } public convenience init(json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { self.init() setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) } //------------------------------------------------------ // MARK: - View Lifecycle //------------------------------------------------------ override open func setupView() { super.setupView() guard subviews.isEmpty else { return } setDefaultState() addSubview(leftContainer) addSubview(imageLoader) leftContainer.addSubview(header) leftContainer.addSubview(button) leftContainer.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true leftContainer.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: leftContainer.bottomAnchor).isActive = true let leftContainerBottom = leftContainer.bottomAnchor.constraint(equalTo: bottomAnchor) leftContainerBottom.priority = UILayoutPriority(249) leftContainerBottom.isActive = true header.topAnchor.constraint(equalTo: leftContainer.topAnchor).isActive = true header.leadingAnchor.constraint(equalTo: leftContainer.leadingAnchor).isActive = true leftContainer.trailingAnchor.constraint(equalTo: header.trailingAnchor).isActive = true buttonTopConstraint = button.topAnchor.constraint(equalTo: header.bottomAnchor, constant: buttonHeaderPadding) buttonTopConstraint?.isActive = true button.leadingAnchor.constraint(equalTo: leftContainer.leadingAnchor).isActive = true leftContainer.bottomAnchor.constraint(equalTo: button.bottomAnchor).isActive = true leftContainer.trailingAnchor.constraint(greaterThanOrEqualTo: button.trailingAnchor).isActive = true imageLoader.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true layoutMarginsGuide.trailingAnchor.constraint(equalTo: imageLoader.trailingAnchor).isActive = true imageLeadingConstraint = imageLoader.leadingAnchor.constraint(greaterThanOrEqualTo: leftContainer.trailingAnchor, constant: 16) imageLeadingConstraint?.isActive = true imageLoader.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor).isActive = true layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: imageLoader.bottomAnchor).isActive = true let imageloaderBottom = imageLoader.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor) imageloaderBottom.priority = UILayoutPriority(249) imageloaderBottom.isActive = true } override open func updateView(_ size: CGFloat) { super.updateView(size) header.updateView(size) button.updateView(size) imageLoader.updateView(size) leftContainer.updateView(size) buttonTopConstraint?.constant = header.hasText() ? buttonHeaderPadding : 0 } public override static func estimatedHeight(forRow json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat { return 197 } //------------------------------------------------------ // MARK: - Methods //------------------------------------------------------ private func setDefaultState() { header.headlineLabel.font = MFStyler.fontH3() header.messageLabel.font = MFStyler.fontB3() button.setAsSecondaryCustom() button.isHidden = false imageLoader.imageView.contentMode = .scaleAspectFit imageLoader.addSizeConstraintsForAspectRatio = true buttonHeaderPadding = PaddingTwo } override open func reset() { super.reset() header.reset() button.reset() imageLeadingConstraint?.constant = 16 imageLoader.reset() setDefaultState() } open override func setAsMolecule() { super.setAsMolecule() header.setAsMolecule() button.setAsMolecule() imageLoader.setAsMolecule() setDefaultState() } open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) guard let dictionary = json else { return } if let padding = dictionary.optionalCGFloatForKey("buttonHeaderPadding") { buttonHeaderPadding = padding } header.setWithJSON(dictionary.optionalDictionaryForKey("headlineBody"), delegateObject: delegateObject, additionalData: additionalData) imageLoader.setWithJSON(dictionary.optionalDictionaryForKey("image"), delegateObject: delegateObject, additionalData: additionalData) if let buttonDictionary = dictionary.optionalDictionaryForKey("button") { button.setWithJSON(buttonDictionary, delegateObject: delegateObject, additionalData: additionalData) } else { button.isHidden = true } } }