added duplicate host swipe action

moved logic for dleting hosts into the hostsmanager
added host "name" textbox
added host "name" in keys view
This commit is contained in:
neon443
2025-06-25 17:38:02 +01:00
parent 52629d7654
commit 58f4388168
4 changed files with 32 additions and 9 deletions

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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")
}
}
}
}

View File

@@ -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")
}