fix removing one host from keydetailview removes them all

added hostsNotUsing(key) to get the opposite result of hostsuign
make the hosts icons a tad smaller
rename gethostsusingkeys -> hostsUsing(key)
This commit is contained in:
neon443
2025-07-03 10:41:40 +01:00
parent c2c74248e2
commit 5ef1a51ba8
2 changed files with 45 additions and 20 deletions

View File

@@ -171,18 +171,28 @@ class HostsManager: ObservableObject, @unchecked Sendable {
func set(keypair: Keypair, onHost: Host) { func set(keypair: Keypair, onHost: Host) {
guard let index = hosts.firstIndex(where: { $0.id == onHost.id }) else { return } guard let index = hosts.firstIndex(where: { $0.id == onHost.id }) else { return }
hosts[index].privateKeyID = keypair.id withAnimation { hosts[index].privateKeyID = keypair.id }
saveHosts() saveHosts()
} }
func getHostsUsingKeys(_ keys: [Keypair]) -> [Host] { func unsetKeypair(onHost: Host) {
guard let index = hosts.firstIndex(where: { $0.id == onHost.id }) else { return }
withAnimation { hosts[index].privateKeyID = nil }
saveHosts()
}
func hostsUsing(key: Keypair) -> [Host] {
var result: [Host] = [] var result: [Host] = []
for key in keys { let hosts = hosts.filter({
let hosts = hosts.filter({ $0.privateKeyID == key.id
$0.privateKeyID == key.id })
}) result += hosts
result += hosts return result
} }
func hostsNotUsing(key: Keypair) -> [Host] {
var result: [Host]
result = hosts.filter { $0.privateKeyID != key.id }
return result return result
} }
} }

View File

@@ -23,32 +23,47 @@ struct KeyDetailView: View {
.ignoresSafeArea(.all) .ignoresSafeArea(.all)
List { List {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text("Used on") if hostsManager.hostsUsing(key: keypair).isEmpty {
.bold() Text("This key is not used on any host.")
ForEach(hostsManager.getHostsUsingKeys([keypair])) { host in .bold()
} else {
Text("Used on")
.bold()
}
ForEach(hostsManager.hostsUsing(key: keypair)) { host in
HStack { HStack {
HostSymbolPreview(symbol: host.symbol, label: host.label) HostSymbolPreview(symbol: host.symbol, label: host.label)
.frame(width: 40, height: 40) .frame(width: 30, height: 30)
.foregroundStyle(.gray)
Text(host.description) Text(host.description)
Spacer()
Button() {
hostsManager.unsetKeypair(onHost: host)
} label: {
Image(systemName: "minus.circle.fill")
.symbolRenderingMode(.multicolor)
}
.buttonStyle(.plain)
} }
} }
Menu("Add") { Menu() {
let hostsNotUsingKey = hostsManager.hosts.filter( ForEach(hostsManager.hostsNotUsing(key: keypair)) { host in
{
hostsManager.getHostsUsingKeys([keypair]).contains($0)
})
ForEach(hostsNotUsingKey) { host in
Button() { Button() {
hostsManager.set(keypair: keypair, onHost: host) hostsManager.set(keypair: keypair, onHost: host)
} label: { } label: {
Image(systemName: "plus") Image(systemName: "plus")
.resizable().scaledToFit() .resizable().scaledToFit()
.foregroundStyle(.blue) .foregroundStyle(.blue)
.frame(width: 30, height: 30) .frame(width: 20, height: 20)
Text("Add") Text(host.description)
.foregroundStyle(.blue) .foregroundStyle(.blue)
} }
} }
} label: {
Image(systemName: "plus.circle.fill")
.symbolRenderingMode(.multicolor)
Text("Add")
.foregroundStyle(.green)
} }
} }