Files
2021-09-24 01:33:53 +06:00

229 lines
8.5 KiB
Swift

//
// ModalControllerIPAD.swift
// PrivadoVPN
//
// Created by Zhandos Bolatbekov on 23.09.2021.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import Foundation
import UIKit
final class ModalControllerIPAD: BaseViewController {
// swiftlint:disable nesting
private enum Constants {
enum Color {
static let background = UIColor(rgb: 0x151935).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.primary(size: 28, weight: .semibold)
static let body = UIFont.primary(size: 18, weight: .medium)
static let actionButtonText = UIFont.primary(size: 16, weight: .bold)
}
enum Geometry {
enum Portrait {
static let closeButtonTop: CGFloat = 61
static let closeButtonRight: CGFloat = 24
static let actionButtonTop: CGFloat = 41.5
}
enum Landscape {
static let closeButtonTop: CGFloat = 47
static let closeButtonRight: CGFloat = 31
static let actionButtonTop: CGFloat = 31
}
static let bodyHorizontalOffset: CGFloat = 24
static let titleBottom: CGFloat = 31
static let titleHorizontalOffset: CGFloat = 24
static let actionButtonSize = CGSize(width: 344, height: 50)
static let actionButtonCornerRadius: CGFloat = min(actionButtonSize.width, actionButtonSize.height) / 2
static let closeButtonSize = CGSize(width: 34, height: 34)
}
static let buttonImageName = "modal_close_ipad"
}
// swiftlint:enable nesting
// MARK: - Properties
private let output: ModalControllerOutput
private let titleLabel: UILabel
private let bodyLabel: UILabel
private let actionButton: UIButton
private let closeButton: UIButton
// MARK: - Init
init(output: ModalControllerOutput) {
self.output = output
self.titleLabel = Self.createTitleLabel()
self.bodyLabel = Self.createBodyLabel()
self.actionButton = Self.createActionButton()
self.closeButton = Self.createCloseButton()
super.init()
self.actionButton.addTarget(self, action: #selector(self.onActionButton), for: .touchUpInside)
self.closeButton.addTarget(self, action: #selector(self.onCloseButton), for: .touchUpInside)
}
// MARK: - Life-cycle
override func viewDidLoad() {
super.viewDidLoad()
self.createUI()
}
// MARK: - Private
private func createUI() {
self.view.backgroundColor = Constants.Color.background
[self.titleLabel,
self.bodyLabel,
self.actionButton,
self.closeButton]
.forEach(self.view.addSubview(_:))
self.sharedConstraints = [
self.titleLabel.bottomAnchor.constraint(equalTo: self.bodyLabel.topAnchor, constant: -Constants.Geometry.titleBottom),
self.titleLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Constants.Geometry.titleHorizontalOffset),
self.titleLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.titleHorizontalOffset),
self.bodyLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
self.bodyLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Constants.Geometry.bodyHorizontalOffset),
self.bodyLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.bodyHorizontalOffset),
self.actionButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
self.actionButton.heightAnchor.constraint(equalToConstant: Constants.Geometry.actionButtonSize.height),
self.actionButton.widthAnchor.constraint(equalToConstant: Constants.Geometry.actionButtonSize.width),
self.closeButton.heightAnchor.constraint(equalToConstant: Constants.Geometry.closeButtonSize.height),
self.closeButton.widthAnchor.constraint(equalToConstant: Constants.Geometry.closeButtonSize.width)
]
self.portraitConstraints = [
self.actionButton.topAnchor.constraint(equalTo: self.bodyLabel.bottomAnchor, constant: Constants.Geometry.Portrait.actionButtonTop),
self.closeButton.topAnchor.constraint(equalTo: self.view.topAnchor, constant: Constants.Geometry.Portrait.closeButtonTop),
self.closeButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.Portrait.closeButtonRight)
]
self.landscapeConstraints = [
self.actionButton.topAnchor.constraint(equalTo: self.bodyLabel.bottomAnchor, constant: Constants.Geometry.Landscape.actionButtonTop),
self.closeButton.topAnchor.constraint(equalTo: self.view.topAnchor, constant: Constants.Geometry.Landscape.closeButtonTop),
self.closeButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.Landscape.closeButtonRight)
]
self.layoutForCurrentOrientation()
}
@objc
private func onActionButton() {
self.output.didTapAction()
}
@objc
private func onCloseButton() {
self.output.didTapClose()
}
}
// MARK: - UI
extension ModalControllerIPAD {
private static func createTitleLabel() -> UILabel {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textColor = Constants.Color.titleText
label.font = Constants.Font.title
label.textAlignment = .center
return label
}
private static func createBodyLabel() -> UILabel {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textColor = Constants.Color.bodyText
label.font = Constants.Font.body
label.textAlignment = .center
return label
}
private static func createActionButton() -> 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
return actionButton
}
private static func createCloseButton() -> UIButton {
let icon = UIImage(named: Constants.buttonImageName)?.resizeImage(targetSize: Constants.Geometry.closeButtonSize)
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(icon, for: .normal)
return button
}
}
// MARK: - ModalControllerInput
extension ModalControllerIPAD: ModalControllerInput {
var modalIsVisible: Bool { self.parent.isExist }
func modalUpdate(closable: Bool, imageName: String?, title: String?, description: String?, action: String?) {
self.view.isHidden = false
self.closeButton.isHidden = !closable
if let title = title {
self.titleLabel.text = title
self.titleLabel.isHidden = false
} else {
self.titleLabel.isHidden = true
}
if let description = description {
self.bodyLabel.text = description
self.bodyLabel.isHidden = false
} else {
self.bodyLabel.isHidden = true
}
if let action = action {
self.actionButton.setTitle(action, for: .normal)
self.actionButton.isHidden = false
} else {
self.actionButton.isHidden = true
}
}
func modalHide() {
self.view.isHidden = true
}
func modalLoader(show: Bool) {
show ? LoaderView.shared.startAnimation() : LoaderView.shared.stopAnimation()
}
}