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 import Foundation
struct History: Identifiable { struct History: Identifiable, Codable {
var id: UUID = UUID() var id: UUID = UUID()
var host: Host var host: Host

View File

@@ -24,7 +24,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
@Published var snippets: [Snippet] = [] @Published var snippets: [Snippet] = []
@Published var history: [Host] = [] @Published var history: [History] = []
var tint: SwiftUI.Color { var tint: SwiftUI.Color {
selectedTheme.ansi[selectedAnsi].suiColor selectedTheme.ansi[selectedAnsi].suiColor
@@ -40,22 +40,27 @@ class HostsManager: ObservableObject, @unchecked Sendable {
func loadHistory() { func loadHistory() {
guard let data = userDefaults.data(forKey: "history") else { return } 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 } withAnimation { self.history = decoded }
} }
func formatHistory() -> [History] { func addToHistory(_ host: Host) {
history.append(History(host: host, count: 1))
formatHistory()
}
func formatHistory() {
var result: [History] = [] var result: [History] = []
for host in history { for item in history {
if result.last?.host == host { if result.last?.host == item.host {
guard var lastOne = result.popLast() else { return result } guard var lastOne = result.popLast() else { continue }
lastOne.count += 1 lastOne.count += 1
result.append(lastOne) result.append(lastOne)
} else { } else {
result.append(History(host: host, count: 1)) result.append(History(host: item.host, count: 1))
} }
} }
return result withAnimation { self.history = result }
} }
func saveHistory() { func saveHistory() {

View File

@@ -146,7 +146,7 @@ Hostkey fingerprint is \(handler.getHostkey() ?? "nil")
handler.go() handler.go()
showTerminal = checkShell(handler.state) showTerminal = checkShell(handler.state)
if showTerminal { if showTerminal {
hostsManager.history.append(handler.host) hostsManager.addToHistory(handler.host)
handler.writeToChannel(hostsManager.snippets.first(where: { $0.id == handler.host.startupSnippetID })?.content) handler.writeToChannel(hostsManager.snippets.first(where: { $0.id == handler.host.startupSnippetID })?.content)
} }
} label: { } label: {

View File

@@ -14,7 +14,7 @@ struct RecentsView: View {
var body: some View { var body: some View {
if !hostsManager.history.isEmpty { if !hostsManager.history.isEmpty {
Section("Recents") { Section("Recents") {
ForEach(hostsManager.formatHistory()) { history in ForEach(hostsManager.history) { history in
NavigationLink() { NavigationLink() {
ConnectionView( ConnectionView(
handler: SSHHandler( handler: SSHHandler(