@@ -6,6 +6,8 @@ import SuggestionModel
66import SwiftUI
77
88struct CopilotView : View {
9+ static var copilotAuthService : GitHubCopilotAuthServiceType ?
10+
911 class Settings : ObservableObject {
1012 @AppStorage ( \. nodePath) var nodePath : String
1113 @AppStorage ( \. runNodeWith) var runNodeWith
@@ -18,11 +20,18 @@ struct CopilotView: View {
1820 @Environment ( \. toast) var toast
1921 @StateObject var settings = Settings ( )
2022
21- @State var copilotStatus : GitHubCopilotAccountStatus ?
23+ @State var status : GitHubCopilotAccountStatus ?
2224 @State var userCode : String ?
2325 @State var version : String ?
2426 @State var isRunningAction : Bool = false
2527 @State var isUserCodeCopiedAlertPresented = false
28+
29+ func getGitHubCopilotAuthService( ) throws -> GitHubCopilotAuthServiceType {
30+ if let service = Self . copilotAuthService { return service }
31+ let service = try GitHubCopilotAuthService ( )
32+ Self . copilotAuthService = service
33+ return service
34+ }
2635
2736 var body : some View {
2837 HStack {
@@ -56,12 +65,12 @@ struct CopilotView: View {
5665 }
5766
5867 VStack ( alignment: . leading) {
59- Text ( " Copilot Version: \( version ?? " Loading.. " ) " )
60- Text ( " Status: \( copilotStatus ? . description ?? " Loading.. " ) " )
68+ Text ( " Language Server Version: \( version ?? " Loading.. " ) " )
69+ Text ( " Status: \( status ? . description ?? " Loading.. " ) " )
6170
6271 HStack ( alignment: . center) {
6372 Button ( " Refresh " ) { checkStatus ( ) }
64- if copilotStatus == . notSignedIn {
73+ if status == . notSignedIn {
6574 Button ( " Sign In " ) { signIn ( ) }
6675 . alert ( isPresented: $isUserCodeCopiedAlertPresented) {
6776 Alert (
@@ -74,8 +83,8 @@ struct CopilotView: View {
7483 }
7584 Button ( " Confirm Sign-in " ) { confirmSignIn ( ) }
7685 }
77- if copilotStatus == . ok || copilotStatus == . alreadySignedIn ||
78- copilotStatus == . notAuthorized
86+ if status == . ok || status == . alreadySignedIn ||
87+ status == . notAuthorized
7988 {
8089 Button ( " Sign Out " ) { signOut ( ) }
8190 }
@@ -105,12 +114,12 @@ struct CopilotView: View {
105114 isRunningAction = true
106115 defer { isRunningAction = false }
107116 do {
108- let service = try getService ( )
109- copilotStatus = try await service. checkStatus ( )
110- version = try await service. getVersion ( )
117+ let service = try getGitHubCopilotAuthService ( )
118+ status = try await service. checkStatus ( )
119+ version = try await service. version ( )
111120 isRunningAction = false
112121
113- if copilotStatus != . ok {
122+ if status != . ok {
114123 toast (
115124 Text (
116125 " GitHub Copilot status is not \" ok \" . Please check if you have a valid GitHub Copilot subscription. "
@@ -129,7 +138,7 @@ struct CopilotView: View {
129138 isRunningAction = true
130139 defer { isRunningAction = false }
131140 do {
132- let service = try getService ( )
141+ let service = try getGitHubCopilotAuthService ( )
133142 let ( uri, userCode) = try await service. signInInitiate ( )
134143 self . userCode = userCode
135144 guard let url = URL ( string: uri) else {
@@ -153,14 +162,14 @@ struct CopilotView: View {
153162 isRunningAction = true
154163 defer { isRunningAction = false }
155164 do {
156- let service = try getService ( )
165+ let service = try getGitHubCopilotAuthService ( )
157166 guard let userCode else {
158167 toast ( Text ( " Usercode is empty. " ) , . error)
159168 return
160169 }
161170 let ( username, status) = try await service. signInConfirm ( userCode: userCode)
162171 self . settings. username = username
163- copilotStatus = status
172+ self . status = status
164173 } catch {
165174 toast ( Text ( error. localizedDescription) , . error)
166175 }
@@ -172,8 +181,8 @@ struct CopilotView: View {
172181 isRunningAction = true
173182 defer { isRunningAction = false }
174183 do {
175- let service = try getService ( )
176- copilotStatus = try await service. signOut ( )
184+ let service = try getGitHubCopilotAuthService ( )
185+ status = try await service. signOut ( )
177186 } catch {
178187 toast ( Text ( error. localizedDescription) , . error)
179188 }
@@ -199,8 +208,8 @@ struct ActivityIndicatorView: NSViewRepresentable {
199208struct CopilotView_Previews : PreviewProvider {
200209 static var previews : some View {
201210 VStack ( alignment: . leading, spacing: 8 ) {
202- CopilotView ( copilotStatus : . notSignedIn, version: " 1.0.0 " )
203- CopilotView ( copilotStatus : . alreadySignedIn, isRunningAction: true )
211+ CopilotView ( status : . notSignedIn, version: " 1.0.0 " )
212+ CopilotView ( status : . alreadySignedIn, isRunningAction: true )
204213 }
205214 . frame ( height: 800 )
206215 . padding ( . all, 8 )
0 commit comments