added collapsible recents

added last connect date
added more and collapse button
added a thingy that says how many are displayed and the total
updated funcs
This commit is contained in:
neon443
2025-08-15 19:54:25 +01:00
parent 4eb9cbb842
commit d7f498c164
4 changed files with 57 additions and 9 deletions

View File

@@ -12,4 +12,5 @@ struct History: Identifiable, Codable {
var host: Host
var count: Int
var lastConnect: Date = .now
}

View File

@@ -47,6 +47,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
func addToHistory(_ host: Host) {
history.append(History(host: host, count: 1))
formatHistory()
saveHistory()
}
func formatHistory() {
@@ -55,6 +56,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
if result.last?.host == item.host {
guard var lastOne = result.popLast() else { continue }
lastOne.count += 1
lastOne.lastConnect = .now
result.append(lastOne)
} else {
result.append(History(host: item.host, count: 1))
@@ -68,7 +70,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
userDefaults.set(data, forKey: "history")
}
func removeFromHistory(_ toRemove: Host) {
func removeFromHistory(_ toRemove: History) {
history.removeAll(where: { $0.id == toRemove.id })
saveHistory()
}

View File

@@ -11,10 +11,13 @@ struct RecentsView: View {
@ObservedObject var hostsManager: HostsManager
@ObservedObject var keyManager: KeyManager
@State var historyCount: Int = 1
var body: some View {
if !hostsManager.history.isEmpty {
Section("Recents") {
ForEach(hostsManager.history) { history in
ForEach(0..<historyCount, id: \.self) { index in
let history = hostsManager.history[index]
NavigationLink() {
ConnectionView(
handler: SSHHandler(
@@ -25,16 +28,54 @@ struct RecentsView: View {
keyManager: keyManager
)
} label: {
Text(history.host.description)
Text("\(history.count)")
.foregroundStyle(.gray)
.padding(.trailing, 10)
.font(.caption)
VStack(alignment: .leading) {
Text(history.host.description)
.font(.body)
Text("Last connected at " + history.lastConnect.formatted())
.font(.caption)
.foregroundStyle(.gray)
}
}
.swipeActions {
Button("Remove", systemImage: "trash", role: .destructive) {
hostsManager.removeFromHistory(history.host)
hostsManager.removeFromHistory(history)
}
.tint(.red)
}
}
if historyCount != hostsManager.history.count {
HStack(alignment: .center) {
Button() {
withAnimation { historyCount += 2 }
} label: {
Image(systemName: "chevron.down")
.resizable().scaledToFit()
.frame(width: 20)
.foregroundStyle(hostsManager.tint)
}
.buttonStyle(.plain)
Spacer()
Text("\(historyCount)/\(hostsManager.history.count)")
.foregroundStyle(.gray)
.font(.caption)
.contentTransition(.numericText())
Spacer()
Button {
withAnimation { historyCount = 2 }
} label: {
Image(systemName: "chevron.up.2")
.resizable().scaledToFit()
.frame(width: 20)
.foregroundStyle(hostsManager.tint)
}
.buttonStyle(.plain)
}
}
}
}
}

View File

@@ -15,8 +15,10 @@ struct HostSymbolPreview: View {
var body: some View {
if small {
HStack(alignment: .center, spacing: 5) {
if !label.isEmpty {
Text(label)
.font(.headline)
}
symbol.image
.resizable().scaledToFit()
.symbolRenderingMode(.monochrome)
@@ -30,6 +32,7 @@ struct HostSymbolPreview: View {
symbol.image
.resizable().scaledToFit()
.symbolRenderingMode(.monochrome)
if !label.isEmpty {
Text(label)
.font(.headline)
.offset(symbol.offset)
@@ -37,6 +40,7 @@ struct HostSymbolPreview: View {
}
}
}
}
#Preview {
HostSymbolPreview(symbol: HostSymbol.desktopcomputer, label: "lo0")