Newer
Older
import UIKit
import Combine
import DependencyInjection
public final class RootViewController: UIViewController {
@Dependency var barStylist: StatusBarStylist
@Dependency var toastDispatcher: ToastController
let content: UIViewController?
var cancellables = Set<AnyCancellable>()
var toastTimer: Timer?
let toastTopPadding: CGFloat = 10
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
var topToastConstraint: NSLayoutConstraint?
public init(_ content: UIViewController?) {
self.content = content
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) { nil }
public override var preferredStatusBarStyle: UIStatusBarStyle {
barStylist.styleSubject.value
}
public override func viewDidLoad() {
super.viewDidLoad()
if let content {
addChild(content)
view.addSubview(content.view)
content.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
content.view.frame = view.bounds
content.didMove(toParent: self)
} else {
view.isUserInteractionEnabled = false
}
barStylist
.styleSubject
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
UIView.animate(withDuration: 0.2) {
self?.setNeedsStatusBarAppearanceUpdate()
}
}.store(in: &cancellables)
.receive(on: DispatchQueue.main)
.sink { [unowned self] model in
let toastView = ToastView(model: model)
add(toastView: toastView)
present(toastView: toastView)
}.store(in: &cancellables)
hudDispatcher
.modelPublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] model in
guard model != nil else {
// REMOVE FROM SUPERVIEW
return
}
// ADD TO SUPERVIEW
}.store(in: &cancellables)
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
161
162
163
164
@objc private func didPanToast(_ sender: UIPanGestureRecognizer) {
guard let toastView = sender.view else { return }
switch sender.state {
case .began, .changed:
toastTimer?.invalidate()
let padding = toastTopPadding + min(0, sender.translation(in: view).y)
topToastConstraint?.constant = padding
case .cancelled, .ended, .failed:
let halfFrameHeight = -0.5 * toastView.frame.height
let verticalTranslation = sender.translation(in: toastView).y
let didSwipeAboveHalf = verticalTranslation < halfFrameHeight
if didSwipeAboveHalf {
dismiss(toastView: toastView)
} else {
present(toastView: toastView)
}
case .possible:
break
@unknown default:
break
}
}
private func dismiss(toastView: UIView) {
toastView.isUserInteractionEnabled = false
topToastConstraint?.constant = -(toastView.frame.height + view.safeAreaLayoutGuide.layoutFrame.minY)
topToastConstraint = nil
UIView.animate(withDuration: 0.25) {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
} completion: { _ in
toastView.isUserInteractionEnabled = true
toastView.removeFromSuperview()
self.toastDispatcher.dismissCurrentToast()
}
}
private func add(toastView: UIView) {
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanToast(_:)))
toastView.addGestureRecognizer(gestureRecognizer)
toastView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toastView)
NSLayoutConstraint.activate([
toastView.heightAnchor.constraint(equalToConstant: 78),
toastView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 20),
toastView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -20)
])
topToastConstraint = toastView.topAnchor.constraint(
equalTo: view.safeAreaLayoutGuide.topAnchor,
constant: -(toastView.frame.height + view.safeAreaLayoutGuide.layoutFrame.height)
)
topToastConstraint?.isActive = true
view.setNeedsLayout()
view.layoutIfNeeded()
}
private func present(toastView: UIView) {
toastView.isUserInteractionEnabled = false
topToastConstraint?.constant = toastTopPadding
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0.5,
options: .curveEaseInOut
) {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
} completion: { _ in
toastView.isUserInteractionEnabled = true
self.toastTimer?.invalidate()
self.toastTimer = Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { [weak self] _ in
guard let self = self else { return }
self.dismiss(toastView: toastView)
}
}
}
}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// MARK: - HUD
extension RootViewController {
// private func showWindow() {
// if let animation = animation {
// window?.addSubview(animation)
// animation.setColor(.white)
// animation.snp.makeConstraints { $0.center.equalToSuperview() }
// }
//
// if let titleLabel = titleLabel {
// window?.addSubview(titleLabel)
// titleLabel.textAlignment = .center
// titleLabel.numberOfLines = 0
// titleLabel.snp.makeConstraints { make in
// make.left.equalToSuperview().offset(18)
// make.center.equalToSuperview().offset(50)
// make.right.equalToSuperview().offset(-18)
// }
// }
//
// if let actionButton = actionButton {
// window?.addSubview(actionButton)
// actionButton.snp.makeConstraints {
// $0.left.equalToSuperview().offset(18)
// $0.right.equalToSuperview().offset(-18)
// $0.bottom.equalToSuperview().offset(-50)
// }
// }
//
// 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.actionButton = nil
// self.titleLabel = nil
// self.window = nil
// }
// }
// if statusSubject.value.isPresented == true && status.isPresented == true {
// self.errorView = nil
// self.animation = nil
// self.window = nil
// self.actionButton = nil
// self.titleLabel = nil
//
// switch status {
// case .on:
// animation = DotAnimation()
//
// case .onTitle(let text):
// animation = DotAnimation()
// titleLabel = UILabel()
// titleLabel!.text = text
//
// case .onAction(let title):
// animation = DotAnimation()
// actionButton = CapsuleButton()
// actionButton!.set(style: .seeThroughWhite, title: title)
//
// case .error(let error):
// errorView = ErrorView(with: error)
// case .none:
// break
// }
//
// showWindow()
// }
// if statusSubject.value.isPresented == false && status.isPresented == true {
// switch status {
// case .on:
// animation = DotAnimation()
//
// case .onTitle(let text):
// animation = DotAnimation()
// titleLabel = UILabel()
// titleLabel!.text = text
//
// case .onAction(let title):
// animation = DotAnimation()
// actionButton = CapsuleButton()
// actionButton!.set(style: .seeThroughWhite, title: title)
//
// case .error(let error):
// errorView = ErrorView(with: error)
// case .none:
// break
// }
//
// showWindow()
// }
// if statusSubject.value.isPresented == true && status.isPresented == false {
// hideWindow()
// }
}