-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.swift
216 lines (195 loc) · 7.66 KB
/
App.swift
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
// Started by Manuel @StuFFmc Carrasco Molina on New year's Eve 2021 / January 2022
import SwiftUI
import AppKit
import SafariServices
enum AlertType: Int {
case installed
case deleted
}
struct InstallButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.font(Font.system(size: 14, weight: .regular))
.foregroundColor(configuration.isPressed ? Color.black.opacity(0.5) : Color.black)
.frame(width: 213, height: 35)
.background(Color(red: 0.97, green: 0.77, blue: 0.33))
.cornerRadius(35 / 2)
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
PFMoveToApplicationsFolderIfNecessary()
NSWindow.allowsAutomaticWindowTabbing = false
}
}
@main
struct Window: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.scenePhase) private var scenePhase
@State var localizedError: String?
@State var browsers: [Browser] = []
@State var showAlert: AlertType? = nil
@State var isEnabledForSafari = false
var body: some Scene {
WindowGroup {
ZStack {
Color("Background")
VStack {
VStack {
Spacer().frame(height: 32.0)
Image("logo")
.frame(width: 120.0)
Spacer().frame(height: 24.0)
Text("Lightning buzz for your Browser")
.font(Font.system(size: 24.0, weight: .bold))
Spacer().frame(height: 4.0)
Text("Alby brings Bitcoin payments to the web with in-browser payments and identity, all with your own wallet.\n Install Alby for you browser and use Alby directly in your browser.")
.font(Font.system(size: 14.0, weight: .regular))
.foregroundColor(Color.primary.opacity(0.7))
}.multilineTextAlignment(.center)
Spacer().frame(height: 16.0)
HStack(spacing: 40.0) {
ForEach(browsers) {
view(for: $0)
}
}
VStack {
VStack {
Spacer().frame(height: 16.0)
Text("(You can close this app after installation, but keep it in you Applications folder.)")
.font(Font.system(size: 12.0, weight: .regular))
.foregroundColor(Color.primary.opacity(0.6))
}}
Spacer().frame(height: 60.0)
}
}
.navigationTitle("Alby")
.frame(width: (CGFloat(columns) * 213.0) + (CGFloat(columns - 1) * 40.0) + 78.0, height: 570.0)
.alert(isPresented: .constant(localizedError?.isEmpty == false)) {
Alert(title: Text(localizedError!))
}
.alert(isPresented: .constant(showAlert != nil)) {
Alert(title: Text(showAlert == .installed ? "Alby is configured successfully. Now let’s add it to your browser" : "Companion App Deleted"))
}
.onAppear {
seek()
}
}
.onChange(of: scenePhase) { _ in
checkIfIsEnabledForSafari()
}
.commands {
CommandGroup(after: .newItem) {
Button {
seek()
} label: {
Text("Refresh")
}
.keyboardShortcut("R", modifiers: [.command])
}
}
}
private func seek() {
// browsers = [Browser(id: "com.apple.Safari", appSupportFolder: nil, chrome: nil)] + Browser.installed
browsers = Browser.installed
// checkIfIsEnabledForSafari()
}
private var columns: Int {
browsers.count < 3 ? 3 : browsers.count
}
private func checkIfIsEnabledForSafari() {
Task {
do {
isEnabledForSafari = try await SFSafariExtensionManager.stateOfSafariExtension(withIdentifier: safariExtensionBundleIdentifier).isEnabled
} catch {
localizedError = error.localizedDescription
isEnabledForSafari = false
}
}
}
var checkmark: some View {
Image(systemName: "checkmark")
.foregroundColor(.green)
.font(.largeTitle.bold())
}
@ViewBuilder
func view(for browser: Browser) -> some View {
VStack(spacing: 16.0) {
ZStack {
Color(red: 1.00, green: 0.98, blue: 0.93)
if let icon = browser.icon {
ZStack {
Image(nsImage: icon)
.resizable()
.frame(width: 150.0, height: 150.0)
if let installed = browser.companionInstalled {
if installed { checkmark }
} else {
if isEnabledForSafari {
checkmark
}
}
}
}
}
.frame(width: 213.0, height: 213.0)
.cornerRadius(16.0)
Button {
if browser.chrome == nil {
SFSafariApplication.showPreferencesForExtension(withIdentifier: safariExtensionBundleIdentifier) { error in
guard let error = error else {
return
}
localizedError = error.localizedDescription
}
} else {
installOrRemoveCompanion(for: browser)
}
} label: {
if let installed = browser.companionInstalled {
if installed {
Text("Remove")
} else {
Text("Install")
}
} else {
if isEnabledForSafari {
Text("Disable")
} else {
Text("Enable")
}
}
}
.buttonStyle(InstallButtonStyle())
}
}
private func installOrRemoveCompanion(for browser: Browser) {
guard let installed = browser.companionInstalled else {
return
}
if installed {
// Remove JSON
do {
try browser.remove()
showAlert = installed ? nil : .deleted
} catch {
localizedError = error.localizedDescription
}
} else {
// Install
do {
// Copy JSON
try browser.install()
showAlert = installed ? .installed : nil
// Install Extension
if let url = URL(string: "https://getalby.com/install/")?.appendingPathComponent(browser.id),
let application = browser.application {
NSWorkspace.shared.open([url], withApplicationAt: application, configuration: NSWorkspace.OpenConfiguration())
}
} catch {
localizedError = error.localizedDescription
}
}
seek()
}
}