// // NotificationTableViewCell.swift // PrivadoVPN // // Created by Zhandos Bolatbekov on 21.01.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import Foundation import UIKit protocol NotificationCellInput { func display(viewAdapter: NotificationCellAdapter) } class NotificationTableViewCell: UITableViewCell, NotificationCellInput { private enum Constants { enum Color { static let background = UIColor(rgb: 0x202864) static let separator = UIColor(rgb: 0x192147) static let bodyTint = UIColor(rgb: 0x28d799) } static let titleFont = UIFont(name: "SFProText-Semibold", size: 20) static let dateFont = UIFont(name: "SFProText-Semibold", size: 20) static let titleTop: CGFloat = 25 static let titleLeft: CGFloat = 34 static let dateRight: CGFloat = 52 static let bodyLeft: CGFloat = 34 static let bodyRight: CGFloat = 52 static let bodyTop: CGFloat = 10 static let bodyBottom: CGFloat = 20 static let separatorBottom: CGFloat = 2 static let separatorHeight: CGFloat = 1 } private let titleLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.font = Constants.titleFont label.textColor = .white label.lineBreakMode = .byTruncatingTail label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal) label.numberOfLines = 0 return label }() private lazy var bodyTextView: UITextView = { let textView = UITextView() textView.translatesAutoresizingMaskIntoConstraints = false textView.isScrollEnabled = false textView.dataDetectorTypes = .all textView.isEditable = false textView.backgroundColor = .clear textView.textContainerInset = .zero textView.textContainer.lineFragmentPadding = 0 textView.tintColor = Constants.Color.bodyTint textView.delegate = self return textView }() private let dateLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.font = Constants.dateFont label.textColor = .white label.setContentCompressionResistancePriority(.required, for: .horizontal) return label }() private let separatorView: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = Constants.Color.separator return view }() private var linkAction: NotificationCellLinkAction? // MARK: - Init override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.selectionStyle = .none self.backgroundColor = Constants.Color.background self.contentView.isUserInteractionEnabled = false self.configureUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } // MARK: - Interface func display(viewAdapter: NotificationCellAdapter) { self.titleLabel.text = viewAdapter.title self.bodyTextView.attributedText = viewAdapter.body self.dateLabel.text = viewAdapter.date self.linkAction = viewAdapter.linkAction } // MARK: - Methods private func configureUI() { [self.titleLabel, self.dateLabel, self.bodyTextView, self.separatorView].forEach(self.addSubview(_:)) NSLayoutConstraint.activate([ self.titleLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: Constants.titleTop), self.titleLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: Constants.titleLeft), self.titleLabel.rightAnchor.constraint(lessThanOrEqualTo: self.dateLabel.leftAnchor, constant: -16), self.dateLabel.topAnchor.constraint(equalTo: self.titleLabel.topAnchor), self.dateLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -Constants.dateRight), self.separatorView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: Constants.bodyLeft), self.separatorView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -Constants.bodyRight), self.separatorView.heightAnchor.constraint(equalToConstant: Constants.separatorHeight), self.separatorView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -Constants.separatorBottom), self.bodyTextView.topAnchor.constraint(greaterThanOrEqualTo: self.dateLabel.bottomAnchor, constant: Constants.bodyTop), self.bodyTextView.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: Constants.bodyTop), self.bodyTextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: Constants.bodyLeft), self.bodyTextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -Constants.bodyRight), self.bodyTextView.bottomAnchor.constraint(equalTo: self.separatorView.topAnchor, constant: -Constants.bodyBottom) ]) } } extension NotificationTableViewCell: UITextViewDelegate { func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { self.linkAction?(URL) return false } }