-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathStatusActivityView.swift
81 lines (69 loc) · 2.4 KB
/
StatusActivityView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// StatusActivityView.swift
// MullvadVPN
//
// Created by Andreas Lif on 2022-08-15.
// Copyright © 2022 Mullvad VPN AB. All rights reserved.
//
import Foundation
import UIKit
class StatusActivityView: UIView {
enum State {
case hidden, activity, success, failure
}
var state: State = .hidden {
didSet {
updateView()
}
}
private let statusImageView: StatusImageView = {
let imageView = StatusImageView(style: .failure)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
return imageView
}()
private let activityIndicator: SpinnerActivityIndicatorView = {
let view = SpinnerActivityIndicatorView(style: .large)
view.tintColor = .white
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
init(state: State) {
super.init(frame: .zero)
self.state = state
addSubview(statusImageView)
addSubview(activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.widthAnchor.constraint(equalTo: statusImageView.widthAnchor),
activityIndicator.heightAnchor.constraint(equalTo: statusImageView.heightAnchor),
statusImageView.topAnchor.constraint(equalTo: topAnchor),
statusImageView.bottomAnchor.constraint(equalTo: bottomAnchor),
statusImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
statusImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
activityIndicator.centerXAnchor.constraint(equalTo: centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: centerYAnchor),
])
updateView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
private func updateView() {
switch state {
case .hidden:
statusImageView.alpha = 0
activityIndicator.stopAnimating()
case .activity:
statusImageView.alpha = 0
activityIndicator.startAnimating()
case .success:
statusImageView.alpha = 1
statusImageView.style = .success
activityIndicator.stopAnimating()
case .failure:
statusImageView.alpha = 1
statusImageView.style = .failure
activityIndicator.stopAnimating()
}
}
}