forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneral.swift
More file actions
78 lines (71 loc) · 2.62 KB
/
General.swift
File metadata and controls
78 lines (71 loc) · 2.62 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
import Client
import ComposableArchitecture
import Foundation
import LaunchAgentManager
import SwiftUI
struct General: ReducerProtocol {
struct State: Equatable {
var xpcServiceVersion: String?
var isAccessibilityPermissionGranted: Bool?
var isReloading = false
}
enum Action: Equatable {
case appear
case setupLaunchAgentIfNeeded
case reloadStatus
case finishReloading(xpcServiceVersion: String, permissionGranted: Bool)
case failedReloading
}
@Dependency(\.toast) var toast
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .appear:
return .run { send in
await send(.setupLaunchAgentIfNeeded)
}
case .setupLaunchAgentIfNeeded:
return .run { send in
#if DEBUG
// do not auto install on debug build
#else
Task {
do {
try await LaunchAgentManager()
.setupLaunchAgentForTheFirstTimeIfNeeded()
} catch {
toast(error.localizedDescription, .error)
}
}
#endif
await send(.reloadStatus)
}
case .reloadStatus:
state.isReloading = true
return .run { send in
let service = try getService()
do {
let xpcServiceVersion = try await service.getXPCServiceVersion().version
let isAccessibilityPermissionGranted = try await service
.getXPCServiceAccessibilityPermission()
await send(.finishReloading(
xpcServiceVersion: xpcServiceVersion,
permissionGranted: isAccessibilityPermissionGranted
))
} catch {
toast(error.localizedDescription, .error)
await send(.failedReloading)
}
}
case let .finishReloading(version, granted):
state.xpcServiceVersion = version
state.isAccessibilityPermissionGranted = granted
state.isReloading = false
return .none
case .failedReloading:
state.isReloading = false
return .none
}
}
}
}