// // Notification.swift // VDS // // Created by Nadigadda, Sumanth on 14/03/23. // import Foundation import UIKit import VDSColorTokens @objc(VDSNotification) /// A VDS Component that will render a view with information public class Notification: View { public struct ButtonModel { public var text: String public var onClick: (Button) -> () public init(text: String, onClick: @escaping (Button) -> Void) { self.text = text self.onClick = onClick } } //-------------------------------------------------- // 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 = .boldBodySmall } open var subTitleLabel = Label().with { $0.textStyle = .bodySmall } open var buttonsView = ButtonGroup().with { $0.buttonPosition = .left } #warning("we want to add only the things that are needed, everything else should be hidden, meaning don't access controls directly") //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 } //-------------------------------------------------- // 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) closeButton.publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in self?.didClickOnCloseButton() }.store(in: &subscribers) //labels titleLabel.textColorConfiguration = textColorConfig.eraseToAnyColorable() subTitleLabel.textColorConfiguration = textColorConfig.eraseToAnyColorable() } open override func reset() { super.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 let titleText { 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.onClickSubscriber = primaryButton .publisher(for: .touchUpInside) .sink(receiveValue: { button in primaryButtonModel.onClick(button) }) buttons.append(primaryButton) } if let secondaryButtonModel { secondaryButton.text = secondaryButtonModel.text secondaryButton.surface = surface secondaryButton.onClickSubscriber = secondaryButton .publisher(for: .touchUpInside) .sink(receiveValue: { button in secondaryButtonModel.onClick(button) }) 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() } } func didClickOnCloseButton() { print("Notification close button clicked!!!") } ///Temporary Place holder public struct TitleModel { public var text: String public var textAttributes: [any LabelAttributeModel]? public var textStyle: TextStyle = .boldBodySmall public var numberOfLines: Int public init(text: String, textAttributes: [any LabelAttributeModel]? = nil, numberOfLines: Int = 0) { self.text = text self.textAttributes = textAttributes self.numberOfLines = numberOfLines } } public struct SubTitleModel { public var text: String public var textAttributes: [any LabelAttributeModel]? public var textStyle: TextStyle = .bodySmall public var numberOfLines: Int public init(text: String, textColor: Use = .primary, textAttributes: [any LabelAttributeModel]? = nil, numberOfLines: Int = 0) { self.text = text self.textAttributes = textAttributes self.numberOfLines = numberOfLines } } }