-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.swift
More file actions
332 lines (292 loc) · 9.22 KB
/
Copy pathToast.swift
File metadata and controls
332 lines (292 loc) · 9.22 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// Toast.swift
// DevLogPresentation
//
// Created by 최윤진 on 2/10/26.
//
import SwiftUI
@MainActor
@Observable
final class ToastPresenter {
fileprivate static let presenter = ToastPresenter()
private(set) var item: ToastItem?
private init() { }
static var item: ToastItem? {
presenter.item
}
static func present(
message: String,
systemImage: String? = nil,
duration: TimeInterval = 2,
font: Font? = nil,
multilineTextAlignment: TextAlignment = .leading,
lineLimit: Int? = nil,
action: (() -> Void)? = nil,
onDismiss: (() -> Void)? = nil
) {
presenter.present(
ToastItem(
message: message,
systemImage: systemImage,
duration: duration,
font: font,
multilineTextAlignment: multilineTextAlignment,
lineLimit: lineLimit,
action: action,
onDismiss: onDismiss
)
)
}
static func reset() {
presenter.item = nil
}
private func present(_ item: ToastItem) {
dismissImmediately()
self.item = item
}
fileprivate func dismiss(itemId: UUID) {
guard let item,
item.id == itemId else { return }
self.item = nil
}
private func dismissImmediately() {
guard let item else { return }
self.item = nil
item.onDismiss?()
}
}
struct ToastItem: Identifiable {
let id = UUID()
let message: String
let systemImage: String?
let duration: TimeInterval
let font: Font?
let multilineTextAlignment: TextAlignment
let lineLimit: Int?
let action: (() -> Void)?
let onDismiss: (() -> Void)?
}
extension View {
func toastHost() -> some View {
modifier(ToastHostModifier())
}
}
private struct ToastHostModifier: ViewModifier {
@Environment(\.safeAreaInsets) private var safeAreaInsets
@State private var tabBarHeight = CGFloat.zero
private let toastPresenter = ToastPresenter.presenter
func body(content: Content) -> some View {
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onAppear {
updateTabBarHeight()
}
.onChange(of: toastPresenter.item?.id) { _, _ in
updateTabBarHeight()
}
.overlay(alignment: .bottom) {
if let item = toastPresenter.item {
ToastOverlayView(
isPresented: Binding(
get: { toastPresenter.item?.id == item.id },
set: { isPresented in
if !isPresented {
toastPresenter.dismiss(itemId: item.id)
}
}
),
duration: item.duration,
action: item.action,
onDismiss: item.onDismiss
) {
ToastItemLabel(item: item)
}
.id(item.id)
.padding(.horizontal, 12)
.padding(.bottom, toastBottomInset)
}
}
}
private var toastBottomInset: CGFloat {
max(0, tabBarHeight - safeAreaInsets.bottom)
}
@MainActor
private func updateTabBarHeight() {
let window = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap(\.windows)
.first { $0.isKeyWindow }
guard let window else {
tabBarHeight = .zero
return
}
tabBarHeight = window.rootViewController?.visibleTabBarHeight ?? .zero
}
}
private struct ToastItemLabel: View {
let item: ToastItem
var body: some View {
Group {
if let systemImage = item.systemImage {
Label(item.message, systemImage: systemImage)
} else {
Text(item.message)
}
}
.font(item.font)
.multilineTextAlignment(item.multilineTextAlignment)
.lineLimit(item.lineLimit)
}
}
private struct ToastOverlayView<Label: View>: View {
@Binding var isPresented: Bool
let duration: TimeInterval
let action: (() -> Void)?
let onDismiss: (() -> Void)?
@ViewBuilder let label: () -> Label
@State private var yOffset: CGFloat = 0
@State private var opacityValue: Double = 0
@State private var dismissWorkItem: DispatchWorkItem?
@State private var dismissCompletionWorkItem: DispatchWorkItem?
@State private var isTapped: Bool = false
@State private var isScheduled: Bool = false
var body: some View {
if isPresented {
ToastCardView(
label,
color: action == nil ? .primary : .blue
)
.offset(y: yOffset)
.opacity(opacityValue)
.onChange(of: isPresented) { _, newValue in
if newValue {
resetForNewPresentation()
presentAnimated()
scheduleDismissIfNeeded()
} else {
cleanupPresentation()
}
}
.onAppear {
presentAnimated()
scheduleDismissIfNeeded()
}
.onDisappear {
cleanupPresentation()
}
.onTapGesture {
isTapped = true
dismissAnimated()
action?()
}
.transition(.identity)
}
}
private func presentAnimated() {
guard opacityValue == 0 else { return }
withAnimation(.spring(response: 0.5, dampingFraction: 1, blendDuration: 0.0)) {
yOffset = -50
opacityValue = 1
}
}
private func resetForNewPresentation() {
dismissWorkItem?.cancel()
dismissWorkItem = nil
dismissCompletionWorkItem?.cancel()
dismissCompletionWorkItem = nil
isScheduled = false
isTapped = false
yOffset = 0
opacityValue = 0
}
private func cleanupPresentation() {
dismissWorkItem?.cancel()
dismissWorkItem = nil
dismissCompletionWorkItem?.cancel()
dismissCompletionWorkItem = nil
isScheduled = false
isTapped = false
yOffset = 0
opacityValue = 0
}
private func scheduleDismissIfNeeded() {
guard !isScheduled else { return }
isScheduled = true
let workItem = DispatchWorkItem {
dismissAnimated()
}
dismissWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + duration, execute: workItem)
}
private func dismissAnimated() {
dismissWorkItem?.cancel()
dismissWorkItem = nil
dismissCompletionWorkItem?.cancel()
withAnimation(.easeInOut(duration: 0.2)) {
yOffset = 0
opacityValue = 0
}
let workItem = DispatchWorkItem {
isPresented = false
isScheduled = false
if !isTapped {
onDismiss?()
}
isTapped = false
}
dismissCompletionWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: workItem)
}
}
private struct ToastCardView<Label: View>: View {
@ViewBuilder let label: Label
let color: Color
init(
@ViewBuilder _ label: @escaping () -> Label,
color: Color = .primary
) {
self.label = label()
self.color = color
}
var body: some View {
self.label
.foregroundStyle(color)
.padding(.vertical, 12)
.padding(.horizontal, 14)
.background {
if #available(iOS 26.0, *) {
RoundedRectangle(cornerRadius: 16, style: .continuous)
.glassEffect()
} else {
RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(.ultraThinMaterial)
}
}
.overlay {
if #unavailable(iOS 26.0) {
RoundedRectangle(cornerRadius: 16, style: .continuous)
.strokeBorder(Color(.systemGray4).opacity(0.2), lineWidth: 1)
}
}
.shadow(color: Color(.systemGray2).opacity(0.4), radius: 18, x: 0, y: 10)
}
}
@MainActor
private extension UIViewController {
var visibleTabBarHeight: CGFloat {
var topViewController = self
while let presentedViewController = topViewController.presentedViewController {
topViewController = presentedViewController
}
if let tabBarController = (topViewController as? UITabBarController) ?? topViewController.tabBarController {
return tabBarController.tabBar.isHidden ? .zero : tabBarController.tabBar.frame.height
}
for child in topViewController.children {
let childHeight = child.visibleTabBarHeight
if 0 < childHeight {
return childHeight
}
}
return .zero
}
}