From 54d7fbe88d34d6e3d7e25adc604508e56a5cc210 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Thu, 10 Sep 2020 23:17:03 -0700 Subject: [PATCH] Lots more SwiftUI cleanup. --- Secretive/App.swift | 8 +- Secretive/Views/ContentView.swift | 126 ++++++++++++------------- Secretive/Views/CreateSecretView.swift | 13 +-- Secretive/Views/DeleteSecretView.swift | 6 +- Secretive/Views/SecretDetailView.swift | 4 +- Secretive/Views/SetupView.swift | 11 +-- 6 files changed, 82 insertions(+), 86 deletions(-) diff --git a/Secretive/App.swift b/Secretive/App.swift index b4de2e6..2e2d95a 100644 --- a/Secretive/App.swift +++ b/Secretive/App.swift @@ -21,14 +21,14 @@ struct AppDelegate: App { @SceneBuilder var body: some Scene { WindowGroup { - ContentView() + ContentView(runningSetup: $showingSetup) .environmentObject(storeList) .environmentObject(updater) .environmentObject(agentStatusChecker) .sheet(isPresented: $showingSetup) { SetupView { completed in - self.showingSetup = false - self.hasRunSetup = completed + showingSetup = false + hasRunSetup = completed } } .onAppear { @@ -51,7 +51,7 @@ struct AppDelegate: App { } CommandGroup(after: .help) { Button("Setup Secret Agent") { - self.showingSetup = true + showingSetup = true } } } diff --git a/Secretive/Views/ContentView.swift b/Secretive/Views/ContentView.swift index f047f76..8da4580 100644 --- a/Secretive/Views/ContentView.swift +++ b/Secretive/Views/ContentView.swift @@ -7,39 +7,40 @@ struct ContentView Void)? @State private var active: AnySecret.ID? @State private var showingCreation = false @State private var deletingSecret: AnySecret? @State private var selectedUpdate: Release? + @Binding var runningSetup: Bool var body: some View { VStack { if storeList.anyAvailable { - NavigationView { - List(selection: $active) { - ForEach(storeList.stores) { store in - if store.isAvailable { - Section(header: Text(store.name)) { - if store.secrets.isEmpty { - if store is AnySecretStoreModifiable { - NavigationLink(destination: EmptyStoreModifiableView(), tag: Constants.emptyStoreModifiableTag, selection: self.$active) { - Text("No Secrets") + NavigationView { + List(selection: $active) { + ForEach(storeList.stores) { store in + if store.isAvailable { + Section(header: Text(store.name)) { + if store.secrets.isEmpty { + if store is AnySecretStoreModifiable { + NavigationLink(destination: EmptyStoreModifiableView(), tag: Constants.emptyStoreModifiableTag, selection: $active) { + Text("No Secrets") + } + } else { + NavigationLink(destination: EmptyStoreView(), tag: Constants.emptyStoreTag, selection: $active) { + Text("No Secrets") + } } } else { - NavigationLink(destination: EmptyStoreView(), tag: Constants.emptyStoreTag, selection: self.$active) { - Text("No Secrets") - } - } - } else { - ForEach(store.secrets) { secret in - NavigationLink(destination: SecretDetailView(secret: secret), tag: secret.id, selection: self.$active) { - Text(secret.name) - }.contextMenu { - if store is AnySecretStoreModifiable { - Button(action: { self.delete(secret: secret) }) { - Text("Delete") + ForEach(store.secrets) { secret in + NavigationLink(destination: SecretDetailView(secret: secret), tag: secret.id, selection: $active) { + Text(secret.name) + }.contextMenu { + if store is AnySecretStoreModifiable { + Button(action: { delete(secret: secret) }) { + Text("Delete") + } } } } @@ -47,42 +48,35 @@ struct ContentView ToolbarItem { -// let update = updater.update ?? Release(name: "", html_url: URL(string:"https://example.com")!, body: "") + var updateNotice: ToolbarItem { guard let update = updater.update else { - return ToolbarItem { - AnyView(Spacer()) - } + return ToolbarItem { AnyView(Spacer()) } } let color: Color let text: String @@ -108,7 +99,7 @@ struct ContentView ToolbarItem { -// ToolbarItem { -// Button(action: { -// self.runSetupBlock?() -// }, label: { -// Text("Agent is not running.") -// .font(.headline) -// .foregroundColor(.white) -// }) -// .background(Color.orange) -// .cornerRadius(5) -// } -// } + + var agentNotice: ToolbarItem { + guard agentStatusChecker.running else { + return ToolbarItem { AnyView(Spacer()) } + } + return ToolbarItem { + AnyView( + Button(action: { + runningSetup = true + }, label: { + Text("Secret Agent Is Not Running") + .font(.headline) + .foregroundColor(.white) + }) + .background(Color.orange) + .cornerRadius(5) + ) + } + } func delete(secret: SecretType) { deletingSecret = AnySecret(secret) @@ -143,12 +139,12 @@ struct ContentView () - + @Binding var showing: Bool + var body: some View { VStack { HStack { @@ -36,8 +35,10 @@ struct CreateSecretView: View { } HStack { Spacer() - Button("Cancel", action: dismissalBlock) - .keyboardShortcut(.cancelAction) + Button("Cancel") { + showing = false + } + .keyboardShortcut(.cancelAction) Button("Create", action: save) .disabled(name.isEmpty) .keyboardShortcut(.defaultAction) @@ -47,6 +48,6 @@ struct CreateSecretView: View { func save() { try! store.create(name: name, requiresAuthentication: requiresAuthentication) - dismissalBlock() + showing = false } } diff --git a/Secretive/Views/DeleteSecretView.swift b/Secretive/Views/DeleteSecretView.swift index 9b1ea92..7822565 100644 --- a/Secretive/Views/DeleteSecretView.swift +++ b/Secretive/Views/DeleteSecretView.swift @@ -38,7 +38,7 @@ struct DeleteSecretView: View { } } .onExitCommand { - self.dismissalBlock(false) + dismissalBlock(false) } } HStack { @@ -47,7 +47,7 @@ struct DeleteSecretView: View { .disabled(confirm != secret.name) .keyboardShortcut(.delete) Button("Don't Delete") { - self.dismissalBlock(false) + dismissalBlock(false) } .keyboardShortcut(.cancelAction) } @@ -58,6 +58,6 @@ struct DeleteSecretView: View { func delete() { try! store.delete(secret: secret) - self.dismissalBlock(true) + dismissalBlock(true) } } diff --git a/Secretive/Views/SecretDetailView.swift b/Secretive/Views/SecretDetailView.swift index 01d42ed..7aa6422 100644 --- a/Secretive/Views/SecretDetailView.swift +++ b/Secretive/Views/SecretDetailView.swift @@ -17,7 +17,7 @@ struct SecretDetailView: View { .frame(minWidth: 150, maxWidth: .infinity) .padding() }.onDrag { - return NSItemProvider(item: NSData(data: self.keyWriter.openSSHFingerprint(secret: self.secret).data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String) + return NSItemProvider(item: NSData(data: keyWriter.openSSHFingerprint(secret: secret).data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String) } Spacer().frame(height: 10) GroupBox(label: Text("Public Key")) { @@ -35,7 +35,7 @@ struct SecretDetailView: View { .padding() } .onDrag { - return NSItemProvider(item: NSData(data: self.keyString.data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String) + return NSItemProvider(item: NSData(data: keyString.data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String) } Spacer() } diff --git a/Secretive/Views/SetupView.swift b/Secretive/Views/SetupView.swift index b475d77..667ee95 100644 --- a/Secretive/Views/SetupView.swift +++ b/Secretive/Views/SetupView.swift @@ -11,17 +11,17 @@ struct SetupView: View { index: 1, nestedView: nil, actionText: "Install") { - self.installLaunchAgent() + installLaunchAgent() } SetupStepView(text: "Add this line to your shell config (.bashrc or .zshrc) telling SSH to talk to SecretAgent when it wants to authenticate. Drag this into your config file.", index: 2, nestedView: SetupStepCommandView(text: Constants.socketPrompt), actionText: "Added") { - self.markAsDone() + markAsDone() } HStack { Spacer() - Button(action: { self.completion?(true) }) { + Button(action: { completion?(true) }) { Text("Finish") } .padding() @@ -69,7 +69,7 @@ struct SetupStepView: View { } .padding() Button(action: { - self.completed = self.action() + completed = action() }) { Text(actionText) }.disabled(completed) @@ -101,8 +101,7 @@ struct SetupStepCommandView: View { .background(Color(white: 0, opacity: 0.10)) .cornerRadius(10) .onDrag { - return NSItemProvider(item: NSData(data: self.text.data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String) - + return NSItemProvider(item: NSData(data: text.data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String) } }