Skip to content

Commit 94398f6

Browse files
committed
Add toast
1 parent 118d56a commit 94398f6

File tree

5 files changed

+140
-87
lines changed

5 files changed

+140
-87
lines changed

Copilot for Xcode/App.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import Client
22
import SwiftUI
33
import UpdateChecker
44
import XPCShared
5+
import HostApp
56

67
@main
78
struct CopilotForXcodeApp: App {
89
var body: some Scene {
910
WindowGroup {
10-
ContentView()
11-
.frame(minWidth: 500, minHeight: 700)
12-
.preferredColorScheme(.dark)
11+
TabContainer()
12+
.frame(minWidth: 800, minHeight: 600)
1313
.onAppear {
1414
UserDefaults.setupDefaultSettings()
1515
}
@@ -20,14 +20,3 @@ struct CopilotForXcodeApp: App {
2020
}
2121

2222
var isPreview: Bool { ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" }
23-
24-
struct UpdateCheckerKey: EnvironmentKey {
25-
static var defaultValue: UpdateChecker = .init(hostBundle: nil)
26-
}
27-
28-
extension EnvironmentValues {
29-
var updateChecker: UpdateChecker {
30-
get { self[UpdateCheckerKey.self] }
31-
set { self[UpdateCheckerKey.self] = newValue }
32-
}
33-
}

Core/Sources/HostApp/AccountSettings/CopilotView.swift

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ struct CopilotView: View {
1515
}
1616

1717
@Environment(\.openURL) var openURL
18+
@Environment(\.toast) var toast
1819
@StateObject var settings = Settings()
1920

2021
@State var copilotStatus: GitHubCopilotAccountStatus?
21-
@State var message: String?
2222
@State var userCode: String?
2323
@State var version: String?
2424
@State var isRunningAction: Bool = false
@@ -47,16 +47,18 @@ struct CopilotView: View {
4747
Text("Run Node with")
4848
}
4949

50-
Text(
51-
"You may have to restart the helper app to apply the changes. To do so, simply close the helper app by clicking on the menu bar icon that looks like a steer wheel, it will automatically restart as needed."
52-
)
53-
.foregroundColor(.secondary)
50+
VStack { // workaround a layout issue of SwiftUI
51+
Text(
52+
"You may have to restart the helper app to apply the changes. To do so, simply close the helper app by clicking on the menu bar icon that looks like a steer wheel, it will automatically restart as needed."
53+
)
54+
.foregroundColor(.secondary)
55+
}
5456
}
5557

5658
VStack(alignment: .leading) {
5759
Text("Copilot Version: \(version ?? "Loading..")")
5860
Text("Status: \(copilotStatus?.description ?? "Loading..")")
59-
61+
6062
HStack(alignment: .center) {
6163
Button("Refresh") { checkStatus() }
6264
if copilotStatus == .notSignedIn {
@@ -92,17 +94,6 @@ struct CopilotView: View {
9294
}
9395
}
9496
Spacer()
95-
}.overlay(alignment: .topTrailing) {
96-
if let message {
97-
Text(message)
98-
.padding(.horizontal, 4)
99-
.padding(.vertical, 2)
100-
.background(
101-
RoundedRectangle(cornerRadius: 4)
102-
.fill(Color.red)
103-
)
104-
.frame(maxWidth: 200, alignment: .topTrailing)
105-
}
10697
}.onAppear {
10798
if isPreview { return }
10899
checkStatus()
@@ -118,8 +109,17 @@ struct CopilotView: View {
118109
copilotStatus = try await service.checkStatus()
119110
version = try await service.getVersion()
120111
isRunningAction = false
112+
113+
if copilotStatus != .ok {
114+
toast(
115+
Text(
116+
"GitHub Copilot status is not \"ok\". Please check if you have a valid GitHub Copilot subscription."
117+
),
118+
.error
119+
)
120+
}
121121
} catch {
122-
message = error.localizedDescription
122+
toast(Text(error.localizedDescription), .error)
123123
}
124124
}
125125
}
@@ -133,17 +133,17 @@ struct CopilotView: View {
133133
let (uri, userCode) = try await service.signInInitiate()
134134
self.userCode = userCode
135135
guard let url = URL(string: uri) else {
136-
message = "Verification URI is incorrect."
136+
toast(Text("Verification URI is incorrect."), .error)
137137
return
138138
}
139139
let pasteboard = NSPasteboard.general
140140
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
141141
pasteboard.setString(userCode, forType: NSPasteboard.PasteboardType.string)
142-
message = "Usercode \(userCode) already copied!"
142+
toast(Text("Usercode \(userCode) already copied!"), .info)
143143
openURL(url)
144144
isUserCodeCopiedAlertPresented = true
145145
} catch {
146-
message = error.localizedDescription
146+
toast(Text(error.localizedDescription), .error)
147147
}
148148
}
149149
}
@@ -155,14 +155,14 @@ struct CopilotView: View {
155155
do {
156156
let service = try getService()
157157
guard let userCode else {
158-
message = "Usercode is empty."
158+
toast(Text("Usercode is empty."), .error)
159159
return
160160
}
161161
let (username, status) = try await service.signInConfirm(userCode: userCode)
162162
self.settings.username = username
163163
copilotStatus = status
164164
} catch {
165-
message = error.localizedDescription
165+
toast(Text(error.localizedDescription), .error)
166166
}
167167
}
168168
}
@@ -175,7 +175,7 @@ struct CopilotView: View {
175175
let service = try getService()
176176
copilotStatus = try await service.signOut()
177177
} catch {
178-
message = error.localizedDescription
178+
toast(Text(error.localizedDescription), .error)
179179
}
180180
}
181181
}
@@ -200,12 +200,6 @@ struct CopilotView_Previews: PreviewProvider {
200200
static var previews: some View {
201201
VStack(alignment: .leading, spacing: 8) {
202202
CopilotView(copilotStatus: .notSignedIn, version: "1.0.0")
203-
204-
CopilotView(
205-
copilotStatus: .alreadySignedIn,
206-
message: "Error"
207-
)
208-
209203
CopilotView(copilotStatus: .alreadySignedIn, isRunningAction: true)
210204
}
211205
.frame(height: 800)

Core/Sources/HostApp/CustomCommandView.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ struct CustomCommandView: View {
128128
}
129129

130130
struct EditCustomCommandView: View {
131+
@Environment(\.toast) var toast
131132
@Binding var editingCommand: CustomCommandView.EditingCommand?
132133
var settings: CustomCommandView.Settings
133134
let originalName: String
@@ -137,7 +138,6 @@ struct EditCustomCommandView: View {
137138
@State var prompt: String
138139
@State var systemPrompt: String
139140
@State var continuousMode: Bool
140-
@State var errorMessage: String?
141141

142142
enum CommandType: Int, CaseIterable {
143143
case chatWithSelection
@@ -220,10 +220,6 @@ struct EditCustomCommandView: View {
220220
)
221221
.foregroundStyle(.secondary)
222222

223-
if let errorMessage {
224-
Text(errorMessage).foregroundColor(.red)
225-
}
226-
227223
HStack {
228224
Spacer()
229225
Button("Cancel") {
@@ -255,11 +251,11 @@ struct EditCustomCommandView: View {
255251
if editingCommand?.isNew ?? true {
256252
Button("Add") {
257253
guard !settings.illegalNames.contains(newCommand.name) else {
258-
errorMessage = "Command name is illegal."
254+
toast(Text("Command name is illegal."), .error)
259255
return
260256
}
261257
guard !newCommand.name.isEmpty else {
262-
errorMessage = "Command name cannot be empty."
258+
toast(Text("Command name cannot be empty."), .error)
263259
return
264260
}
265261
settings.customCommands.append(newCommand)
@@ -270,11 +266,11 @@ struct EditCustomCommandView: View {
270266
guard !settings.illegalNames.contains(newCommand.name)
271267
|| newCommand.name == originalName
272268
else {
273-
errorMessage = "Command name is illegal."
269+
toast(Text("Command name is illegal."), .error)
274270
return
275271
}
276272
guard !newCommand.name.isEmpty else {
277-
errorMessage = "Command name cannot be empty."
273+
toast(Text("Command name cannot be empty."), .error)
278274
return
279275
}
280276

Core/Sources/HostApp/GeneralView.swift

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,17 @@ import Preferences
44
import SwiftUI
55

66
struct GeneralView: View {
7-
@State var errorMessage: String?
8-
97
var body: some View {
108
ScrollView {
119
VStack(alignment: .leading, spacing: 0) {
1210
AppInfoView()
1311
Divider()
14-
ExtensionServiceView(errorMessage: $errorMessage)
12+
ExtensionServiceView()
1513
Divider()
16-
LaunchAgentView(errorMessage: $errorMessage)
14+
LaunchAgentView()
1715
Divider()
1816
GeneralSettingsView()
1917
}
20-
}.safeAreaInset(edge: .bottom) {
21-
if let errorMessage {
22-
Text(errorMessage)
23-
.foregroundColor(.white)
24-
.padding()
25-
.background(Color.red, in: RoundedRectangle(cornerRadius: 8))
26-
.padding()
27-
.shadow(radius: 10)
28-
}
2918
}
3019
}
3120
}
@@ -81,7 +70,7 @@ struct AppInfoView: View {
8170
}
8271

8372
struct ExtensionServiceView: View {
84-
@Binding var errorMessage: String?
73+
@Environment(\.toast) var toast
8574
@State var xpcServiceVersion: String?
8675
@State var accessibilityPermissionGranted: Bool?
8776
@State var isRunningAction = false
@@ -131,14 +120,14 @@ struct ExtensionServiceView: View {
131120
let service = try getService()
132121
xpcServiceVersion = try await service.getXPCServiceVersion().version
133122
} catch {
134-
errorMessage = error.localizedDescription
123+
toast(Text(error.localizedDescription), .error)
135124
}
136125
}
137126
}
138127
}
139128

140129
struct LaunchAgentView: View {
141-
@Binding var errorMessage: String?
130+
@Environment(\.toast) var toast
142131
@State var isDidRemoveLaunchAgentAlertPresented = false
143132
@State var isDidSetupLaunchAgentAlertPresented = false
144133
@State var isDidRestartLaunchAgentAlertPresented = false
@@ -152,7 +141,7 @@ struct LaunchAgentView: View {
152141
try await LaunchAgentManager().setupLaunchAgent()
153142
isDidSetupLaunchAgentAlertPresented = true
154143
} catch {
155-
errorMessage = error.localizedDescription
144+
toast(Text(error.localizedDescription), .error)
156145
}
157146
}
158147
}) {
@@ -174,7 +163,7 @@ struct LaunchAgentView: View {
174163
try await LaunchAgentManager().removeLaunchAgent()
175164
isDidRemoveLaunchAgentAlertPresented = true
176165
} catch {
177-
errorMessage = error.localizedDescription
166+
toast(Text(error.localizedDescription), .error)
178167
}
179168
}
180169
}) {
@@ -193,7 +182,7 @@ struct LaunchAgentView: View {
193182
try await LaunchAgentManager().reloadLaunchAgent()
194183
isDidRestartLaunchAgentAlertPresented = true
195184
} catch {
196-
errorMessage = error.localizedDescription
185+
toast(Text(error.localizedDescription), .error)
197186
}
198187
}
199188
}) {
@@ -278,9 +267,3 @@ struct GeneralView_Previews: PreviewProvider {
278267
}
279268
}
280269

281-
struct GeneralView_ErrorMessage_Previews: PreviewProvider {
282-
static var previews: some View {
283-
GeneralView(errorMessage: "Error Message")
284-
}
285-
}
286-

0 commit comments

Comments
 (0)