// // MoleculeTableViewCell.swift // MVMCoreUI // // Created by Scott Pfeil on 4/18/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit @objcMembers open class MoleculeTableViewCell: UITableViewCell, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol { open var molecule: (UIView & MVMCoreUIMoleculeViewProtocol)? // For the accessory view convenience. public var caretView: CaretView? private var caretViewWidthSizeObject: MFSizeObject? private var caretViewHeightSizeObject: MFSizeObject? // For separation between cells. public var topSeparatorView: SeparatorView? public var bottomSeparatorView: SeparatorView? public enum SeparatorFrequency: String { case All = "all" case AllExceptTop = "allExceptTop" case AllExceptBottom = "allExceptBottom" case Between = "between" } // MARK: - Inits public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupView() } public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupView() } // MARK: - MFViewProtocol public func updateView(_ size: CGFloat) { MFStyler.setDefaultMarginsFor(self, size: size) if #available(iOS 11.0, *) { contentView.directionalLayoutMargins = directionalLayoutMargins topSeparatorView?.setLeftAndRightPinConstant(directionalLayoutMargins.leading) bottomSeparatorView?.setLeftAndRightPinConstant(directionalLayoutMargins.leading) } else { contentView.layoutMargins = layoutMargins topSeparatorView?.setLeftAndRightPinConstant(layoutMargins.left) bottomSeparatorView?.setLeftAndRightPinConstant(layoutMargins.left) } if let molecule = molecule as? MVMCoreViewProtocol { molecule.updateView(size) } if let _ = accessoryView, let caretView = caretView, let widthObject = caretViewWidthSizeObject, let heightObject = caretViewHeightSizeObject { caretView.frame = CGRect(x: 0, y: 0, width: widthObject.getValueBased(onSize: size), height: heightObject.getValueBased(onSize: size)) } topSeparatorView?.updateView(size) bottomSeparatorView?.updateView(size) } public func setupView() { preservesSuperviewLayoutMargins = false contentView.preservesSuperviewLayoutMargins = false selectionStyle = .none } // MARK: - MVMCoreUIMoleculeViewProtocol public func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) { guard let json = json else { return } if molecule == nil { if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeForStack(withJSON: json, delegateObject: delegateObject) { contentView.addSubview(moleculeView) NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: moleculeView, useMargins: moleculeView.needsToBeConstrained?() ?? false).values)) molecule = moleculeView } } else { molecule?.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) } backgroundColor = molecule?.backgroundColor } // MARK: - Convenience /// Adds the standard mvm style caret to the accessory view public func addCaretViewAccessory() { guard accessoryView == nil else { return } let width: CGFloat = 6 let height: CGFloat = 10 caretView = CaretView(lineThickness: CaretView.thin) caretView?.frame = CGRect(x: 0, y: 0, width: width, height: height) caretViewWidthSizeObject = MFSizeObject(scalingStandardSize: width) caretViewHeightSizeObject = MFSizeObject(scalingStandardSize: height) accessoryView = caretView } func addSeparatorsIfNeeded() { if topSeparatorView == nil { topSeparatorView = SeparatorView.separatorAdd(to: self, position: SeparatorPositionTop) topSeparatorView?.hide() } if bottomSeparatorView == nil { bottomSeparatorView = SeparatorView.separatorAdd(to: self, position: SeparatorPositionBot) bottomSeparatorView?.hide() } } /// For when the separator between cells shows using json and frequency. public func setSeparatorWithJSON(_ json: [AnyHashable : Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?, indexPath: IndexPath) { guard let json = json else { return } addSeparatorsIfNeeded() topSeparatorView?.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) bottomSeparatorView?.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) if let separatorFrequencyString = json.optionalStringForKey("frequency"), let separatorFrequency = SeparatorFrequency(rawValue: separatorFrequencyString) { setSeparatorFrequency(separatorFrequency, indexPath: indexPath) } } /// For when the separator between cells shows. public func setSeparatorFrequency(_ separatorFrequency: SeparatorFrequency, indexPath: IndexPath) { switch separatorFrequency { case .All: if indexPath.row == 0 { topSeparatorView?.show() } else { topSeparatorView?.hide() } bottomSeparatorView?.show() case .AllExceptBottom: topSeparatorView?.show() bottomSeparatorView?.hide() case .Between: if indexPath.row == 0 { topSeparatorView?.hide() } else { topSeparatorView?.show() } bottomSeparatorView?.hide() case .AllExceptTop: fallthrough default: topSeparatorView?.hide() bottomSeparatorView?.show() } } }