Fix performance for connection list
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import Foundation
|
||||
|
||||
public struct Connection: Codable {
|
||||
public struct Connection: Codable, Hashable, Equatable {
|
||||
public let id: String
|
||||
public let inbound: String
|
||||
public let inboundType: String
|
||||
@@ -24,12 +24,22 @@ public struct Connection: Codable {
|
||||
public let outboundType: String
|
||||
public let chain: [String]
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(id)
|
||||
hasher.combine(upload)
|
||||
hasher.combine(download)
|
||||
hasher.combine(uploadTotal)
|
||||
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 {
|
||||
|
||||
@@ -16,8 +16,8 @@ public struct ConnectionListView: View {
|
||||
Text("Empty connections")
|
||||
} else {
|
||||
ScrollView {
|
||||
VStack {
|
||||
ForEach(viewModel.filteredConnections(), id: \.id) { it in
|
||||
LazyVStack {
|
||||
ForEach(viewModel.filteredConnections, id: \.id) { it in
|
||||
ConnectionView(it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,19 @@ import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public class ConnectionListViewModel: BaseViewModel {
|
||||
@Published public var connections: [Connection] = []
|
||||
@Published public var searchText = ""
|
||||
@Published public var connections: [Connection] = [] {
|
||||
didSet {
|
||||
updateFilteredConnections()
|
||||
}
|
||||
}
|
||||
|
||||
@Published public var searchText = "" {
|
||||
didSet {
|
||||
updateFilteredConnections()
|
||||
}
|
||||
}
|
||||
|
||||
@Published public var filteredConnections: [Connection] = []
|
||||
@Published public var connectionStateFilter: ConnectionStateFilter {
|
||||
didSet {
|
||||
saveStateFilterTask?.cancel()
|
||||
@@ -74,9 +85,11 @@ public class ConnectionListViewModel: BaseViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
public func filteredConnections() -> [Connection] {
|
||||
connections.filter { connection in
|
||||
searchText == "" || connection.performSearch(searchText)
|
||||
private func updateFilteredConnections() {
|
||||
if searchText.isEmpty {
|
||||
filteredConnections = connections
|
||||
} else {
|
||||
filteredConnections = connections.filter { $0.performSearch(searchText) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,14 @@ public struct ConnectionView: View {
|
||||
self.connection = connection
|
||||
}
|
||||
|
||||
private func format(_ date: Date) -> String {
|
||||
private static let timeFormatter: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user