// // Notification.swift // VDS // // Created by Nadigadda, Sumanth on 14/03/23. // import Foundation import UIKit import VDSColorTokens import Combine @objc(VDSNotification) /// A VDS Component that will render a view with information public class Notification: View { //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- public enum NotificationStyle: String, CaseIterable { case info, success, warning, error func styleIconName() -> Icon.Name { switch self { case .info: return .infoBold case .success: return .checkmarkAltBold case .warning: return .warningBold case .error: return .errorBold } } } //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private var mainStackView = UIStackView().with { $0.translatesAutoresizingMaskIntoConstraints = false $0.alignment = .top $0.axis = .horizontal $0.spacing = VDSLayout.Spacing.space2X.value } private var labelsView = UIStackView().with { $0.translatesAutoresizingMaskIntoConstraints = false $0.alignment = .top $0.axis = .vertical } private var edgeSpacing: CGFloat { return UIDevice.isIPad ? VDSLayout.Spacing.space5X.value : VDSLayout.Spacing.space4X.value } private var minViewHeight: CGFloat { return UIDevice.isIPad ? VDSLayout.Spacing.space16X.value : VDSLayout.Spacing.space12X.value } private var minContentHeight: CGFloat { return UIDevice.isIPad ? VDSLayout.Spacing.space5X.value : VDSLayout.Spacing.space4X.value } //-------------------------------------------------- // MARK: - View Properties //-------------------------------------------------- open var typeIcon = Icon().with { $0.name = .infoBold } open var closeButton = Icon().with { $0.name = .close } open var titleLabel = Label().with { $0.textStyle = .boldBodyLarge } open var subTitleLabel = Label().with { $0.textStyle = .bodyLarge } open var buttonsView = ButtonGroup().with { $0.buttonPosition = .left } //Text open var titleText: String = "" { didSet{didChange()}} open var subTitleText: String? { didSet{didChange()}} #warning("will need to think about this one, probably create a model that has 2 props - text, onClick = (Button) -> () so we are not accessing the button directly. The only reason why I leave it open is for things like accessibility, but not for setting properties outside of this class. More or less follow how Tilelet is working, look at that, below is a temp fix until we can discuss with the guys") //Buttons open var primaryButtonModel: ButtonModel? { didSet{didChange()}} open var primaryButton = Button().with { $0.size = .small $0.use = .secondary } open var secondaryButtonModel: ButtonModel? { didSet{didChange()}} open var secondaryButton = Button().with { $0.size = .small $0.use = .secondary } open var onCloseClick: ((Notification)->())? { didSet { if let onCloseClick { onCloseSubscriber = closeButton .publisher(for: UITapGestureRecognizer()) .sink { _ in onCloseClick(self) } } else { onCloseSubscriber = nil } } } internal var onCloseSubscriber: AnyCancellable? //-------------------------------------------------- // MARK: - Modal Properties //-------------------------------------------------- open var type: NotificationStyle = .info { didSet{didChange()}} //-------------------------------------------------- // MARK: - Configuration //-------------------------------------------------- private var backgroundColorConfiguration: AnyColorable = { let config = KeyedColorConfiguration(keyPath: \.type) config.setSurfaceColors(VDSColor.feedbackInformationBackgroundOnlight, VDSColor.feedbackInformationBackgroundOndark, forKey: .info) config.setSurfaceColors(VDSColor.feedbackWarningBackgroundOnlight, VDSColor.feedbackWarningBackgroundOndark, forKey: .warning) config.setSurfaceColors(VDSColor.feedbackSuccessBackgroundOnlight, VDSColor.feedbackSuccessBackgroundOndark, forKey: .success) config.setSurfaceColors(VDSColor.feedbackErrorBackgroundOnlight, VDSColor.feedbackErrorBackgroundOndark, forKey: .error) return config.eraseToAnyColorable() }() private var textColorConfig = ViewColorConfiguration().with { $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false) $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: true) } //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero) } public override init(frame: CGRect) { super.init(frame: .zero) } public required init?(coder: NSCoder) { super.init(coder: coder) } //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- open override func setup() { super.setup() addSubview(mainStackView) mainStackView.pinToSuperView(.init(top: edgeSpacing, left: edgeSpacing, bottom: edgeSpacing, right: edgeSpacing)) NSLayoutConstraint.activate([ heightAnchor.constraint(greaterThanOrEqualToConstant: minViewHeight), mainStackView.heightAnchor.constraint(greaterThanOrEqualToConstant: minContentHeight) ]) mainStackView.addArrangedSubview(typeIcon) mainStackView.addArrangedSubview(labelsView) mainStackView.addArrangedSubview(closeButton) //labels titleLabel.textColorConfiguration = textColorConfig.eraseToAnyColorable() subTitleLabel.textColorConfiguration = textColorConfig.eraseToAnyColorable() } open override func reset() { super.reset() titleLabel.reset() subTitleLabel.reset() buttonsView.reset() primaryButton.reset() secondaryButton.reset() type = .info typeIcon.name = .infoBold closeButton.name = .close } //-------------------------------------------------- // MARK: - State //-------------------------------------------------- open override func updateView() { backgroundColor = backgroundColorConfiguration.getColor(self) updateIcons() updateLabels() updateButtons() } private func updateIcons() { let iconColor = surface == .dark ? Icon.Color.white : Icon.Color.black typeIcon.name = type.styleIconName() typeIcon.color = iconColor closeButton.color = iconColor } private func updateLabels() { titleLabel.surface = surface subTitleLabel.surface = surface if !titleText.isEmpty { titleLabel.text = titleText labelsView.addArrangedSubview(titleLabel) } else { titleLabel.removeFromSuperview() } if let subTitleText { subTitleLabel.text = subTitleText labelsView.addArrangedSubview(subTitleLabel) } else { subTitleLabel.removeFromSuperview() } } private func updateButtons() { var buttons: [Button] = [] if let primaryButtonModel { primaryButton.text = primaryButtonModel.text primaryButton.surface = surface primaryButton.onClick = primaryButtonModel.onClick buttons.append(primaryButton) } if let secondaryButtonModel { secondaryButton.text = secondaryButtonModel.text secondaryButton.surface = surface secondaryButton.onClick = secondaryButtonModel.onClick buttons.append(secondaryButton) } if buttons.isEmpty { labelsView.setCustomSpacing(0, after: subTitleLabel) buttonsView.removeFromSuperview() } else { labelsView.setCustomSpacing(VDSLayout.Spacing.space3X.value, after: subTitleLabel) ///This below doesn't work buttonsView.buttons = buttons labelsView.addArrangedSubview(buttonsView) buttonsView .pinLeading() .pinTrailing() } } }