-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathCopilotConnectionView.swift
More file actions
142 lines (131 loc) · 4.8 KB
/
CopilotConnectionView.swift
File metadata and controls
142 lines (131 loc) · 4.8 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
import ComposableArchitecture
import GitHubCopilotViewModel
import SwiftUI
import Client
struct CopilotConnectionView: View {
@AppStorage("username") var username: String = ""
@Environment(\.toast) var toast
@StateObject var viewModel: GitHubCopilotViewModel
let store: StoreOf<General>
var body: some View {
WithPerceptionTracking {
VStack {
connection
.padding(.bottom, 20)
copilotResources
}
}
}
var accountStatusString: String {
switch store.xpcServiceAuthStatus.status {
case .loggedIn:
return "Active"
case .notLoggedIn:
return "Not Signed In"
case .notAuthorized:
return "No Subscription"
case .unknown:
return "Loading..."
}
}
var accountStatus: some View {
SettingsButtonRow(
title: "GitHub Account Status Permissions",
subtitle: "GitHub Account: \(accountStatusString)"
) {
if viewModel.isRunningAction || viewModel.waitingForSignIn {
ProgressView().controlSize(.small)
}
Button("Refresh Connection") {
store.send(.reloadStatus)
}
if viewModel.waitingForSignIn {
Button("Cancel") {
viewModel.cancelWaiting()
}
} else if store.xpcServiceAuthStatus.status == .notLoggedIn {
Button("Log in to GitHub") {
viewModel.signIn()
}
.alert(
viewModel.signInResponse?.userCode ?? "",
isPresented: $viewModel.isSignInAlertPresented,
presenting: viewModel.signInResponse) { _ in
Button("Cancel", role: .cancel, action: {})
Button("Copy Code and Open", action: viewModel.copyAndOpen)
} message: { response in
Text("""
Please enter the above code in the \
GitHub website to authorize your \
GitHub account with Copilot for Xcode.
\(response?.verificationURL.absoluteString ?? "")
""")
}
}
if store.xpcServiceAuthStatus.status == .loggedIn || store.xpcServiceAuthStatus.status == .notAuthorized {
Button("Log Out from GitHub") {
Task {
viewModel.signOut()
viewModel.isSignInAlertPresented = false
let service = try getService()
do {
try await service.signOutAllGitHubCopilotService()
} catch {
toast(error.localizedDescription, .error)
}
}
}
}
}
}
var connection: some View {
SettingsSection(
title: "Account Settings",
showWarning: store.xpcServiceAuthStatus.status == .notAuthorized
) {
accountStatus
Divider()
if store.xpcServiceAuthStatus.status == .notAuthorized {
SettingsLink(
url: "https://github.com/features/copilot/plans",
title: "Enable powerful AI features for free with the GitHub Copilot Free plan"
)
Divider()
}
SettingsLink(
url: "https://github.com/settings/copilot",
title: "GitHub Copilot Account Settings"
)
}
.onReceive(DistributedNotificationCenter.default().publisher(for: .authStatusDidChange)) { _ in
store.send(.reloadStatus)
}
}
var copilotResources: some View {
SettingsSection(title: "Copilot Resources") {
SettingsLink(
url: "https://docs.github.com/en/copilot",
title: "View Copilot Documentation"
)
Divider()
SettingsLink(
url: "https://github.com/orgs/community/discussions/categories/copilot",
title: "View Copilot Feedback Forum"
)
}
}
}
#Preview {
CopilotConnectionView(
viewModel: GitHubCopilotViewModel.shared,
store: .init(initialState: .init(), reducer: { General() })
)
}
#Preview("Running") {
let runningModel = GitHubCopilotViewModel.shared
runningModel.isRunningAction = true
return CopilotConnectionView(
viewModel: GitHubCopilotViewModel.shared,
store: .init(initialState: .init(), reducer: { General() })
)
}