-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathGitHubCopilotViewModel.swift
More file actions
134 lines (121 loc) · 4.44 KB
/
GitHubCopilotViewModel.swift
File metadata and controls
134 lines (121 loc) · 4.44 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
import GitHubCopilotService
import ComposableArchitecture
import SwiftUI
struct SignInResponse {
let userCode: String
let verificationURL: URL
}
@MainActor
class GitHubCopilotViewModel: ObservableObject {
@Dependency(\.toast) var toast
@Dependency(\.openURL) var openURL
@AppStorage("username") var username: String = ""
@Published var isRunningAction: Bool = false
@Published var status: GitHubCopilotAccountStatus?
@Published var version: String?
@Published var userCode: String?
@Published var isSignInAlertPresented = false
@Published var signInResponse: SignInResponse?
@Published var waitingForSignIn = false
static var copilotAuthService: GitHubCopilotAuthServiceType?
func getGitHubCopilotAuthService() throws -> GitHubCopilotAuthServiceType {
if let service = Self.copilotAuthService { return service }
let service = try GitHubCopilotService()
Self.copilotAuthService = service
return service
}
func signIn() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getGitHubCopilotAuthService()
let (uri, userCode) = try await service.signInInitiate()
guard let url = URL(string: uri) else {
toast("Verification URI is incorrect.", .error)
return
}
self.signInResponse = .init(userCode: userCode, verificationURL: url)
self.isSignInAlertPresented = true
} catch {
toast(error.localizedDescription, .error)
}
}
}
func checkStatus() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getGitHubCopilotAuthService()
status = try await service.checkStatus()
version = try await service.version()
isRunningAction = false
if status != .ok, status != .notSignedIn {
toast(
"GitHub Copilot status is not \"ok\". Please check if you have a valid GitHub Copilot subscription.",
.error
)
}
} catch {
toast(error.localizedDescription, .error)
}
}
}
func signOut() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getGitHubCopilotAuthService()
status = try await service.signOut()
} catch {
toast(error.localizedDescription, .error)
}
}
}
func cancelWaiting() {
waitingForSignIn = false
}
func copyAndOpen() {
waitingForSignIn = true
guard let signInResponse else {
toast("Missing sign in details.", .error)
return
}
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(signInResponse.userCode, forType: NSPasteboard.PasteboardType.string)
toast("Sign-in code \(signInResponse.userCode) copied", .info)
Task {
await openURL(signInResponse.verificationURL)
waitForSignIn()
}
}
func waitForSignIn() {
Task {
do {
guard waitingForSignIn else { return }
guard let signInResponse else {
waitingForSignIn = false
return
}
let service = try getGitHubCopilotAuthService()
let (username, status) = try await service.signInConfirm(userCode: signInResponse.userCode)
waitingForSignIn = false
self.username = username
self.status = status
} catch let error as GitHubCopilotError {
if case .languageServerError(.timeout) = error {
// TODO figure out how to extend the default timeout on a Chime LSP request
// Until then, reissue request
waitForSignIn()
return
}
throw error
} catch {
toast(error.localizedDescription, .error)
}
}
}
}