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 Foundation
import LocalAuthentication import LocalAuthentication
import SwiftUI
class HostsManager: ObservableObject, @unchecked Sendable { class HostsManager: ObservableObject, @unchecked Sendable {
private let userDefaults = NSUbiquitousKeyValueStore.default private let userDefaults = NSUbiquitousKeyValueStore.default
@@ -36,11 +37,19 @@ class HostsManager: ObservableObject, @unchecked Sendable {
func updateHost(_ updatedHost: Host) { func updateHost(_ updatedHost: Host) {
if let index = savedHosts.firstIndex(where: { $0.id == updatedHost.id }) { if let index = savedHosts.firstIndex(where: { $0.id == updatedHost.id }) {
savedHosts[index] = updatedHost withAnimation { savedHosts[index] = updatedHost }
saveSavedHosts() 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() { func loadSavedHosts() {
userDefaults.synchronize() userDefaults.synchronize()
let decoder = JSONDecoder() 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] { func getKeys() -> [Keypair] {
var result: [Keypair] = [] var result: [Keypair] = []
for host in savedHosts { for host in savedHosts {

View File

@@ -33,6 +33,9 @@ struct ConnectionView: View {
.modifier(foregroundColorStyle(checkAuth(handler.state) ? .green : .red)) .modifier(foregroundColorStyle(checkAuth(handler.state) ? .green : .red))
Text("\(handler.state)") Text("\(handler.state)")
} }
TextField("name", text: $handler.host.name)
.textFieldStyle(.roundedBorder)
TextField("address", text: $handler.host.address) TextField("address", text: $handler.host.address)
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)

View File

@@ -44,22 +44,26 @@ struct HostsView: View {
keyManager: keyManager keyManager: keyManager
) )
} label: { } label: {
if host.address.isEmpty { if host.name.isEmpty {
Text(host.id.uuidString)
} else {
Text(host.address) Text(host.address)
} else if host.address.isEmpty {
Text(host.name)
} else {
Text(host.id.uuidString)
} }
} }
.animation(.default, value: host) .animation(.default, value: host)
.swipeActions(edge: .trailing) { .swipeActions(edge: .trailing) {
Button(role: .destructive) { Button(role: .destructive) {
if let index = hostsManager.savedHosts.firstIndex(where: { $0.id == host.id }) { hostsManager.removeHost(host)
let _ = withAnimation { hostsManager.savedHosts.remove(at: index) }
hostsManager.saveSavedHosts()
}
} label: { } label: {
Label("Delete", systemImage: "trash") 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 ForEach(hostsManager.savedHosts) { host in
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text(host.address) Text(host.name + "\n" + host.address)
.bold() .bold()
Text(host.key ?? "nil") Text(host.key ?? "nil")
} }