181 lines
6.6 KiB
Swift
181 lines
6.6 KiB
Swift
//
|
|
// ConnectionViewIPAD.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Zhandos Bolatbekov on 29.06.2021.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
final class ConnectionViewIPAD: BaseView, ConnectionViewInput {
|
|
|
|
// swiftlint:disable nesting
|
|
private enum Constants {
|
|
|
|
enum Geometry {
|
|
|
|
enum Portrait {
|
|
static let titleTop: CGFloat = 21
|
|
}
|
|
enum Landscape {
|
|
static let titleTop: CGFloat = 11
|
|
}
|
|
static let lockImageSize: CGFloat = 208
|
|
static let viewSize: CGFloat = UIScreen.main.bounds.height
|
|
}
|
|
|
|
enum Font {
|
|
static let title = UIFont.primary(size: 14, weight: .bold)
|
|
}
|
|
|
|
enum LocalizedString {
|
|
static let connected = "connection.ipad.status.connected"
|
|
static let disconnected = "connection.ipad.status.disconnected"
|
|
}
|
|
|
|
enum Color {
|
|
static let titleDefault = UIColor(rgb: 0xFFFFFF)
|
|
static let titleConnected = Colors.SystemAlerts.good.color
|
|
}
|
|
|
|
static let lockImageAnimationDuration: TimeInterval = 2
|
|
}
|
|
// swiftlint:enable nesting
|
|
|
|
private let output: ConnectionViewOutput
|
|
|
|
private var lockView: UIView?
|
|
private var lockViewInput: ConnectionLogoInput?
|
|
private var backgroundImageView: UIImageView?
|
|
private var titleLabel: UILabel?
|
|
private var circlesView: ConnectionCirclesView?
|
|
|
|
// MARK: - Init
|
|
|
|
init(output: ConnectionViewOutput) {
|
|
self.output = output
|
|
super.init()
|
|
self.configureUI()
|
|
}
|
|
|
|
// MARK: - Lifecycle
|
|
|
|
override func didMoveToSuperview() {
|
|
super.didMoveToSuperview()
|
|
|
|
self.output.isReady()
|
|
}
|
|
|
|
// MARK: - ConnectionViewInput
|
|
|
|
func state(text: String, colorState: ConnectionViewColorState, allowClick: Bool) {
|
|
|
|
self.lockView?.isUserInteractionEnabled = allowClick
|
|
|
|
self.lockViewInput?.update(state: colorState)
|
|
switch colorState {
|
|
case .connected:
|
|
self.circlesView?.stopAnimating()
|
|
self.circlesView?.resetState()
|
|
self.titleLabel?.text = NSLocalizedString(Constants.LocalizedString.connected, comment: "Connected")
|
|
self.titleLabel?.textColor = Constants.Color.titleConnected
|
|
case .connecting, .disconnecting:
|
|
self.circlesView?.startAnimating()
|
|
self.titleLabel?.text = nil
|
|
case .disconnected, .error:
|
|
self.circlesView?.stopAnimating()
|
|
self.titleLabel?.text = NSLocalizedString(Constants.LocalizedString.disconnected, comment: "Tap to Connect")
|
|
self.titleLabel?.textColor = Constants.Color.titleDefault
|
|
}
|
|
}
|
|
// MARK: - Private
|
|
private func configureUI() {
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
let circlesView = self.initializeCircles()
|
|
let backgroundView = self.initializeBackgroundView()
|
|
let lockImageView = self.initializeLockImageView()
|
|
let titleLabel = self.initializeTitleLabel()
|
|
|
|
self.portraitConstraints = [
|
|
titleLabel.topAnchor.constraint(equalTo: lockImageView.bottomAnchor,
|
|
constant: Constants.Geometry.Portrait.titleTop)
|
|
]
|
|
|
|
self.landscapeConstraints = [
|
|
titleLabel.topAnchor.constraint(equalTo: lockImageView.bottomAnchor,
|
|
constant: Constants.Geometry.Landscape.titleTop)
|
|
]
|
|
|
|
|
|
self.sharedConstraints = [
|
|
self.widthAnchor.constraint(equalToConstant: Constants.Geometry.viewSize),
|
|
self.heightAnchor.constraint(equalToConstant: Constants.Geometry.viewSize),
|
|
backgroundView.widthAnchor.constraint(equalToConstant: Constants.Geometry.viewSize),
|
|
backgroundView.heightAnchor.constraint(equalToConstant: Constants.Geometry.viewSize),
|
|
circlesView.widthAnchor.constraint(equalTo: self.widthAnchor),
|
|
circlesView.heightAnchor.constraint(equalTo: self.heightAnchor),
|
|
circlesView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
|
|
circlesView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
|
lockImageView.widthAnchor.constraint(equalToConstant: Constants.Geometry.lockImageSize),
|
|
lockImageView.heightAnchor.constraint(equalToConstant: Constants.Geometry.lockImageSize),
|
|
lockImageView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
|
|
lockImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
|
titleLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor)
|
|
]
|
|
|
|
let landscape = DeviceInfoProvider.isLandscapeOrienation && self.traitCollection.horizontalSizeClass == .regular
|
|
self.layoutEmitter.invoke(landscape)
|
|
|
|
circlesView.setNeedsLayout()
|
|
}
|
|
|
|
private func initializeBackgroundView() -> UIView {
|
|
let backgroundView = UIView()
|
|
backgroundView.isUserInteractionEnabled = true
|
|
backgroundView.translatesAutoresizingMaskIntoConstraints = false
|
|
let gesture = UITapGestureRecognizer(target: self, action: #selector(didTapBackground))
|
|
backgroundView.addGestureRecognizer(gesture)
|
|
self.addSubview(backgroundView)
|
|
return backgroundView
|
|
}
|
|
|
|
@objc func didTapBackground() {
|
|
self.lockViewInput?.didTapPastClickZone()
|
|
}
|
|
|
|
private func initializeLockImageView() -> UIView {
|
|
let view = ConnectionLogo()
|
|
view.isUserInteractionEnabled = true
|
|
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTapConnection))
|
|
view.addGestureRecognizer(gestureRecognizer)
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
self.addSubview(view)
|
|
self.lockView = view
|
|
self.lockViewInput = view
|
|
return view
|
|
}
|
|
|
|
private func initializeTitleLabel() -> UILabel {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.font = Constants.Font.title
|
|
label.textAlignment = .center
|
|
self.addSubview(label)
|
|
self.titleLabel = label
|
|
return label
|
|
}
|
|
|
|
private func initializeCircles() -> ConnectionCirclesView {
|
|
let view = ConnectionCirclesView(circlesCount: 4)
|
|
self.addSubview(view)
|
|
self.circlesView = view
|
|
return view
|
|
}
|
|
|
|
@objc func didTapConnection() {
|
|
self.output.connectionClicked()
|
|
}
|
|
}
|