Files

168 lines
6.2 KiB
Swift

//
// NotificationTableViewCell.swift
// PrivadoVPN
//
// Created by Lizaveta Malinouskaya on 25.08.21.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import UIKit
class NotificationTableViewCellIPAD: UITableViewCell, NotificationCellInput {
// MARK: - Constant
enum Constant {
enum Color {
static let title = Colors.Notification.title.color
static let bg = Colors.Notification.bg.color
static let accent = Colors.Notification.accent.color
static let separator = Colors.Notification.separator.color
}
enum Geometry {
static let horizontalOffset: CGFloat = 19
static let lineHeight: CGFloat = 18
static let titleTop: CGFloat = 22
static let dateTop: CGFloat = 11
static let textTop: CGFloat = 11
static let separatorTop: CGFloat = 15
static let separatorHeight: CGFloat = 0.5
static let textWidth: CGFloat = 270
}
enum Font {
static let title = UIFont.primary(size: 12, weight: .semibold)
static let body = UIFont.primary(size: 10, weight: .regular)
static let date = UIFont.primary(size: 8, weight: .medium)
}
}
// MARK: - Property
private var titleLabel: UILabel?
private var dateLabel: UILabel?
private var bodyTextView: UITextView?
private var linkAction: NotificationCellLinkAction?
// MARK: - init
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
self.backgroundColor = Constant.Color.bg
self.contentView.isUserInteractionEnabled = false
self.setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Interface
func display(viewAdapter: NotificationCellAdapter) {
self.titleLabel?.attributedText = viewAdapter.attrTitle
self.bodyTextView?.attributedText = viewAdapter.body
self.dateLabel?.text = viewAdapter.date
self.linkAction = viewAdapter.linkAction
}
// MARK: - UI
private func setupUI() {
self.titleLabel = self.initTitle(with: self)
self.dateLabel = self.setupDateLabel(with: self)
self.bodyTextView = self.setupBodyText(with: self)
self.setupSeparator(with: self)
}
private func activateBaseConstraints(for view: UIView, and container: UITableViewCell) {
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalToConstant: Constant.Geometry.textWidth),
view.leftAnchor.constraint(equalTo: container.leftAnchor, constant: Constant.Geometry.horizontalOffset),
view.rightAnchor.constraint(equalTo: container.rightAnchor, constant: -Constant.Geometry.horizontalOffset)
])
}
private func initTitle(with container: UITableViewCell) -> UILabel {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 5
label.font = Constant.Font.title
label.textColor = Constant.Color.title
label.lineBreakMode = .byTruncatingTail
label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
container.addSubview(label)
self.activateBaseConstraints(for: label, and: container)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: container.topAnchor, constant: Constant.Geometry.titleTop)
])
return label
}
private func setupDateLabel(with container: UITableViewCell) -> UILabel? {
guard let top = self.titleLabel else { return nil }
let date = UILabel()
date.translatesAutoresizingMaskIntoConstraints = false
date.font = Constant.Font.date
date.textColor = Constant.Color.accent
container.addSubview(date)
self.activateBaseConstraints(for: date, and: container)
NSLayoutConstraint.activate([
date.topAnchor.constraint(equalTo: top.bottomAnchor, constant: Constant.Geometry.dateTop)
])
return date
}
private func setupBodyText(with container: UITableViewCell) -> UITextView? {
guard let top = self.dateLabel else { return nil }
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 = .white
textView.font = Constant.Font.body
textView.delegate = self
container.addSubview(textView)
self.activateBaseConstraints(for: textView, and: container)
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: top.bottomAnchor, constant: Constant.Geometry.textTop)
])
return textView
}
private func setupSeparator(with container: UITableViewCell) {
guard let top = self.bodyTextView else { return }
let separator = UIView()
separator.translatesAutoresizingMaskIntoConstraints = false
separator.backgroundColor = Constant.Color.separator
container.addSubview(separator)
self.activateBaseConstraints(for: separator, and: container)
NSLayoutConstraint.activate([
separator.topAnchor.constraint(equalTo: top.bottomAnchor, constant: Constant.Geometry.separatorTop),
separator.heightAnchor.constraint(equalToConstant: Constant.Geometry.separatorHeight),
separator.bottomAnchor.constraint(equalTo: container.bottomAnchor)
])
}
}
extension NotificationTableViewCellIPAD: UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
self.linkAction?(URL)
return false
}
}