204 lines
7.4 KiB
Swift
204 lines
7.4 KiB
Swift
//
|
|
// ConnectionView.swift
|
|
// Privado
|
|
//
|
|
// Created by Jura on 10/10/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
protocol ConnectionViewInput: AnyObject {
|
|
func state(text: String, colorState: ConnectionViewColorState, allowClick: Bool)
|
|
}
|
|
|
|
protocol ConnectionViewOutput: AnyObject {
|
|
func isReady()
|
|
func connectionClicked()
|
|
}
|
|
|
|
final class ConnectionView: View, ConnectionViewInput {
|
|
|
|
private enum Constants {
|
|
enum ImageName {
|
|
static let background = "connection.background"
|
|
static let ellipse = "connection.ellipse"
|
|
}
|
|
enum Geometry {
|
|
static let viewSize = UIScreen.main.bounds.height
|
|
static let bgWidthMultiplier: CGFloat = UIScreen.main.bounds.height >= 736 ? 0.5 : 0.4
|
|
static let logoWidthMultiplier: CGFloat = 0.56
|
|
static let ellipseWidthMultiplier: CGFloat = 0.65
|
|
}
|
|
|
|
static let colorConnected = UIColor(rgb: 0x28d799)
|
|
static let colorProgress = UIColor(red: 1, green: 0.7690353394, blue: 0.2566133142, alpha: 1)
|
|
static let colorDisconnected = UIColor(rgb: 0xe43b69)
|
|
static let colorError = UIColor(rgb: 0xe43b69)
|
|
|
|
static let titleFont = UIFont(name: "SFProText-Black", size: UIScreen.main.bounds.height >= 736 ? 23 : 20)
|
|
}
|
|
|
|
private let output: ConnectionViewOutput
|
|
|
|
private var tapBackground: UIView?
|
|
private var backgroundImageView: UIImageView?
|
|
private var connectionImageInput: ConnectionLogoInput?
|
|
private var connectionImage: UIView?
|
|
private var titleLabel: UILabel?
|
|
|
|
// MARK: - Init
|
|
|
|
init(output: ConnectionViewOutput) {
|
|
self.output = output
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
override func didMoveToSuperview() {
|
|
super.didMoveToSuperview()
|
|
|
|
self.output.isReady()
|
|
}
|
|
|
|
// MARK: - View
|
|
|
|
override func setup() {
|
|
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
self.action = self.onClick
|
|
|
|
super.setup()
|
|
}
|
|
|
|
override func addSubviews() {
|
|
super.addSubviews()
|
|
|
|
self.tapBackground = self.initializeBackgroundView()
|
|
self.backgroundImageView = self.initializeBackgroundImageView()
|
|
let imageView = self.initializeImageView()
|
|
self.connectionImage = imageView
|
|
self.titleLabel = self.initializeTitle()
|
|
}
|
|
|
|
override func addConstraints() {
|
|
super.addConstraints()
|
|
|
|
guard let backgroundImageView = self.backgroundImageView
|
|
, let background = self.tapBackground
|
|
, let logotype = self.connectionImage
|
|
, let label = self.titleLabel else {
|
|
return
|
|
}
|
|
NSLayoutConstraint.activate([
|
|
background.centerXAnchor.constraint(equalTo: self.centerXAnchor),
|
|
background.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
|
background.heightAnchor.constraint(equalToConstant: Constants.Geometry.viewSize),
|
|
background.widthAnchor.constraint(equalToConstant: Constants.Geometry.viewSize),
|
|
backgroundImageView.topAnchor.constraint(equalTo: self.topAnchor),
|
|
backgroundImageView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
|
|
backgroundImageView.widthAnchor.constraint(equalTo: self.widthAnchor,
|
|
multiplier: Constants.Geometry.bgWidthMultiplier),
|
|
backgroundImageView.heightAnchor.constraint(equalTo: backgroundImageView.widthAnchor),
|
|
|
|
logotype.centerXAnchor.constraint(equalTo: backgroundImageView.centerXAnchor),
|
|
logotype.centerYAnchor.constraint(equalTo: backgroundImageView.centerYAnchor),
|
|
logotype.widthAnchor.constraint(equalTo: backgroundImageView.widthAnchor,
|
|
multiplier: Constants.Geometry.logoWidthMultiplier),
|
|
logotype.heightAnchor.constraint(equalTo: logotype.widthAnchor),
|
|
label.topAnchor.constraint(equalTo: backgroundImageView.bottomAnchor),
|
|
label.leadingAnchor.constraint(equalTo: self.leadingAnchor),
|
|
label.trailingAnchor.constraint(equalTo: self.trailingAnchor),
|
|
label.bottomAnchor.constraint(equalTo: self.bottomAnchor)
|
|
])
|
|
}
|
|
|
|
// MARK: - UI
|
|
|
|
private func initializeBackgroundImageView() -> UIImageView {
|
|
let image = UIImage(imageLiteralResourceName: Constants.ImageName.background)
|
|
let view = UIImageView(image: image)
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
let ellipseImage = UIImage(imageLiteralResourceName: Constants.ImageName.ellipse)
|
|
let ellipseView = UIImageView(image: ellipseImage)
|
|
ellipseView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(ellipseView)
|
|
NSLayoutConstraint.activate([
|
|
ellipseView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
|
ellipseView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
|
|
ellipseView.widthAnchor.constraint(equalTo: view.widthAnchor,
|
|
multiplier: Constants.Geometry.ellipseWidthMultiplier),
|
|
ellipseView.heightAnchor.constraint(equalTo: ellipseView.widthAnchor)
|
|
])
|
|
|
|
self.addSubview(view)
|
|
return view
|
|
}
|
|
|
|
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.connectionImageInput?.didTapPastClickZone()
|
|
}
|
|
|
|
private func initializeImageView() -> UIView {
|
|
let logotype = ConnectionLogo()
|
|
self.connectionImageInput = logotype
|
|
logotype.isHidden = true
|
|
logotype.translatesAutoresizingMaskIntoConstraints = false
|
|
self.addSubview(logotype)
|
|
|
|
return logotype
|
|
}
|
|
|
|
private func initializeTitle() -> UILabel {
|
|
let label = UILabel()
|
|
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.font = Constants.titleFont
|
|
label.textAlignment = .center
|
|
label.numberOfLines = 0
|
|
self.addSubview(label)
|
|
|
|
return label
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
@objc
|
|
private func onClick() {
|
|
self.output.connectionClicked()
|
|
}
|
|
|
|
// MARK: - ConnectionViewInput
|
|
|
|
func state(text: String, colorState: ConnectionViewColorState, allowClick: Bool) {
|
|
|
|
self.isUserInteractionEnabled = allowClick
|
|
|
|
self.connectionImage?.isHidden = false
|
|
|
|
self.titleLabel?.text = text.uppercased()
|
|
|
|
self.connectionImageInput?.update(state: colorState)
|
|
|
|
let color: UIColor
|
|
switch colorState {
|
|
case .connected: color = Constants.colorConnected
|
|
case .connecting, .disconnecting: color = Constants.colorProgress
|
|
case .disconnected: color = Constants.colorDisconnected
|
|
case .error: color = Constants.colorError
|
|
}
|
|
self.titleLabel?.textColor = color
|
|
}
|
|
}
|