-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToastSystem.swift
More file actions
223 lines (187 loc) · 5.98 KB
/
Copy pathToastSystem.swift
File metadata and controls
223 lines (187 loc) · 5.98 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
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
161
162
163
164
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
// ToastSystem.swift
// Global toast/alert notification system
import SwiftUI
import Combine
// MARK: - Toast Types
enum ToastType: Equatable {
case success
case error
case warning
case info
case autoAccept(count: Int, label: String)
var icon: String {
switch self {
case .success: return "checkmark.circle.fill"
case .error: return "xmark.circle.fill"
case .warning: return "exclamationmark.triangle.fill"
case .info: return "info.circle.fill"
case .autoAccept: return "sparkles"
}
}
var color: Color {
switch self {
case .success: return .green
case .error: return .red
case .warning: return .orange
case .info: return .blue
case .autoAccept: return .purple
}
}
}
// MARK: - Toast Message
struct ToastMessage: Identifiable, Equatable {
let id: UUID
let type: ToastType
let title: String
let message: String?
let duration: Double
init(type: ToastType, title: String, message: String? = nil, duration: Double = 3.0) {
self.id = UUID()
self.type = type
self.title = title
self.message = message
self.duration = duration
}
static func == (lhs: ToastMessage, rhs: ToastMessage) -> Bool {
lhs.id == rhs.id
}
}
// MARK: - Toast Manager (Global Singleton)
final class ToastManager: ObservableObject {
static let shared = ToastManager()
@Published var currentToast: ToastMessage?
private var toastQueue: [ToastMessage] = []
private var hideTask: Task<Void, Never>?
private init() {}
// MARK: - Public Methods (can be called from any context)
func showSuccess(_ title: String, message: String? = nil) {
let toast = ToastMessage(type: .success, title: title, message: message)
enqueue(toast)
}
func showError(_ title: String, message: String? = nil) {
let toast = ToastMessage(type: .error, title: title, message: message, duration: 4.0)
enqueue(toast)
}
func showWarning(_ title: String, message: String? = nil) {
let toast = ToastMessage(type: .warning, title: title, message: message)
enqueue(toast)
}
func showInfo(_ title: String, message: String? = nil) {
let toast = ToastMessage(type: .info, title: title, message: message)
enqueue(toast)
}
func showAutoAccept(count: Int, label: String, confidence: Float) {
let message = String(format: "Average confidence: %.0f%%", confidence * 100)
let toast = ToastMessage(
type: .autoAccept(count: count, label: label),
title: "Auto-labeled \(count) objects as '\(label)'",
message: message,
duration: 4.0
)
enqueue(toast)
}
func dismiss() {
Task { @MainActor in
hideTask?.cancel()
currentToast = nil
showNextInQueue()
}
}
// MARK: - Private Methods
private func enqueue(_ toast: ToastMessage) {
Task { @MainActor in
if currentToast == nil {
currentToast = toast
scheduleHide(after: toast.duration)
} else {
toastQueue.append(toast)
}
}
}
@MainActor
private func scheduleHide(after duration: Double) {
hideTask?.cancel()
hideTask = Task {
try? await Task.sleep(nanoseconds: UInt64(duration * 1_000_000_000))
guard !Task.isCancelled else { return }
currentToast = nil
showNextInQueue()
}
}
@MainActor
private func showNextInQueue() {
guard !toastQueue.isEmpty else { return }
let next = toastQueue.removeFirst()
currentToast = next
scheduleHide(after: next.duration)
}
}
// MARK: - Toast View
struct ToastView: View {
let toast: ToastMessage
let onDismiss: () -> Void
var body: some View {
HStack(spacing: 12) {
Image(systemName: toast.type.icon)
.font(.title2)
.foregroundColor(toast.type.color)
VStack(alignment: .leading, spacing: 2) {
Text(toast.title)
.font(.subheadline)
.fontWeight(.semibold)
.foregroundColor(.primary)
.lineLimit(2)
if let message = toast.message {
Text(message)
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(1)
}
}
Spacer()
Button {
onDismiss()
} label: {
Image(systemName: "xmark")
.font(.caption)
.foregroundColor(.secondary)
.padding(4)
}
.buttonStyle(.plain)
}
.padding(.horizontal, 16)
.padding(.vertical, 12)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color(.systemBackground))
.shadow(color: .black.opacity(0.15), radius: 10, x: 0, y: 5)
)
.padding(.horizontal, 16)
}
}
// MARK: - Toast Container Modifier
struct ToastContainerModifier: ViewModifier {
@ObservedObject private var toastManager = ToastManager.shared
func body(content: Content) -> some View {
ZStack {
content
VStack {
if let toast = toastManager.currentToast {
ToastView(toast: toast) {
toastManager.dismiss()
}
.transition(.move(edge: .top).combined(with: .opacity))
.zIndex(100)
}
Spacer()
}
.animation(.spring(response: 0.3), value: toastManager.currentToast?.id)
}
}
}
// MARK: - View Extension
extension View {
func withToasts() -> some View {
modifier(ToastContainerModifier())
}
}