add addtohistory function

update formathistory
made history have an array of history
made ts codabel
This commit is contained in:
neon443
2025-08-15 13:16:41 +01:00
parent 3e713b8561
commit 4eb9cbb842
4 changed files with 16 additions and 11 deletions

View File

@@ -7,7 +7,7 @@
import Foundation
struct History: Identifiable {
struct History: Identifiable, Codable {
var id: UUID = UUID()
var host: Host

View File

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

View File

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

View File

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