// // NotificationsViewFormIPAD.swift // OpenVPN // // Created by Lizaveta Malinouskaya on 24.08.21. // Copyright © 2021 Privado LLC. All rights reserved. // import UIKit protocol NotificationsControllerInputIPAD { func display(notificationCells: [NotificationCellAdapter]) } class NotificationsViewIPAD: UIViewController, NotificationsControllerInput { // MARK: - Constant enum Constant { static let selfSize = CGSize(width: 305, height: 311) static let estimatedCellHeight: CGFloat = 1000 static let tableViewTop: CGFloat = 44 static let labelFont = UIFont.primary(size: 12, weight: .semibold) static let labelText = NSLocalizedString("notificationCenter.empty", comment: "There are currently no notifications.") static let labelWidth: CGFloat = 200 static let cornerRadius: CGFloat = 10 } // MARK: - Property public var navigationBar: NotificationsNavBarIPAD? { didSet { self.setupNavigationBar(with: self.view) } } public let output: NotificationNavBarOutput var notificationsTableView: UITableView? var notificationsTableViewManager: NotificationsTableViewManager? var notificationsEmptyLabel: UILabel? // MARK: - init public init(with output: NotificationNavBarOutput) { self.output = output super.init(nibName: nil, bundle: nil) self.setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewWillAppear(_ animated: Bool) { self.output.viewIsReady() } override func loadView() { let view = UIView(frame: .zero) view.autoresizesSubviews = true view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = Colors.Notification.bg.color view.layer.cornerRadius = 10 self.view = view } // MARK: - NotificationsControllerInput func display(notificationCells: [NotificationCellAdapter]) { self.notificationsTableViewManager?.display(notificationCells: notificationCells) if notificationCells.isEmpty { self.notificationsTableView?.isHidden = true self.notificationsEmptyLabel?.isHidden = false } else { self.notificationsTableView?.isHidden = false self.notificationsEmptyLabel?.isHidden = true } } // MARK: - UI private func setupUI() { NSLayoutConstraint.activate([ self.view.widthAnchor.constraint(equalToConstant: Constant.selfSize.width), self.view.heightAnchor.constraint(equalToConstant: Constant.selfSize.height) ]) self.notificationsTableView = self.setupTableView(with: self.view) self.notificationsTableViewManager = NotificationsTableViewManager(tableView: self.notificationsTableView!) self.notificationsEmptyLabel = self.setupEmptyLabel(with: self.view) } private func setupTableView(with container: UIView) -> UITableView { let tableView = UITableView() tableView.translatesAutoresizingMaskIntoConstraints = false tableView.separatorStyle = .none tableView.layer.cornerRadius = Constant.cornerRadius tableView.backgroundColor = .clear tableView.showsVerticalScrollIndicator = true tableView.estimatedRowHeight = Constant.estimatedCellHeight tableView.rowHeight = UITableView.automaticDimension tableView.indicatorStyle = .white if #available(iOS 11.1, *) { tableView.verticalScrollIndicatorInsets = .init(top: 22, left: 0, bottom: 0, right: 3) } container.addSubview(tableView) NSLayoutConstraint.activate([ tableView.topAnchor.constraint(equalTo: container.topAnchor, constant: Constant.tableViewTop), tableView.leadingAnchor.constraint(equalTo: container.leadingAnchor), tableView.trailingAnchor.constraint(equalTo: container.trailingAnchor), tableView.bottomAnchor.constraint(equalTo: container.bottomAnchor) ]) return tableView } private func setupEmptyLabel(with container: UIView) -> UILabel { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.textColor = .white label.numberOfLines = 2 label.textAlignment = .center label.font = Constant.labelFont label.text = Constant.labelText container.addSubview(label) NSLayoutConstraint.activate([ label.widthAnchor.constraint(equalToConstant: Constant.labelWidth), label.centerXAnchor.constraint(equalTo: container.centerXAnchor), label.centerYAnchor.constraint(equalTo: container.centerYAnchor) ]) return label } private func setupNavigationBar(with container: UIView) { guard let navBar = self.navigationBar else { return } container.addSubview(navBar) NSLayoutConstraint.activate([ navBar.heightAnchor.constraint(equalToConstant: 40), navBar.topAnchor.constraint(equalTo: container.topAnchor), navBar.leadingAnchor.constraint(equalTo: container.leadingAnchor), navBar.trailingAnchor.constraint(equalTo: container.trailingAnchor) ]) navBar.setNeedsLayout() container.layoutSubviews() } }