// // UpgradeView.swift // PrivadoVPN // // Created by Zhandos Bolatbekov on 29.01.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import Foundation import UIKit final class UpgradeView: UIViewController, UpgradeViewInput { private enum Constants { enum Color { static let background = UIColor(rgb: 0x03072a).withAlphaComponent(0.9) static let titleText = UIColor.white static let bodyText = UIColor(rgb: 0x7a85be) static let actionButtonBackground = UIColor(rgb: 0xff8f00) static let actionButtonText = UIColor.white } enum Font { static let title = UIFont(name: "SFProText-Light", size: 30) static let body = UIFont(name: "SFProText-Regular", size: 21.3) static let actionButtonText = UIFont(name: "Roboto-Black", size: 21.3) } enum Geometry { static let bodyHorizontalOffset: CGFloat = 24 static let titleBottom: CGFloat = 17 static let titleHorizontalOffset: CGFloat = 24 static let actionButtonCornerRadius: CGFloat = 33.3 static let actionButtonHeight: CGFloat = 60.7 static let actionButtonHorizontalOffset: CGFloat = 31.7 static let actionButtonTop: CGFloat = 35.7 } enum LocalizedString { static let actionButtonText = "upgrade.actionButton.title" } } private let output: UpgradeViewOutput private var titleLabel: UILabel? private var bodyLabel: UILabel? private var actionButton: UIButton? // MARK: - Init init(output: UpgradeViewOutput) { self.output = output super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() self.output.viewIsReady() self.configureUI() } override func loadView() { let view = UIView(frame: .zero) view.autoresizesSubviews = true view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = Constants.Color.background self.view = view } // MARK: - UpgradeViewInput func set(title: String?, body: String?) { self.titleLabel?.text = title self.bodyLabel?.text = body self.view.isHidden = false self.parent?.view.bringSubviewToFront(self.view) } func hide() { self.view.isHidden = true } func showLoader(_ show: Bool) { if show { LoaderView.shared.startAnimation() } else { LoaderView.shared.stopAnimation() } self.actionButton?.isEnabled = !show } // MARK: - Private private func configureUI() { let bodyLabel = self.initializeBodyLabel() self.bodyLabel = bodyLabel NSLayoutConstraint.activate([ bodyLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor), bodyLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Constants.Geometry.bodyHorizontalOffset), bodyLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.bodyHorizontalOffset) ]) let titleLabel = self.initializeTitleLabel() self.titleLabel = titleLabel NSLayoutConstraint.activate([ titleLabel.bottomAnchor.constraint(equalTo: bodyLabel.topAnchor, constant: -Constants.Geometry.titleBottom), titleLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Constants.Geometry.titleHorizontalOffset), titleLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.titleHorizontalOffset) ]) let actionButton = self.initializeActionButton() NSLayoutConstraint.activate([ actionButton.topAnchor.constraint(equalTo: bodyLabel.bottomAnchor, constant: Constants.Geometry.actionButtonTop), actionButton.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Constants.Geometry.actionButtonHorizontalOffset), actionButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.actionButtonHorizontalOffset), actionButton.heightAnchor.constraint(equalToConstant: Constants.Geometry.actionButtonHeight) ]) } private func initializeTitleLabel() -> UILabel { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = Constants.Color.titleText label.font = Constants.Font.title label.textAlignment = .center self.view.addSubview(label) return label } private func initializeBodyLabel() -> UILabel { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = Constants.Color.bodyText label.font = Constants.Font.body label.textAlignment = .center self.view.addSubview(label) return label } private func initializeActionButton() -> UIButton { let actionButton = UIButton(frame: .zero) actionButton.translatesAutoresizingMaskIntoConstraints = false actionButton.layer.cornerRadius = Constants.Geometry.actionButtonCornerRadius actionButton.clipsToBounds = true actionButton.backgroundColor = Constants.Color.actionButtonBackground actionButton.setTitleColor(Constants.Color.actionButtonText, for: .normal) actionButton.titleLabel?.font = Constants.Font.actionButtonText actionButton.setTitle(NSLocalizedString(Constants.LocalizedString.actionButtonText, comment: "OK"), for: .normal) actionButton.addTarget(self, action: #selector(self.onActionButtonTouch), for: .touchUpInside) self.view.addSubview(actionButton) self.actionButton = actionButton return actionButton } @objc private func onActionButtonTouch() { self.output.didTapActionButton() } }