From 58f438816846043b3be77fd3cdeb7552a81126a6 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Wed, 25 Jun 2025 17:38:02 +0100 Subject: [PATCH] added duplicate host swipe action moved logic for dleting hosts into the hostsmanager added host "name" textbox added host "name" in keys view --- ShhShell/Host/HostsManager.swift | 18 +++++++++++++++++- ShhShell/Views/Hosts/ConnectionView.swift | 3 +++ ShhShell/Views/Hosts/HostsView.swift | 18 +++++++++++------- ShhShell/Views/Keys/KeyManagerView.swift | 2 +- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ShhShell/Host/HostsManager.swift b/ShhShell/Host/HostsManager.swift index c6d5c07..74d1e50 100644 --- a/ShhShell/Host/HostsManager.swift +++ b/ShhShell/Host/HostsManager.swift @@ -7,6 +7,7 @@ import Foundation import LocalAuthentication +import SwiftUI class HostsManager: ObservableObject, @unchecked Sendable { private let userDefaults = NSUbiquitousKeyValueStore.default @@ -36,11 +37,19 @@ class HostsManager: ObservableObject, @unchecked Sendable { func updateHost(_ updatedHost: Host) { if let index = savedHosts.firstIndex(where: { $0.id == updatedHost.id }) { - savedHosts[index] = updatedHost + withAnimation { savedHosts[index] = updatedHost } saveSavedHosts() } } + func duplicateHost(_ hostToDup: Host) { + var hostNewID = hostToDup + hostNewID.id = UUID() + if let index = savedHosts.firstIndex(where: { $0 == hostToDup }) { + savedHosts.insert(hostNewID, at: index+1) + } + } + func loadSavedHosts() { userDefaults.synchronize() let decoder = JSONDecoder() @@ -59,6 +68,13 @@ class HostsManager: ObservableObject, @unchecked Sendable { } } + func removeHost(_ host: Host) { + if let index = savedHosts.firstIndex(where: { $0.id == host.id }) { + let _ = withAnimation { savedHosts.remove(at: index) } + saveSavedHosts() + } + } + func getKeys() -> [Keypair] { var result: [Keypair] = [] for host in savedHosts { diff --git a/ShhShell/Views/Hosts/ConnectionView.swift b/ShhShell/Views/Hosts/ConnectionView.swift index 7e48e78..9b16c19 100644 --- a/ShhShell/Views/Hosts/ConnectionView.swift +++ b/ShhShell/Views/Hosts/ConnectionView.swift @@ -33,6 +33,9 @@ struct ConnectionView: View { .modifier(foregroundColorStyle(checkAuth(handler.state) ? .green : .red)) Text("\(handler.state)") } + TextField("name", text: $handler.host.name) + .textFieldStyle(.roundedBorder) + TextField("address", text: $handler.host.address) .textFieldStyle(.roundedBorder) diff --git a/ShhShell/Views/Hosts/HostsView.swift b/ShhShell/Views/Hosts/HostsView.swift index 154d49b..a15e556 100644 --- a/ShhShell/Views/Hosts/HostsView.swift +++ b/ShhShell/Views/Hosts/HostsView.swift @@ -44,22 +44,26 @@ struct HostsView: View { keyManager: keyManager ) } label: { - if host.address.isEmpty { - Text(host.id.uuidString) - } else { + if host.name.isEmpty { Text(host.address) + } else if host.address.isEmpty { + Text(host.name) + } else { + Text(host.id.uuidString) } } .animation(.default, value: host) .swipeActions(edge: .trailing) { Button(role: .destructive) { - if let index = hostsManager.savedHosts.firstIndex(where: { $0.id == host.id }) { - let _ = withAnimation { hostsManager.savedHosts.remove(at: index) } - hostsManager.saveSavedHosts() - } + hostsManager.removeHost(host) } label: { Label("Delete", systemImage: "trash") } + Button() { + hostsManager.duplicateHost(host) + } label: { + Label("Duplicate", systemImage: "square.filled.on.square") + } } } } diff --git a/ShhShell/Views/Keys/KeyManagerView.swift b/ShhShell/Views/Keys/KeyManagerView.swift index 90a5bd0..886bcbe 100644 --- a/ShhShell/Views/Keys/KeyManagerView.swift +++ b/ShhShell/Views/Keys/KeyManagerView.swift @@ -46,7 +46,7 @@ struct KeyManagerView: View { } ForEach(hostsManager.savedHosts) { host in VStack(alignment: .leading) { - Text(host.address) + Text(host.name + "\n" + host.address) .bold() Text(host.key ?? "nil") }