forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeiumView.swift
More file actions
335 lines (297 loc) · 11.1 KB
/
CodeiumView.swift
File metadata and controls
335 lines (297 loc) · 11.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import CodeiumService
import Foundation
import SwiftUI
struct CodeiumView: View {
class ViewModel: ObservableObject {
let codeiumAuthService = CodeiumAuthService()
let installationManager = CodeiumInstallationManager()
@Published var isSignedIn: Bool
@Published var installationStatus: CodeiumInstallationManager.InstallationStatus
@Published var installationStep: CodeiumInstallationManager.InstallationStep?
@AppStorage(\.codeiumVerboseLog) var codeiumVerboseLog
@AppStorage(\.codeiumEnterpriseMode) var codeiumEnterpriseMode
@AppStorage(\.codeiumPortalUrl) var codeiumPortalUrl
@AppStorage(\.codeiumApiUrl) var codeiumApiUrl
init() {
isSignedIn = codeiumAuthService.isSignedIn
installationStatus = installationManager.checkInstallation()
}
init(
isSignedIn: Bool,
installationStatus: CodeiumInstallationManager.InstallationStatus,
installationStep: CodeiumInstallationManager.InstallationStep?
) {
assert(isPreview)
self.isSignedIn = isSignedIn
self.installationStatus = installationStatus
self.installationStep = installationStep
}
func generateAuthURL() -> URL {
if codeiumEnterpriseMode && (codeiumPortalUrl != "") {
return URL(
string: codeiumPortalUrl +
"/profile?response_type=token&redirect_uri=show-auth-token&state=\(UUID().uuidString)&scope=openid%20profile%20email&redirect_parameters_type=query"
)!
}
return URL(
string: "https://www.codeium.com/profile?response_type=token&redirect_uri=show-auth-token&state=\(UUID().uuidString)&scope=openid%20profile%20email&redirect_parameters_type=query"
)!
}
func signIn(token: String) async throws {
try await codeiumAuthService.signIn(token: token)
Task { @MainActor in isSignedIn = true }
}
func signOut() async throws {
try await codeiumAuthService.signOut()
Task { @MainActor in isSignedIn = false }
}
func refreshInstallationStatus() {
Task { @MainActor in
installationStatus = installationManager.checkInstallation()
}
}
func install() async throws {
defer { refreshInstallationStatus() }
do {
for try await step in installationManager.installLatestVersion() {
Task { @MainActor in
self.installationStep = step
}
}
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
Task { @MainActor in
self.installationStep = nil
}
}
} catch {
Task { @MainActor in
installationStep = nil
}
throw error
}
}
func uninstall() {
Task {
defer { refreshInstallationStatus() }
try await installationManager.uninstall()
}
}
}
@StateObject var viewModel = ViewModel()
@Environment(\.toast) var toast
@State var isSignInPanelPresented = false
var installButton: some View {
Button(action: {
Task {
do {
try await viewModel.install()
} catch {
toast(error.localizedDescription, .error)
}
}
}) {
Text("Install")
}
.disabled(viewModel.installationStep != nil)
}
var updateButton: some View {
Button(action: {
Task {
do {
try await viewModel.install()
} catch {
toast(error.localizedDescription, .error)
}
}
}) {
Text("Update")
}
.disabled(viewModel.installationStep != nil)
}
var uninstallButton: some View {
Button(action: {
viewModel.uninstall()
}) {
Text("Uninstall")
}
.disabled(viewModel.installationStep != nil)
}
var body: some View {
VStack(alignment: .leading) {
VStack(alignment: .leading) {
switch viewModel.installationStatus {
case .notInstalled:
HStack {
Text("Language Server Version: Not Installed")
installButton
}
case let .installed(version):
HStack {
Text("Language Server Version: \(version)")
uninstallButton
}
case let .outdated(current: current, latest: latest):
HStack {
Text("Language Server Version: \(current) (Update Available: \(latest))")
uninstallButton
updateButton
}
case let .unsupported(current: current, latest: latest):
HStack {
Text("Language Server Version: \(current) (Supported Version: \(latest))")
uninstallButton
updateButton
}
}
if viewModel.isSignedIn {
Text("Status: Signed In")
Button(action: {
Task {
do {
try await viewModel.signOut()
} catch {
toast(error.localizedDescription, .error)
}
}
}) {
Text("Sign Out")
}
} else {
Text("Status: Not Signed In")
Button(action: {
isSignInPanelPresented = true
}) {
Text("Sign In")
}
}
}
.padding(8)
.frame(maxWidth: .infinity, alignment: .leading)
.overlay {
RoundedRectangle(cornerRadius: 8)
.stroke(Color(nsColor: .separatorColor), style: .init(lineWidth: 1))
}
.sheet(isPresented: $isSignInPanelPresented) {
CodeiumSignInView(viewModel: viewModel, isPresented: $isSignInPanelPresented)
}
.onChange(of: viewModel.installationStep) { newValue in
if let step = newValue {
switch step {
case .downloading:
toast("Downloading..", .info)
case .uninstalling:
toast("Uninstalling old version..", .info)
case .decompressing:
toast("Decompressing..", .info)
case .done:
toast("Done!", .info)
}
}
}
Form {
Toggle("Codeium Enterprise Mode", isOn: $viewModel.codeiumEnterpriseMode)
TextField("Codeium Portal URL", text: $viewModel.codeiumPortalUrl)
TextField("Codeium API URL", text: $viewModel.codeiumApiUrl)
}
.padding(8)
.frame(maxWidth: .infinity, alignment: .leading)
.overlay {
RoundedRectangle(cornerRadius: 8)
.stroke(Color(nsColor: .separatorColor), style: .init(lineWidth: 1))
}
Divider()
Form {
Toggle("Verbose Log", isOn: $viewModel.codeiumVerboseLog)
}
}
}
}
struct CodeiumSignInView: View {
let viewModel: CodeiumView.ViewModel
@Binding var isPresented: Bool
@Environment(\.openURL) var openURL
@Environment(\.toast) var toast
@State var isGeneratingKey = false
@State var token = ""
var body: some View {
VStack {
Text(
"You will be redirected to codeium.com. Please paste the generated token below and click the \"Sign In\" button."
)
TextEditor(text: $token)
.font(Font.system(.body, design: .monospaced))
.padding(4)
.frame(minHeight: 120)
.multilineTextAlignment(.leading)
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
)
HStack {
Spacer()
Button(action: {
isPresented = false
}) {
Text("Cancel")
}
Button(action: {
isGeneratingKey = true
Task {
do {
try await viewModel.signIn(token: token)
isGeneratingKey = false
isPresented = false
} catch {
isGeneratingKey = false
toast(error.localizedDescription, .error)
}
}
}) {
Text(isGeneratingKey ? "Signing In.." : "Sign In")
}.disabled(isGeneratingKey)
}
}
.padding()
.onAppear {
openURL(viewModel.generateAuthURL())
}
}
}
struct CodeiumView_Previews: PreviewProvider {
class TestViewModel: CodeiumView.ViewModel {
override func generateAuthURL() -> URL {
return URL(string: "about:blank")!
}
override func signIn(token: String) async throws {}
override func signOut() async throws {}
override func refreshInstallationStatus() {}
override func install() async throws {}
override func uninstall() {}
}
static var previews: some View {
VStack(alignment: .leading, spacing: 8) {
CodeiumView(viewModel: TestViewModel(
isSignedIn: false,
installationStatus: .notInstalled,
installationStep: nil
))
CodeiumView(viewModel: TestViewModel(
isSignedIn: true,
installationStatus: .installed("1.2.9"),
installationStep: nil
))
CodeiumView(viewModel: TestViewModel(
isSignedIn: true,
installationStatus: .outdated(current: "1.2.9", latest: "1.3.0"),
installationStep: .downloading
))
CodeiumView(viewModel: TestViewModel(
isSignedIn: true,
installationStatus: .unsupported(current: "1.5.9", latest: "1.3.0"),
installationStep: .downloading
))
}
.padding(8)
}
}