-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathCircularWidgetFeature.swift
More file actions
83 lines (70 loc) · 2.86 KB
/
CircularWidgetFeature.swift
File metadata and controls
83 lines (70 loc) · 2.86 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
import ActiveApplicationMonitor
import ComposableArchitecture
import Preferences
import SuggestionBasic
import SwiftUI
@Reducer
public struct CircularWidgetFeature {
public struct IsProcessingCounter: Equatable {
var expirationDate: TimeInterval
}
@ObservableState
public struct State: Equatable {
var isProcessingCounters = [IsProcessingCounter]()
var isProcessing: Bool
var isDisplayingContent: Bool
var isContentEmpty: Bool
var isChatPanelDetached: Bool
var isChatOpen: Bool
}
public enum Action: Equatable {
case widgetClicked
case detachChatPanelToggleClicked
case openChatButtonClicked
case runCustomCommandButtonClicked(CustomCommand)
case markIsProcessing
case endIsProcessing
case _forceEndIsProcessing
}
struct CancelAutoEndIsProcessKey: Hashable {}
@Dependency(\.suggestionWidgetControllerDependency) var suggestionWidgetControllerDependency
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .detachChatPanelToggleClicked:
return .none // handled elsewhere
case .openChatButtonClicked:
return .run { _ in
suggestionWidgetControllerDependency.onOpenChatClicked()
}
case let .runCustomCommandButtonClicked(command):
return .run { _ in
suggestionWidgetControllerDependency.onCustomCommandClicked(command)
}
case .widgetClicked:
return .none // handled elsewhere
case .markIsProcessing:
let deadline = Date().timeIntervalSince1970 + 20
state.isProcessingCounters.append(IsProcessingCounter(expirationDate: deadline))
state.isProcessing = true
return .run { send in
try await Task.sleep(nanoseconds: 20 * 1_000_000_000)
try Task.checkCancellation()
await send(._forceEndIsProcessing)
}.cancellable(id: CancelAutoEndIsProcessKey(), cancelInFlight: true)
case .endIsProcessing:
if !state.isProcessingCounters.isEmpty {
state.isProcessingCounters.removeFirst()
}
state.isProcessingCounters
.removeAll(where: { $0.expirationDate < Date().timeIntervalSince1970 })
state.isProcessing = !state.isProcessingCounters.isEmpty
return .none
case ._forceEndIsProcessing:
state.isProcessingCounters.removeAll()
state.isProcessing = false
return .none
}
}
}
}