Skip to content

Commit f2e7888

Browse files
committed
Add Toast to WidgetFeature
1 parent af9c688 commit f2e7888

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

Core/Sources/SuggestionWidget/FeatureReducers/WidgetFeature.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Environment
77
import Foundation
88
import Preferences
99
import SwiftUI
10+
import Toast
1011
import XcodeInspector
1112

1213
public struct WidgetFeature: ReducerProtocol {
@@ -32,6 +33,8 @@ public struct WidgetFeature: ReducerProtocol {
3233
var focusingDocumentURL: URL?
3334
public var colorScheme: ColorScheme = .light
3435

36+
public var toast = Toast.State()
37+
3538
// MARK: Panels
3639

3740
public var panelState = PanelFeature.State()
@@ -120,6 +123,7 @@ public struct WidgetFeature: ReducerProtocol {
120123
case updateWindowOpacityFinished
121124
case updateKeyWindow(WindowCanBecomeKey)
122125

126+
case toast(Toast.Action)
123127
case panel(PanelFeature.Action)
124128
case chatPanel(ChatPanelFeature.Action)
125129
case circularWidget(CircularWidgetFeature.Action)
@@ -143,6 +147,10 @@ public struct WidgetFeature: ReducerProtocol {
143147
public init() {}
144148

145149
public var body: some ReducerProtocol<State, Action> {
150+
Scope(state: \.toast, action: /Action.toast) {
151+
Toast()
152+
}
153+
146154
Scope(state: \._circularWidgetState, action: /Action.circularWidget) {
147155
CircularWidgetFeature()
148156
}
@@ -227,11 +235,14 @@ public struct WidgetFeature: ReducerProtocol {
227235
switch action {
228236
case .startup:
229237
return .merge(
230-
.run { send in await send(.observeActiveApplicationChange) },
231-
.run { send in await send(.observeCompletionPanelChange) },
232-
.run { send in await send(.observeFullscreenChange) },
233-
.run { send in await send(.observeColorSchemeChange) },
234-
.run { send in await send(.observePresentationModeChange) }
238+
.run { send in
239+
await send(.toast(.start))
240+
await send(.observeActiveApplicationChange)
241+
await send(.observeCompletionPanelChange)
242+
await send(.observeFullscreenChange)
243+
await send(.observeColorSchemeChange)
244+
await send(.observePresentationModeChange)
245+
}
235246
)
236247

237248
case .observeActiveApplicationChange:
@@ -628,6 +639,9 @@ public struct WidgetFeature: ReducerProtocol {
628639
await windows.sharedPanelWindow.makeKeyAndOrderFront(nil)
629640
}
630641
}
642+
643+
case .toast:
644+
return .none
631645

632646
case .circularWidget:
633647
return .none

Tool/Sources/Toast/Toast.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ public class ToastController: ObservableObject {
7171
public struct Toast: ReducerProtocol {
7272
public typealias Message = ToastController.Message
7373
public struct State: Equatable {
74+
var isObservingToastController = false
7475
public var messages: [Message] = []
76+
77+
public init(messages: [Message] = []) {
78+
self.messages = messages
79+
}
7580
}
7681

7782
public enum Action: Equatable {
78-
case appear
83+
case start
7984
case updateMessages([Message])
8085
case toast(String, ToastType)
8186
}
@@ -89,7 +94,9 @@ public struct Toast: ReducerProtocol {
8994
public var body: some ReducerProtocol<State, Action> {
9095
Reduce { state, action in
9196
switch action {
92-
case .appear:
97+
case .start:
98+
guard !state.isObservingToastController else { return .none }
99+
state.isObservingToastController = true
93100
return .run { send in
94101
let stream = AsyncStream<[Message]> { continuation in
95102
let cancellable = toastController.$messages.sink { newValue in

0 commit comments

Comments
 (0)