secretive/Secretive/Views/SetupView.swift

146 lines
4.3 KiB
Swift
Raw Normal View History

2020-03-04 07:14:38 +00:00
import Foundation
import SwiftUI
struct SetupView: View {
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
var completion: ((Bool) -> Void)?
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
var body: some View {
Form {
SetupStepView<Spacer>(text: "Secretive needs to install a helper app to sign requests when the main app isn't running. This app is called \"SecretAgent\" and you might see it in Activity Manager from time to time.",
2020-03-21 04:14:51 +00:00
index: 1,
nestedView: nil,
actionText: "Install") {
2020-03-04 07:14:38 +00:00
self.installLaunchAgent()
}
2020-03-21 04:23:15 +00:00
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.",
2020-03-04 07:14:38 +00:00
index: 2,
nestedView: SetupStepCommandView(text: Constants.socketPrompt),
actionText: "Added") {
self.markAsDone()
}
HStack {
Spacer()
2020-03-21 04:14:51 +00:00
Button(action: { self.completion?(true) }) {
Text("Finish")
}
2020-03-04 07:14:38 +00:00
.padding()
}
2020-03-22 01:10:37 +00:00
}.frame(minWidth: 640, minHeight: 400)
2020-03-04 07:14:38 +00:00
}
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
}
struct SetupStepView<NestedViewType: View>: View {
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
let text: String
let index: Int
let nestedView: NestedViewType?
@State var completed = false
let actionText: String
let action: (() -> Bool)
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
var body: some View {
Section {
HStack {
ZStack {
if completed {
Circle().foregroundColor(.green)
.frame(width: 30, height: 30)
Text("")
.foregroundColor(.white)
.bold()
} else {
Circle().foregroundColor(.blue)
.frame(width: 30, height: 30)
Text(String(describing: index))
.foregroundColor(.white)
.bold()
}
}
.padding()
VStack {
Text(text)
.opacity(completed ? 0.5 : 1)
.lineLimit(nil)
if nestedView != nil {
2020-03-22 01:10:37 +00:00
nestedView!.padding()
2020-03-04 07:14:38 +00:00
}
}
.padding()
Button(action: {
self.completed = self.action()
}) {
Text(actionText)
}.disabled(completed)
.padding()
}
}
}
}
struct SetupStepCommandView: View {
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
let text: String
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
var body: some View {
2020-03-21 04:07:58 +00:00
VStack(alignment: .leading) {
Text(text)
.lineLimit(nil)
.font(.system(.caption, design: .monospaced))
.multilineTextAlignment(.leading)
2020-03-22 01:10:37 +00:00
.frame(minHeight: 50)
2020-03-21 04:07:58 +00:00
HStack {
Spacer()
Button(action: copy) {
Text("Copy")
}
}
2020-03-04 07:14:38 +00:00
}
2020-03-21 04:07:58 +00:00
.padding()
.background(Color(white: 0, opacity: 0.10))
.cornerRadius(10)
.onDrag {
return NSItemProvider(item: NSData(data: self.text.data(using: .utf8)!), typeIdentifier: kUTTypeUTF8PlainText as String)
2020-03-21 04:14:51 +00:00
2020-03-21 04:07:58 +00:00
}
}
2020-03-21 04:14:51 +00:00
2020-03-21 04:07:58 +00:00
func copy() {
NSPasteboard.general.declareTypes([.string], owner: nil)
NSPasteboard.general.setString(text, forType: .string)
2020-03-04 07:14:38 +00:00
}
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
}
extension SetupView {
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
func installLaunchAgent() -> Bool {
2020-04-04 22:16:31 +00:00
LaunchAgentController().install()
2020-03-04 07:14:38 +00:00
}
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
func markAsDone() -> Bool {
return true
}
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
}
extension SetupView {
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
enum Constants {
2020-03-07 06:23:51 +00:00
static let socketPath = (NSHomeDirectory().replacingOccurrences(of: "com.maxgoedjen.Secretive.Host", with: "com.maxgoedjen.Secretive.SecretAgent") as NSString).appendingPathComponent("socket.ssh") as String
2020-03-04 07:14:38 +00:00
static let socketPrompt = "export SSH_AUTH_SOCK=\(socketPath)"
}
2020-03-21 04:14:51 +00:00
2020-03-04 07:14:38 +00:00
}
2020-03-12 06:07:22 +00:00
#if DEBUG
2020-03-04 07:14:38 +00:00
struct SetupView_Previews: PreviewProvider {
static var previews: some View {
SetupView()
}
}
2020-03-12 06:07:22 +00:00
#endif