forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotView.swift
More file actions
158 lines (147 loc) · 5.32 KB
/
CopilotView.swift
File metadata and controls
158 lines (147 loc) · 5.32 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
import AppKit
import CopilotModel
import SwiftUI
struct CopilotView: View {
@Environment(\.openURL) var openURL
@AppStorage("username") var username: String = ""
@State var copilotStatus: CopilotStatus?
@State var message: String?
@State var userCode: String?
@State var version: String?
@State var isRunningAction: Bool = false
var body: some View {
Section {
HStack {
VStack(alignment: .leading, spacing: 8) {
Text("Copilot")
.font(.title)
.padding(.bottom, 12)
Text("Version: \(version ?? "Loading..")")
Text("Status: \(copilotStatus?.description ?? "Loading..")")
HStack(alignment: .center) {
Button("Refresh") { checkStatus() }
if copilotStatus == .notSignedIn {
Button("Sign In") { signIn() }
Button("Confirm Sign-in") { confirmSignIn() }
}
if copilotStatus == .ok || copilotStatus == .alreadySignedIn || copilotStatus == .notAuthorized {
Button("Sign Out") { signOut() }
}
if isRunningAction {
ActivityIndicatorView()
}
}
.buttonStyle(.copilot)
.opacity(isRunningAction ? 0.8 : 1)
.disabled(isRunningAction)
}
Spacer()
}.overlay(alignment: .topTrailing) {
if let message {
Text(message)
.padding(.horizontal, 4)
.padding(.vertical, 2)
.background(
RoundedRectangle(cornerRadius: 4)
.fill(Color.red)
)
}
}
}.onAppear {
if isPreview { return }
checkStatus()
}
}
func checkStatus() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getService()
copilotStatus = try await service.checkStatus()
version = try await service.getVersion()
message = nil
isRunningAction = false
} catch {
message = error.localizedDescription
}
}
}
func signIn() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getService()
let (uri, userCode) = try await service.signInInitiate()
self.userCode = userCode
guard let url = URL(string: uri) else {
message = "Verification URI is incorrect."
return
}
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(userCode, forType: NSPasteboard.PasteboardType.string)
message = "Usercode \(userCode) already copied!"
openURL(url)
} catch {
message = error.localizedDescription
}
}
}
func confirmSignIn() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getService()
guard let userCode else {
message = "Usercode is empty."
return
}
let (username, status) = try await service.signInConfirm(userCode: userCode)
self.username = username
copilotStatus = status
} catch {
message = error.localizedDescription
}
}
}
func signOut() {
Task {
isRunningAction = true
defer { isRunningAction = false }
do {
let service = try getService()
copilotStatus = try await service.signOut()
} catch {
message = error.localizedDescription
}
}
}
}
struct ActivityIndicatorView: NSViewRepresentable {
func makeNSView(context _: Context) -> NSProgressIndicator {
let progressIndicator = NSProgressIndicator()
progressIndicator.style = .spinning
progressIndicator.appearance = NSAppearance(named: .vibrantLight)
progressIndicator.controlSize = .small
progressIndicator.startAnimation(nil)
return progressIndicator
}
func updateNSView(_: NSProgressIndicator, context _: Context) {
// No-op
}
}
struct CopilotView_Previews: PreviewProvider {
static var previews: some View {
VStack(alignment: .leading, spacing: 8) {
CopilotView(copilotStatus: .notSignedIn, version: "1.0.0")
CopilotView(copilotStatus: .alreadySignedIn, message: "Error")
CopilotView(copilotStatus: .alreadySignedIn, isRunningAction: true)
}
.frame(height: 800)
.padding(.all, 8)
.background(Color.black)
}
}