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) {
guard let index = hosts.firstIndex(where: { $0.id == onHost.id }) else { return }
hosts[index].privateKeyID = keypair.id
withAnimation { hosts[index].privateKeyID = keypair.id }
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] = []
for key in keys {
let hosts = hosts.filter({
$0.privateKeyID == key.id
})
result += hosts
}
let hosts = hosts.filter({
$0.privateKeyID == key.id
})
result += hosts
return result
}
func hostsNotUsing(key: Keypair) -> [Host] {
var result: [Host]
result = hosts.filter { $0.privateKeyID != key.id }
return result
}
}

View File

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