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
124 lines (105 loc) · 3.7 KB
/
Toast.swift
File metadata and controls
124 lines (105 loc) · 3.7 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
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 { toastController.toast }
}
public class ToastController: ObservableObject {
public struct Message: Identifiable, Equatable {
public var id: UUID
public var type: ToastType
public var content: Text
public init(id: UUID, type: ToastType, content: Text) {
self.id = id
self.type = type
self.content = content
}
}
@Published public var messages: [Message] = []
public init(messages: [Message]) {
self.messages = messages
}
public func toast(content: String, type: ToastType) {
let id = UUID()
let message = Message(id: id, type: type, content: Text(content))
Task { @MainActor in
withAnimation(.easeInOut(duration: 0.2)) {
messages.append(message)
messages = messages.suffix(3)
}
try await Task.sleep(nanoseconds: 4_000_000_000)
withAnimation(.easeInOut(duration: 0.2)) {
messages.removeAll { $0.id == id }
}
}
}
}
public struct Toast: ReducerProtocol {
public typealias Message = ToastController.Message
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)
}
@Dependency(\.toastController) var toastController
struct CancelID: Hashable {}
public init() {}
public var body: some ReducerProtocol<State, Action> {
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):
toastController.toast(content: content, type: type)
return .none
}
}
}
}