Files
2021-09-06 13:04:05 +03:00

107 lines
3.2 KiB
Swift

//
// MessageViewIPAD.swift
// PrivadoVPN
//
// Created by Lizaveta Malinouskaya on 6.09.21.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import UIKit
class MessageViewIPAD: UIView, MessageViewInput {
// MARK: - Constant
enum Constant {
enum Color {
static let text = Colors.Message.text.color
static let bg = Colors.Message.bg.color
}
enum Geometry {
static let viewHeight: CGFloat = 46
}
enum Font {
static let text = UIFont.primary(size: 15, weight: .semibold)
}
}
// MARK: - Property
private var backgroundView: UIView?
private var messageLabel: UILabel?
private let output: MessageViewOutput
// MARK: - init
public init(output: MessageViewOutput) {
self.output = output
super.init(frame: .zero)
self.setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - UI
private func setupUI() {
self.setupSelf(with: self)
self.setupBackgroundView(with: self)
self.setupMessageLabel(with: self)
}
private func setupSelf(with container: UIView) {
container.translatesAutoresizingMaskIntoConstraints = false
self.isHidden = true
}
private func setupBackgroundView(with container: UIView) {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = Constant.Color.bg
container.addSubview(view)
self.backgroundView = view
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: container.topAnchor),
view.leadingAnchor.constraint(equalTo: container.leadingAnchor),
view.trailingAnchor.constraint(equalTo: container.trailingAnchor),
view.heightAnchor.constraint(equalToConstant: Constant.Geometry.viewHeight)
])
}
private func setupMessageLabel(with container: UIView) {
guard let center = self.backgroundView else { return }
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .center
label.font = Constant.Font.text
label.textColor = Constant.Color.text
label.isUserInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer(target: container, action: #selector(self.didTapView)))
container.addSubview(label)
self.messageLabel = label
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: container.topAnchor),
label.bottomAnchor.constraint(equalTo: container.bottomAnchor),
label.centerXAnchor.constraint(equalTo: container.centerXAnchor),
label.centerYAnchor.constraint(equalTo: center.centerYAnchor)
])
}
@objc func didTapView() {
self.output.didTap()
}
// MARK: - MessageViewInput
func showMessage(text: String) {
self.messageLabel?.text = text
}
func setVisibility(hidden: Bool) {
self.fade(fadeIn: !hidden)
}
}