Fix performance for connection list

This commit is contained in:
世界
2025-12-06 19:37:21 +08:00
parent 2044535979
commit 5c7df0d608
4 changed files with 38 additions and 11 deletions
@@ -1,6 +1,6 @@
import Foundation import Foundation
public struct Connection: Codable { public struct Connection: Codable, Hashable, Equatable {
public let id: String public let id: String
public let inbound: String public let inbound: String
public let inboundType: String public let inboundType: String
@@ -24,12 +24,22 @@ public struct Connection: Codable {
public let outboundType: String public let outboundType: String
public let chain: [String] public let chain: [String]
func hash(into hasher: inout Hasher) { public func hash(into hasher: inout Hasher) {
hasher.combine(id) hasher.combine(id)
hasher.combine(upload) hasher.combine(upload)
hasher.combine(download) hasher.combine(download)
hasher.combine(uploadTotal) hasher.combine(uploadTotal)
hasher.combine(downloadTotal) hasher.combine(downloadTotal)
hasher.combine(closedAt)
}
public static func == (lhs: Connection, rhs: Connection) -> Bool {
lhs.id == rhs.id &&
lhs.upload == rhs.upload &&
lhs.download == rhs.download &&
lhs.uploadTotal == rhs.uploadTotal &&
lhs.downloadTotal == rhs.downloadTotal &&
lhs.closedAt == rhs.closedAt
} }
func performSearch(_ content: String) -> Bool { func performSearch(_ content: String) -> Bool {
@@ -16,8 +16,8 @@ public struct ConnectionListView: View {
Text("Empty connections") Text("Empty connections")
} else { } else {
ScrollView { ScrollView {
VStack { LazyVStack {
ForEach(viewModel.filteredConnections(), id: \.id) { it in ForEach(viewModel.filteredConnections, id: \.id) { it in
ConnectionView(it) ConnectionView(it)
} }
} }
@@ -4,8 +4,19 @@ import SwiftUI
@MainActor @MainActor
public class ConnectionListViewModel: BaseViewModel { public class ConnectionListViewModel: BaseViewModel {
@Published public var connections: [Connection] = [] @Published public var connections: [Connection] = [] {
@Published public var searchText = "" didSet {
updateFilteredConnections()
}
}
@Published public var searchText = "" {
didSet {
updateFilteredConnections()
}
}
@Published public var filteredConnections: [Connection] = []
@Published public var connectionStateFilter: ConnectionStateFilter { @Published public var connectionStateFilter: ConnectionStateFilter {
didSet { didSet {
saveStateFilterTask?.cancel() saveStateFilterTask?.cancel()
@@ -74,9 +85,11 @@ public class ConnectionListViewModel: BaseViewModel {
} }
} }
public func filteredConnections() -> [Connection] { private func updateFilteredConnections() {
connections.filter { connection in if searchText.isEmpty {
searchText == "" || connection.performSearch(searchText) filteredConnections = connections
} else {
filteredConnections = connections.filter { $0.performSearch(searchText) }
} }
} }
@@ -8,10 +8,14 @@ public struct ConnectionView: View {
self.connection = connection self.connection = connection
} }
private func format(_ date: Date) -> String { private static let timeFormatter: DateFormatter = {
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss" formatter.dateFormat = "HH:mm:ss"
return formatter.string(from: date) return formatter
}()
private func format(_ date: Date) -> String {
Self.timeFormatter.string(from: date)
} }
public func formatInterval(_ createdAt: Date, _ closedAt: Date) -> String { public func formatInterval(_ createdAt: Date, _ closedAt: Date) -> String {