forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.swift
More file actions
180 lines (157 loc) · 5.29 KB
/
Copy pathToast.swift
File metadata and controls
180 lines (157 loc) · 5.29 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
import ComposableArchitecture
import Dependencies
import Foundation
import SwiftUI
public enum ToastType {
case info
case warning
case error
}
public struct ToastKey: EnvironmentKey {
public static var defaultValue: (String, ToastType) -> Void = { _, _ in }
}
public extension EnvironmentValues {
var toast: (String, ToastType) -> Void {
get { self[ToastKey.self] }
set { self[ToastKey.self] = newValue }
}
}
public struct ToastControllerDependencyKey: DependencyKey {
public static let liveValue = ToastController(messages: [])
}
public extension DependencyValues {
var toastController: ToastController {
get { self[ToastControllerDependencyKey.self] }
set { self[ToastControllerDependencyKey.self] = newValue }
}
var toast: (String, ToastType) -> Void {
return { content, type in
toastController.toast(content: content, type: type, namespace: nil)
}
}
var namespacedToast: (String, ToastType, String) -> Void {
return {
content, type, namespace in
toastController.toast(content: content, type: type, namespace: namespace)
}
}
}
public class ToastController: ObservableObject {
public struct Message: Identifiable, Equatable {
public struct MessageButton: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.label == rhs.label
}
public var label: Text
public var action: () -> Void
public init(label: Text, action: @escaping () -> Void) {
self.label = label
self.action = action
}
}
public var namespace: String?
public var id: UUID
public var type: ToastType
public var content: Text
public var buttons: [MessageButton]
public init(
id: UUID,
type: ToastType,
namespace: String? = nil,
content: Text,
buttons: [MessageButton] = []
) {
self.namespace = namespace
self.id = id
self.type = type
self.content = content
self.buttons = buttons
}
}
@Published public var messages: [Message] = []
public init(messages: [Message]) {
self.messages = messages
}
public func toast(
content: String,
type: ToastType,
namespace: String? = nil,
buttons: [Message.MessageButton] = [],
duration: TimeInterval = 4
) {
let id = UUID()
let message = Message(
id: id,
type: type,
namespace: namespace,
content: Text(content),
buttons: buttons.map { b in
Message.MessageButton(label: b.label, action: { [weak self] in
b.action()
withAnimation(.easeInOut(duration: 0.2)) {
self?.messages.removeAll { $0.id == id }
}
})
}
)
Task { @MainActor in
withAnimation(.easeInOut(duration: 0.2)) {
messages.append(message)
messages = messages.suffix(3)
}
try await Task.sleep(nanoseconds: UInt64(duration * 1_000_000_000))
withAnimation(.easeInOut(duration: 0.2)) {
messages.removeAll { $0.id == id }
}
}
}
}
@Reducer
public struct Toast {
public typealias Message = ToastController.Message
@ObservableState
public struct State: Equatable {
var isObservingToastController = false
public var messages: [Message] = []
public init(messages: [Message] = []) {
self.messages = messages
}
}
public enum Action: Equatable {
case start
case updateMessages([Message])
case toast(String, ToastType, String?)
}
@Dependency(\.toastController) var toastController
struct CancelID: Hashable {}
public init() {}
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .start:
guard !state.isObservingToastController else { return .none }
state.isObservingToastController = true
return .run { send in
let stream = AsyncStream<[Message]> { continuation in
let cancellable = toastController.$messages.sink { newValue in
continuation.yield(newValue)
}
continuation.onTermination = { _ in
cancellable.cancel()
}
}
for await newValue in stream {
try Task.checkCancellation()
await send(.updateMessages(newValue), animation: .linear(duration: 0.2))
}
}.cancellable(id: CancelID(), cancelInFlight: true)
case let .updateMessages(messages):
state.messages = messages
return .none
case let .toast(content, type, namespace):
toastController.toast(content: content, type: type, namespace: namespace)
return .none
}
}
}
}