Sesame-iOS/Sesame/SingleKeyView.swift
2022-05-02 23:25:11 +02:00

70 lines
1.7 KiB
Swift

import SwiftUI
import CryptoKit
struct SingleKeyView: View {
@State
private var needRefresh = false
@Binding
var keyManager: KeyManagement
let type: KeyManagement.KeyType
private var generateText: String {
hasKey ? "Regenerate" : "Generate"
}
var hasKey: Bool {
keyManager.has(type)
}
var content: String {
keyManager.get(type)?.displayString ?? "-"
}
var copyText: String {
guard let key = keyManager.get(type)?.data else {
return ""
}
guard type.usesHashing else {
return key.hexEncoded
}
return SHA256.hash(data: key).hexEncoded
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(type.displayName)
.bold()
Text(needRefresh ? content : content)
.font(.system(.body, design: .monospaced))
.foregroundColor(.secondary)
HStack() {
Button(generateText) {
keyManager.generate(type)
needRefresh.toggle()
}
.padding([.horizontal, .bottom])
.padding(.top, 4)
Button(type.usesHashing ? "Copy hash" : "Copy") {
UIPasteboard.general.string = copyText
}
.disabled(!hasKey)
.padding([.horizontal, .bottom])
.padding(.top, 4)
Spacer()
}
}
}
}
struct SingleKeyView_Previews: PreviewProvider {
static var previews: some View {
SingleKeyView(
keyManager: .constant(KeyManagement()),
type: .deviceKey)
}
}