diff --git a/punchnet/Networking/SDLAPIClient+Network.swift b/punchnet/Networking/SDLAPIClient+Network.swift index f8a4006..15f81d5 100644 --- a/punchnet/Networking/SDLAPIClient+Network.swift +++ b/punchnet/Networking/SDLAPIClient+Network.swift @@ -59,19 +59,6 @@ extension SDLAPIClient { } } - enum CodingKeys: String, CodingKey { - case ip - case maskLen = "mask_len" - case hostname - case identityId = "identity_id" - case resourceList = "resource_list" - case nodeList = "node_list" - } - - static func `default`() -> Self { - return .init(ip: "", maskLen: 24, hostname: "", identityId: 0, resourceList: [], nodeList: []) - } - // 节点详情 struct NodeDetail: Codable { let id: Int @@ -91,6 +78,26 @@ extension SDLAPIClient { } } + enum CodingKeys: String, CodingKey { + case ip + case maskLen = "mask_len" + case hostname + case identityId = "identity_id" + case resourceList = "resource_list" + case nodeList = "node_list" + } + + static func `default`() -> Self { + return .init(ip: "", maskLen: 24, hostname: "", identityId: 0, resourceList: [], nodeList: []) + } + + func getNode(id: Int?) -> Node? { + return nodeList.first(where: { $0.id == id }) + } + + func firstNodeId() -> Int? { + return nodeList.first?.id + } } static func connectNetwork(accesToken: String) async throws -> NetworkContext { diff --git a/punchnet/Views/Network/NetworkView.swift b/punchnet/Views/Network/NetworkView.swift index 56f3041..22fb0a4 100644 --- a/punchnet/Views/Network/NetworkView.swift +++ b/punchnet/Views/Network/NetworkView.swift @@ -218,7 +218,7 @@ struct NetworkDeviceGroupView: View { .listStyle(.sidebar) .navigationSplitViewColumnWidth(min: 200, ideal: 220) } detail: { - if let selectedNode = appContext.networkContext.nodeList.first(where: { $0.id == selectedId }) { + if let selectedNode = appContext.networkContext.getNode(id: selectedId) { NetworkNodeDetailView(node: selectedNode) } else { ContentUnavailableView("选择成员设备", systemImage: "macbook.and.iphone", description: Text("查看详细网络信息和服务")) @@ -226,7 +226,7 @@ struct NetworkDeviceGroupView: View { } .onAppear { if selectedId == nil { - selectedId = appContext.networkContext.nodeList.first?.id + selectedId = appContext.networkContext.firstNodeId() } } }