|
1 | 1 | import UIKit |
2 | 2 |
|
3 | | -final class MessageViewController: UIViewController { |
| 3 | +public enum MessageStyle { |
| 4 | + case initial |
| 5 | + case loading |
| 6 | + case error |
| 7 | +} |
| 8 | + |
| 9 | +public final class MessageViewController: UIViewController { |
| 10 | + // Blur effect view. |
| 11 | + private lazy var blurView: UIVisualEffectView = .init(effect: UIBlurEffect(style: .extraLight)) |
| 12 | + /// Text label. |
| 13 | + public private(set) lazy var textLabel: UILabel = .init() |
| 14 | + /// Info image view. |
| 15 | + public private(set) lazy var imageView: UIImageView = .init() |
| 16 | + /// Border view. |
| 17 | + public private(set) lazy var borderView: UIView = .init() |
| 18 | + |
| 19 | + private lazy var collapsedConstraints: [NSLayoutConstraint] = self.makeCollapsedConstraints() |
| 20 | + private lazy var expandedConstraints: [NSLayoutConstraint] = self.makeExpandedConstraints() |
| 21 | + |
| 22 | + var state: State = .scanning { |
| 23 | + didSet { |
| 24 | + handleStateUpdate() |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - View lifecycle |
| 29 | + |
| 30 | + public override func viewDidLoad() { |
| 31 | + super.viewDidLoad() |
| 32 | + view.addSubview(blurView) |
| 33 | + blurView.contentView.addSubviews(textLabel, imageView, borderView) |
| 34 | + setupSubviews() |
| 35 | + handleStateUpdate() |
| 36 | + } |
| 37 | + |
| 38 | + public override func viewDidLayoutSubviews() { |
| 39 | + super.viewDidLayoutSubviews() |
| 40 | + blurView.frame = view.bounds |
| 41 | + } |
| 42 | + |
| 43 | + public override func viewWillLayoutSubviews() { |
| 44 | + super.viewWillLayoutSubviews() |
| 45 | + } |
| 46 | + |
| 47 | + // MARK: - Subviews |
| 48 | + |
| 49 | + private func setupSubviews() { |
| 50 | + textLabel.translatesAutoresizingMaskIntoConstraints = false |
| 51 | + textLabel.textColor = .black |
| 52 | + textLabel.numberOfLines = 3 |
| 53 | + |
| 54 | + imageView.translatesAutoresizingMaskIntoConstraints = false |
| 55 | + imageView.image = imageNamed("info").withRenderingMode(.alwaysTemplate) |
| 56 | + imageView.tintColor = .black |
| 57 | + |
| 58 | + borderView.translatesAutoresizingMaskIntoConstraints = false |
| 59 | + borderView.backgroundColor = .clear |
| 60 | + borderView.layer.borderWidth = 2 |
| 61 | + borderView.layer.cornerRadius = 10 |
| 62 | + borderView.layer.borderColor = UIColor.black.cgColor |
| 63 | + } |
| 64 | + |
| 65 | + private func handleStateUpdate() { |
| 66 | + borderView.isHidden = true |
| 67 | + borderView.layer.removeAllAnimations() |
| 68 | + |
| 69 | + switch state { |
| 70 | + case .scanning, .unauthorized: |
| 71 | + textLabel.text = state == .scanning ? Info.text : Info.settingsText |
| 72 | + textLabel.textColor = .black |
| 73 | + textLabel.font = UIFont.boldSystemFont(ofSize: 14) |
| 74 | + textLabel.numberOfLines = 3 |
| 75 | + textLabel.textAlignment = .left |
| 76 | + imageView.tintColor = .black |
| 77 | + case .processing: |
| 78 | + textLabel.text = Info.loadingText |
| 79 | + textLabel.textColor = .black |
| 80 | + textLabel.font = UIFont.boldSystemFont(ofSize: 16) |
| 81 | + textLabel.numberOfLines = 10 |
| 82 | + textLabel.textAlignment = .center |
| 83 | + borderView.isHidden = false |
| 84 | + imageView.tintColor = .black |
| 85 | + case .notFound: |
| 86 | + textLabel.text = Info.notFoundText |
| 87 | + textLabel.textColor = .black |
| 88 | + textLabel.font = UIFont.boldSystemFont(ofSize: 16) |
| 89 | + textLabel.numberOfLines = 10 |
| 90 | + textLabel.textAlignment = .center |
| 91 | + imageView.tintColor = .red |
| 92 | + } |
| 93 | + |
| 94 | + if state == .scanning || state == .unauthorized { |
| 95 | + expandedConstraints.forEach({ $0.isActive = false }) |
| 96 | + collapsedConstraints.forEach({ $0.isActive = true }) |
| 97 | + } else { |
| 98 | + collapsedConstraints.forEach({ $0.isActive = false }) |
| 99 | + expandedConstraints.forEach({ $0.isActive = true }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // MARK: - Animations |
| 104 | + |
| 105 | + /** |
| 106 | + Animates blur and border view. |
| 107 | + */ |
| 108 | + func animateLoading() { |
| 109 | + animate(blurStyle: .light) |
| 110 | + animate(borderViewAngle: CGFloat(Double.pi/2)) |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + Animates blur to make pulsating effect. |
| 115 | + |
| 116 | + - Parameter style: The current blur style. |
| 117 | + */ |
| 118 | + private func animate(blurStyle: UIBlurEffectStyle) { |
| 119 | + guard state == .processing else { return } |
| 120 | + |
| 121 | + UIView.animate( |
| 122 | + withDuration: 2.0, |
| 123 | + delay: 0.5, |
| 124 | + options: [.beginFromCurrentState], |
| 125 | + animations: ({ [weak self] in |
| 126 | + self?.blurView.effect = UIBlurEffect(style: blurStyle) |
| 127 | + }), |
| 128 | + completion: ({ [weak self] _ in |
| 129 | + self?.animate(blurStyle: blurStyle == .light ? .extraLight : .light) |
| 130 | + })) |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + Animates border view with a given angle. |
| 135 | + |
| 136 | + - Parameter angle: Rotation angle. |
| 137 | + */ |
| 138 | + private func animate(borderViewAngle: CGFloat) { |
| 139 | + guard state == .processing else { |
| 140 | + borderView.transform = .identity |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + UIView.animate( |
| 145 | + withDuration: 0.8, |
| 146 | + delay: 0.5, |
| 147 | + usingSpringWithDamping: 0.6, |
| 148 | + initialSpringVelocity: 1.0, |
| 149 | + options: [.beginFromCurrentState], |
| 150 | + animations: ({ [weak self] in |
| 151 | + self?.borderView.transform = CGAffineTransform(rotationAngle: borderViewAngle) |
| 152 | + }), |
| 153 | + completion: ({ [weak self] _ in |
| 154 | + self?.animate(borderViewAngle: borderViewAngle + CGFloat(Double.pi / 2)) |
| 155 | + })) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +extension MessageViewController { |
| 160 | + private func makeExpandedConstraints() -> [NSLayoutConstraint] { |
| 161 | + let padding: CGFloat = 10 |
| 162 | + let borderSize: CGFloat = 51 |
| 163 | + |
| 164 | + return [ |
| 165 | + imageView.centerYAnchor.constraint(equalTo: blurView.centerYAnchor, constant: -60), |
| 166 | + imageView.centerXAnchor.constraint(equalTo: blurView.centerXAnchor), |
| 167 | + imageView.widthAnchor.constraint(equalToConstant: 30), |
| 168 | + imageView.heightAnchor.constraint(equalToConstant: 27), |
| 169 | + |
| 170 | + textLabel.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 14), |
| 171 | + textLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding), |
| 172 | + textLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding), |
| 173 | + |
| 174 | + borderView.topAnchor.constraint(equalTo: imageView.topAnchor, constant: -12), |
| 175 | + borderView.centerXAnchor.constraint(equalTo: blurView.centerXAnchor), |
| 176 | + borderView.widthAnchor.constraint(equalToConstant: borderSize), |
| 177 | + borderView.heightAnchor.constraint(equalToConstant: borderSize) |
| 178 | + ] |
| 179 | + } |
| 180 | + |
| 181 | + private func makeCollapsedConstraints() -> [NSLayoutConstraint] { |
| 182 | + let padding: CGFloat = 10 |
| 183 | + var constraints = [ |
| 184 | + imageView.topAnchor.constraint(equalTo: blurView.topAnchor, constant: 18), |
| 185 | + imageView.widthAnchor.constraint(equalToConstant: 30), |
| 186 | + imageView.heightAnchor.constraint(equalToConstant: 27), |
| 187 | + |
| 188 | + textLabel.topAnchor.constraint(equalTo: imageView.topAnchor, constant: -3), |
| 189 | + textLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10) |
| 190 | + ] |
| 191 | + |
| 192 | + if #available(iOS 11.0, *) { |
| 193 | + constraints += [ |
| 194 | + imageView.leadingAnchor.constraint( |
| 195 | + equalTo: view.safeAreaLayoutGuide.leadingAnchor, |
| 196 | + constant: padding |
| 197 | + ), |
| 198 | + textLabel.trailingAnchor.constraint( |
| 199 | + equalTo: view.safeAreaLayoutGuide.trailingAnchor, |
| 200 | + constant: -padding |
| 201 | + ) |
| 202 | + ] |
| 203 | + } else { |
| 204 | + constraints += [ |
| 205 | + imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding), |
| 206 | + textLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding) |
| 207 | + ] |
| 208 | + } |
4 | 209 |
|
| 210 | + return constraints |
| 211 | + } |
5 | 212 | } |
0 commit comments