Add swiftlint & Minor fixes

This commit is contained in:
世界
2025-11-27 16:10:55 +08:00
parent 8855e0eef9
commit 0bfb6d2516
29 changed files with 382 additions and 196 deletions
@@ -24,13 +24,12 @@ public struct Connection: Codable {
public let outboundType: String
public let chain: [String]
var hashValue: Int {
var value = id.hashValue
(value, _) = value.addingReportingOverflow(upload.hashValue)
(value, _) = value.addingReportingOverflow(download.hashValue)
(value, _) = value.addingReportingOverflow(uploadTotal.hashValue)
(value, _) = value.addingReportingOverflow(downloadTotal.hashValue)
return value
func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(upload)
hasher.combine(download)
hasher.combine(uploadTotal)
hasher.combine(downloadTotal)
}
func performSearch(_ content: String) -> Bool {
@@ -17,7 +17,7 @@ public struct ConnectionListView: View {
} else {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible())], alignment: .leading) {
ForEach(viewModel.filteredConnections(), id: \.hashValue) { it in
ForEach(viewModel.filteredConnections(), id: \.id) { it in
ConnectionView(it)
}
}
@@ -56,9 +56,19 @@ public struct ConnectionListView: View {
#endif
.alertBinding($viewModel.alert)
.onAppear {
viewModel.setCommandClient(environments.commandClient)
viewModel.connect()
}
.onReceive(environments.commandClient.$connections) { connections in
viewModel.setConnections(connections)
}
.onChangeCompat(of: viewModel.connectionStateFilter) { filter in
environments.commandClient.connectionStateFilter = filter
environments.commandClient.filterConnectionsNow()
}
.onChangeCompat(of: viewModel.connectionSort) { sort in
environments.commandClient.connectionSort = sort
environments.commandClient.filterConnectionsNow()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
#if os(iOS)
.background(Color(uiColor: .systemGroupedBackground))
@@ -1,4 +1,3 @@
import Combine
import Libbox
import Library
import SwiftUI
@@ -11,8 +10,6 @@ public class ConnectionListViewModel: ObservableObject {
@Published public var alert: Alert?
@Published public var connectionStateFilter: ConnectionStateFilter {
didSet {
commandClient?.connectionStateFilter = connectionStateFilter
commandClient?.filterConnectionsNow()
saveStateFilterTask?.cancel()
saveStateFilterTask = Task {
await SharedPreferences.connectionStateFilter.set(connectionStateFilter.rawValue)
@@ -22,8 +19,6 @@ public class ConnectionListViewModel: ObservableObject {
@Published public var connectionSort: ConnectionSort {
didSet {
commandClient?.connectionSort = connectionSort
commandClient?.filterConnectionsNow()
saveSortTask?.cancel()
saveSortTask = Task {
await SharedPreferences.connectionSort.set(connectionSort.rawValue)
@@ -31,8 +26,6 @@ public class ConnectionListViewModel: ObservableObject {
}
}
private var commandClient: CommandClient?
private var cancellables = Set<AnyCancellable>()
private var connectTask: Task<Void, Never>?
private var saveStateFilterTask: Task<Void, Never>?
private var saveSortTask: Task<Void, Never>?
@@ -42,16 +35,6 @@ public class ConnectionListViewModel: ObservableObject {
connectionSort = .byDate
}
public func setCommandClient(_ client: CommandClient) {
commandClient = client
client.$connections
.compactMap { $0 }
.sink { [weak self] goConnections in
self?.setConnections(goConnections)
}
.store(in: &cancellables)
}
public func connect() {
if ApplicationLibrary.inPreview {
isLoading = false
@@ -97,7 +80,8 @@ public class ConnectionListViewModel: ObservableObject {
}
}
private func setConnections(_ goConnections: [LibboxConnection]) {
public func setConnections(_ goConnections: [LibboxConnection]?) {
guard let goConnections else { return }
connections = convertConnections(goConnections)
isLoading = false
}