From 4eb9cbb842c319e09226bce7f9ce99c86b040609 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Fri, 15 Aug 2025 13:16:41 +0100 Subject: [PATCH] add addtohistory function update formathistory made history have an array of history made ts codabel --- ShhShell/Host/History.swift | 2 +- ShhShell/Host/HostsManager.swift | 21 +++++++++++++-------- ShhShell/Views/Hosts/ConnectionView.swift | 2 +- ShhShell/Views/Hosts/RecentsView.swift | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ShhShell/Host/History.swift b/ShhShell/Host/History.swift index 7ef77a6..bcc1091 100644 --- a/ShhShell/Host/History.swift +++ b/ShhShell/Host/History.swift @@ -7,7 +7,7 @@ import Foundation -struct History: Identifiable { +struct History: Identifiable, Codable { var id: UUID = UUID() var host: Host diff --git a/ShhShell/Host/HostsManager.swift b/ShhShell/Host/HostsManager.swift index c430188..0a67ce3 100644 --- a/ShhShell/Host/HostsManager.swift +++ b/ShhShell/Host/HostsManager.swift @@ -24,7 +24,7 @@ class HostsManager: ObservableObject, @unchecked Sendable { @Published var snippets: [Snippet] = [] - @Published var history: [Host] = [] + @Published var history: [History] = [] var tint: SwiftUI.Color { selectedTheme.ansi[selectedAnsi].suiColor @@ -40,22 +40,27 @@ class HostsManager: ObservableObject, @unchecked Sendable { func loadHistory() { guard let data = userDefaults.data(forKey: "history") else { return } - guard let decoded = try? JSONDecoder().decode([Host].self, from: data) else { return } + guard let decoded = try? JSONDecoder().decode([History].self, from: data) else { return } withAnimation { self.history = decoded } } - func formatHistory() -> [History] { + func addToHistory(_ host: Host) { + history.append(History(host: host, count: 1)) + formatHistory() + } + + func formatHistory() { var result: [History] = [] - for host in history { - if result.last?.host == host { - guard var lastOne = result.popLast() else { return result } + for item in history { + if result.last?.host == item.host { + guard var lastOne = result.popLast() else { continue } lastOne.count += 1 result.append(lastOne) } else { - result.append(History(host: host, count: 1)) + result.append(History(host: item.host, count: 1)) } } - return result + withAnimation { self.history = result } } func saveHistory() { diff --git a/ShhShell/Views/Hosts/ConnectionView.swift b/ShhShell/Views/Hosts/ConnectionView.swift index 68deefb..f8d7df1 100644 --- a/ShhShell/Views/Hosts/ConnectionView.swift +++ b/ShhShell/Views/Hosts/ConnectionView.swift @@ -146,7 +146,7 @@ Hostkey fingerprint is \(handler.getHostkey() ?? "nil") handler.go() showTerminal = checkShell(handler.state) if showTerminal { - hostsManager.history.append(handler.host) + hostsManager.addToHistory(handler.host) handler.writeToChannel(hostsManager.snippets.first(where: { $0.id == handler.host.startupSnippetID })?.content) } } label: { diff --git a/ShhShell/Views/Hosts/RecentsView.swift b/ShhShell/Views/Hosts/RecentsView.swift index d4a6493..46e42f3 100644 --- a/ShhShell/Views/Hosts/RecentsView.swift +++ b/ShhShell/Views/Hosts/RecentsView.swift @@ -14,7 +14,7 @@ struct RecentsView: View { var body: some View { if !hostsManager.history.isEmpty { Section("Recents") { - ForEach(hostsManager.formatHistory()) { history in + ForEach(hostsManager.history) { history in NavigationLink() { ConnectionView( handler: SSHHandler(