-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathWarningState.swift
More file actions
53 lines (44 loc) · 1.36 KB
/
Copy pathWarningState.swift
File metadata and controls
53 lines (44 loc) · 1.36 KB
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
import Foundation
import Status
public struct WarningAction: Equatable {
public var title: String
public var url: URL
public init(title: String, url: URL) {
self.title = title
self.url = url
}
}
public struct WarningContent: Equatable {
public var message: String
public var severity: String // "warning" or "info"
public var actions: [WarningAction] // 0-2 CTAs
public init(message: String, severity: String, actions: [WarningAction] = []) {
self.message = message
self.severity = severity
self.actions = actions
}
}
public class WarningStateManager: ObservableObject {
public static let shared = WarningStateManager()
@Published public var currentWarning: WarningContent?
private init() {
DistributedNotificationCenter.default().addObserver(
forName: .authStatusDidChange,
object: nil,
queue: .main
) { [weak self] _ in
self?.dismissWarning()
}
}
public func setWarning(_ warning: WarningContent) {
DispatchQueue.main.async { [weak self] in
guard self?.currentWarning != warning else { return }
self?.currentWarning = warning
}
}
public func dismissWarning() {
DispatchQueue.main.async { [weak self] in
self?.currentWarning = nil
}
}
}