// // UpgradeWarningView.swift // PrivadoVPN // // Created by Zhandos Bolatbekov on 25.02.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import UIKit protocol UpgradeWarningViewOutput: AnyObject { func didTapUpgradeButton() } final class UpgradeWarningView: UIView { private enum Constants { enum Color { static let background = UIColor(rgb: 0x000424).withAlphaComponent(0.9) static let bodyText = UIColor(rgb: 0x7a85be) static let headlineText = UIColor.white static let buttonText = UIColor.white static let buttonBackground = UIColor(rgb: 0x2ad899) } enum Geometry { static let closeButtonSize = CGSize(width: 32, height: 32) static let closeButtonTop: CGFloat = 13 static let closeButtonRight: CGFloat = 20 static let bodyCenterYOffset: CGFloat = -37 static let buttonHeight: CGFloat = 60 static let buttonTop: CGFloat = 52 static let commonIndent: CGFloat = 32 static let headlineBottom: CGFloat = 16 } enum Font { static let headline = UIFont(name: "SFProText-Light", size: 30) ?? .systemFont(ofSize: 30, weight: .light) static let body = UIFont(name: "SFProText-Regular", size: 21.3) ?? .systemFont(ofSize: 21.3, weight: .regular) static let buttonText = UIFont(name: "Roboto-Black", size: 21.3) ?? .systemFont(ofSize: 21.3, weight: .semibold) } enum LocalizedString { static let headline = "upgradeWarning.headline" static let body = "upgradeWarning.body" static let actionButton = "upgradeWarning.button" } static let buttonImageName = "close_rounded" } private let output: UpgradeWarningViewOutput // MARK: - Init init(output: UpgradeWarningViewOutput) { self.output = output super.init(frame: .zero) self.configureUI() } required init?(coder: NSCoder) { nil } // MARK: - Private private func configureUI() { self.translatesAutoresizingMaskIntoConstraints = false self.backgroundColor = Constants.Color.background let closeButton = self.initializeCloseButton() NSLayoutConstraint.activate([ closeButton.topAnchor.constraint(equalTo: self.topAnchor, constant: Constants.Geometry.closeButtonTop), closeButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -Constants.Geometry.closeButtonRight), closeButton.heightAnchor.constraint(equalToConstant: Constants.Geometry.closeButtonSize.height), closeButton.widthAnchor.constraint(equalToConstant: Constants.Geometry.closeButtonSize.width) ]) let bodyLabel = self.initializeBodyLabel() NSLayoutConstraint.activate([ bodyLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor), bodyLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: Constants.Geometry.commonIndent), bodyLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -Constants.Geometry.commonIndent) ]) let headlineLabel = self.initializeHeadlineLabel() NSLayoutConstraint.activate([ headlineLabel.bottomAnchor.constraint(equalTo: bodyLabel.topAnchor, constant: -Constants.Geometry.headlineBottom), headlineLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: Constants.Geometry.commonIndent), headlineLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -Constants.Geometry.commonIndent) ]) let button = self.initializeActionButton() NSLayoutConstraint.activate([ button.topAnchor.constraint(equalTo: bodyLabel.bottomAnchor, constant: Constants.Geometry.buttonTop), button.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: Constants.Geometry.commonIndent), button.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -Constants.Geometry.commonIndent), button.heightAnchor.constraint(equalToConstant: Constants.Geometry.buttonHeight) ]) } private func initializeHeadlineLabel() -> UILabel { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = Constants.Color.headlineText label.font = Constants.Font.headline label.textAlignment = .center label.text = NSLocalizedString(Constants.LocalizedString.headline, comment: "You’ve selected a premium server.") self.addSubview(label) return label } private func initializeBodyLabel() -> UILabel { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.font = Constants.Font.body label.textAlignment = .center label.textColor = Constants.Color.bodyText label.text = NSLocalizedString(Constants.LocalizedString.body, comment: "Upgrade now to access additional worldwide servers!") label.numberOfLines = 0 self.addSubview(label) return label } private func initializeActionButton() -> UIView { let button = UIButton(frame: .zero) button.translatesAutoresizingMaskIntoConstraints = false button.layer.cornerRadius = Constants.Geometry.buttonHeight / 2 button.clipsToBounds = true button.backgroundColor = Constants.Color.buttonBackground button.setTitleColor(Constants.Color.buttonText, for: .normal) button.titleLabel?.font = Constants.Font.buttonText button.setTitle(NSLocalizedString(Constants.LocalizedString.actionButton, comment: "UPGRADE"), for: .normal) button.addTarget(self, action: #selector(self.onActionButtonTouch), for: .touchUpInside) self.addSubview(button) return button } private func initializeCloseButton() -> UIButton { let icon = UIImage(named: Constants.buttonImageName)?.resizeImage(targetSize: Constants.Geometry.closeButtonSize) let button = UIButton() button.translatesAutoresizingMaskIntoConstraints = false button.setImage(icon, for: .normal) button.addTarget(self, action: #selector(self.didTapClose), for: .touchUpInside) self.addSubview(button) return button } @objc private func didTapClose() { self.isHidden = true } @objc private func onActionButtonTouch() { self.isHidden = true self.output.didTapUpgradeButton() } }