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
110 lines (96 loc) · 3.3 KB
/
CodeiumView.swift
File metadata and controls
110 lines (96 loc) · 3.3 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
import CodeiumService
import Foundation
import SwiftUI
struct CodeiumView: View {
class ViewModel: ObservableObject {
let codeiumAuthService = CodeiumAuthService()
@Published var isSignedIn: Bool
init() {
isSignedIn = codeiumAuthService.isSignedIn
}
func generateAuthURL() -> URL {
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 }
}
}
@StateObject var viewModel = ViewModel()
@State var isSignInPanelPresented = false
var body: some View {
Form {
if viewModel.isSignedIn {
Button(action: {
viewModel.isSignedIn = false
}) {
Text("Sign Out")
}
} else {
Button(action: {
isSignInPanelPresented = true
}) {
Text("Sign In")
}
}
}
.sheet(isPresented: $isSignInPanelPresented) {
CodeiumSignInView(viewModel: viewModel, isPresented: $isSignInPanelPresented)
}
}
}
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)
)
Button(action: {
isGeneratingKey = true
Task {
do {
try await viewModel.signIn(token: token)
isGeneratingKey = false
isPresented = false
} catch {
isGeneratingKey = false
toast(Text(error.localizedDescription), .error)
}
}
}) {
Text(isGeneratingKey ? "Signing In.." : "Sign In")
}.disabled(isGeneratingKey)
}
.padding()
.onAppear {
openURL(viewModel.generateAuthURL())
}
}
}
struct CodeiumView_Previews: PreviewProvider {
static var previews: some View {
CodeiumView()
.frame(width: 600, height: 500)
}
}