Newer
Older
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import UIKit
import Theme
import Shared
import Combine
import SnapKit
private enum Constants {
static let title = Localized.Hud.Error.title
static let action = Localized.Hud.Error.action
}
public enum HUDStatus: Equatable {
case on
case none
case error(HUDError)
var isPresented: Bool {
switch self {
case .none:
return false
case .on, .error:
return true
}
}
}
public struct HUDError: Equatable {
var title: String
var content: String
var buttonTitle: String
var dismissable: Bool
public init(
content: String,
title: String? = nil,
buttonTitle: String? = nil,
dismissable: Bool = true
) {
self.content = content
self.title = title ?? Constants.title
self.buttonTitle = buttonTitle ?? Constants.action
self.dismissable = dismissable
}
public init(with error: Error) {
self.title = Constants.title
self.buttonTitle = Constants.action
self.content = error.localizedDescription
self.dismissable = true
}
}
public protocol HUDType {
func update(with status: HUDStatus)
}
public final class HUD: HUDType {
// MARK: UI
private(set) var window: UIWindow?
private(set) var errorView: ErrorView?
private(set) var animation: DotAnimation?
// MARK: Properties
private var cancellables = Set<AnyCancellable>()
private var status: HUDStatus = .none {
didSet {
if oldValue.isPresented == true && status.isPresented == true {
self.errorView = nil
self.animation = nil
self.window = nil
switch status {
case .on:
animation = DotAnimation()
case .error(let error):
errorView = ErrorView(with: error)
case .none:
break
}
showWindow()
}
if oldValue.isPresented == false && status.isPresented == true {
switch status {
case .on:
animation = DotAnimation()
case .error(let error):
errorView = ErrorView(with: error)
case .none:
break
}
showWindow()
}
if oldValue.isPresented == true && status.isPresented == false {
hideWindow()
}
}
}
// MARK: Lifecycle
public init() {}
// MARK: Public
public func update(with status: HUDStatus) {
self.status = status
}
// MARK: Private
private func showWindow() {
window = Window()
window?.backgroundColor = UIColor.black.withAlphaComponent(0.5)
window?.rootViewController = StatusBarViewController(nil)
if let animation = animation {
window?.addSubview(animation)
animation.setColor(.white)
animation.snp.makeConstraints { $0.center.equalToSuperview() }
}
if let errorView = errorView {
window?.addSubview(errorView)
errorView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(18)
make.center.equalToSuperview()
make.right.equalToSuperview().offset(-18)
}
errorView.button
.publisher(for: .touchUpInside)
.receive(on: DispatchQueue.main)
.sink { [unowned self] in hideWindow() }
.store(in: &cancellables)
}
window?.alpha = 0.0
window?.makeKeyAndVisible()
UIView.animate(withDuration: 0.3) { self.window?.alpha = 1.0 }
}
private func hideWindow() {
UIView.animate(withDuration: 0.3) {
self.window?.alpha = 0.0
} completion: { _ in
self.cancellables.removeAll()
self.errorView = nil
self.animation = nil
self.window = nil
}
}
}