From 5ef1a51ba81f076e31de37df49c9dea35507972e Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Thu, 3 Jul 2025 10:41:40 +0100 Subject: [PATCH] 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) --- ShhShell/Host/HostsManager.swift | 26 ++++++++++++----- ShhShell/Views/Keys/KeyDetailView.swift | 39 +++++++++++++++++-------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/ShhShell/Host/HostsManager.swift b/ShhShell/Host/HostsManager.swift index b1fc339..1edff23 100644 --- a/ShhShell/Host/HostsManager.swift +++ b/ShhShell/Host/HostsManager.swift @@ -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 } } diff --git a/ShhShell/Views/Keys/KeyDetailView.swift b/ShhShell/Views/Keys/KeyDetailView.swift index 813dc9e..ec31b55 100644 --- a/ShhShell/Views/Keys/KeyDetailView.swift +++ b/ShhShell/Views/Keys/KeyDetailView.swift @@ -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) } }