解决网络资源的显示
This commit is contained in:
parent
08b2988b03
commit
d48c527326
@ -93,10 +93,10 @@ extension SDLAPIClient {
|
||||
|
||||
}
|
||||
|
||||
static func connectNetwork(networkSession: NetworkSession) async throws -> NetworkContext {
|
||||
static func connectNetwork(accesToken: String) async throws -> NetworkContext {
|
||||
let params: [String: Any] = [
|
||||
"client_id": SystemConfig.getClientId(),
|
||||
"access_token": networkSession.accessToken
|
||||
"access_token": accesToken
|
||||
]
|
||||
|
||||
return try await SDLAPIClient.doPost(path: "/connect", params: params, as: NetworkContext.self)
|
||||
|
||||
@ -68,6 +68,8 @@ class AppContext {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func loadCacheToken() -> String? {
|
||||
if let data = try? KeychainStore.shared.load(account: "token") {
|
||||
return String(data: data, encoding: .utf8)
|
||||
|
||||
@ -1,38 +1,17 @@
|
||||
//
|
||||
// NetworkState.swift
|
||||
// NetworkModel.swift
|
||||
// punchnet
|
||||
//
|
||||
// Created by 安礼成 on 2026/1/16.
|
||||
// Created by 安礼成 on 2026/3/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
@Observable
|
||||
class NetworkModel {
|
||||
|
||||
// 当前选中的设备
|
||||
var selectedNode: SDLAPIClient.NetworkContext.Node?
|
||||
var networkContext: SDLAPIClient.NetworkContext = .default()
|
||||
|
||||
init() {
|
||||
|
||||
func connectNetwork(accessToken: String) async throws {
|
||||
self.networkContext = try await SDLAPIClient.connectNetwork(accesToken: accessToken)
|
||||
}
|
||||
|
||||
func changeSelectedNode(nodeId: Int?) {
|
||||
if let nodeId {
|
||||
if let node = self.networkContext.nodeList.first(where: { $0.id == nodeId}) {
|
||||
self.selectedNode = node
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func connect(networkSession: SDLAPIClient.NetworkSession) async throws {
|
||||
let params: [String: Any] = [
|
||||
"client_id": SystemConfig.getClientId(),
|
||||
"access_token": networkSession.accessToken
|
||||
]
|
||||
self.networkContext = try await SDLAPIClient.connectNetwork(networkSession: networkSession)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -19,13 +19,15 @@ enum NetworkShowMode: String, CaseIterable {
|
||||
|
||||
// MARK: - 主网络视图
|
||||
struct NetworkView: View {
|
||||
@Environment(AppContext.self) var appContext
|
||||
@Environment(AppContext.self) var appContext: AppContext
|
||||
@Environment(\.openWindow) private var openWindow
|
||||
|
||||
@State private var networkModel = NetworkModel()
|
||||
@State private var showMode: NetworkShowMode = .resource
|
||||
@State private var connectState: ConnectState = .disconnected
|
||||
|
||||
// 多个View之间共享变量的最佳方式
|
||||
@State private var networkModel: NetworkModel = NetworkModel()
|
||||
|
||||
private var vpnManager = VPNManager.shared
|
||||
|
||||
var body: some View {
|
||||
@ -90,9 +92,9 @@ struct NetworkView: View {
|
||||
Group {
|
||||
switch connectState {
|
||||
case .waitAuth:
|
||||
NetworkWaitAuthView(networkModel: networkModel)
|
||||
NetworkWaitAuthView()
|
||||
case .connected:
|
||||
NetworkConnectedView(showMode: $showMode, networkModel: networkModel)
|
||||
NetworkConnectedView(showMode: $showMode)
|
||||
case .disconnected:
|
||||
NetworkDisconnectedView()
|
||||
}
|
||||
@ -101,6 +103,7 @@ struct NetworkView: View {
|
||||
.background(VisualEffectView(material: .windowBackground, blendingMode: .behindWindow))
|
||||
}
|
||||
.frame(minWidth: 700, minHeight: 500) // 适当调大宽度以适应 SplitView
|
||||
.environment(self.networkModel)
|
||||
.onAppear {
|
||||
syncState(vpnManager.vpnStatus)
|
||||
}
|
||||
@ -111,18 +114,23 @@ struct NetworkView: View {
|
||||
}
|
||||
}
|
||||
|
||||
// 通过VPN的连接状态同步当前页面的显示状态
|
||||
private func syncState(_ status: VPNManager.VPNStatus) {
|
||||
switch status {
|
||||
case .connected: connectState = .connected
|
||||
case .disconnected: connectState = .disconnected
|
||||
@unknown default: connectState = .disconnected
|
||||
case .connected:
|
||||
connectState = .connected
|
||||
case .disconnected:
|
||||
connectState = .disconnected
|
||||
@unknown default:
|
||||
connectState = .disconnected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct NetworkConnectedView: View {
|
||||
@Environment(NetworkModel.self) private var networkModel: NetworkModel
|
||||
@Binding var showMode: NetworkShowMode
|
||||
@Bindable var networkModel: NetworkModel
|
||||
|
||||
var body: some View {
|
||||
if showMode == .resource {
|
||||
@ -143,7 +151,7 @@ struct NetworkConnectedView: View {
|
||||
.frame(maxWidth: .infinity)
|
||||
} else {
|
||||
// 设备视图:双栏布局
|
||||
NetworkDeviceGroupView(networkModel: networkModel)
|
||||
NetworkDeviceGroupView()
|
||||
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .opacity))
|
||||
}
|
||||
}
|
||||
@ -151,8 +159,9 @@ struct NetworkConnectedView: View {
|
||||
}
|
||||
|
||||
struct NetworkDisconnectedView: View {
|
||||
@State private var isConnecting: Bool = false
|
||||
@Environment(AppContext.self) private var appContext: AppContext
|
||||
@Environment(NetworkModel.self) private var networkModel: NetworkModel
|
||||
@State private var isConnecting: Bool = false
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 20) {
|
||||
@ -196,11 +205,7 @@ struct NetworkDisconnectedView: View {
|
||||
return
|
||||
}
|
||||
|
||||
let context = try await SDLAPIClient.connectNetwork(networkSession: session)
|
||||
|
||||
// 登陆后需要保存到app的上线文
|
||||
self.appContext.networkContext = context
|
||||
|
||||
let context = try await SDLAPIClient.connectNetwork(accesToken: session.accessToken)
|
||||
if let options = SystemConfig.getOptions(
|
||||
networkId: UInt32(session.networkId),
|
||||
networkDomain: session.networkDomain,
|
||||
@ -212,6 +217,7 @@ struct NetworkDisconnectedView: View {
|
||||
noticePort: appContext.noticePort
|
||||
) {
|
||||
try await VPNManager.shared.enableVpn(options: options)
|
||||
self.networkModel.networkContext = context
|
||||
}
|
||||
} catch {
|
||||
print("Connection error: \(error)")
|
||||
@ -222,7 +228,7 @@ struct NetworkDisconnectedView: View {
|
||||
|
||||
// MARK: - 设备组视图 (NavigationSplitView)
|
||||
struct NetworkDeviceGroupView: View {
|
||||
@Bindable var networkModel: NetworkModel
|
||||
@Environment(NetworkModel.self) private var networkModel: NetworkModel
|
||||
@State private var selectedId: Int?
|
||||
|
||||
var body: some View {
|
||||
@ -365,8 +371,6 @@ struct ResourceItemCard: View {
|
||||
}
|
||||
|
||||
struct NetworkWaitAuthView: View {
|
||||
@Bindable var networkModel: NetworkModel
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 16) {
|
||||
ProgressView()
|
||||
|
||||
@ -23,14 +23,15 @@ struct SettingsDeviceView: View {
|
||||
.background(Color.blue.opacity(0.1))
|
||||
.cornerRadius(12)
|
||||
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(self.appContext.networkContext?.hostname ?? "未定义")
|
||||
.font(.title3.bold())
|
||||
|
||||
Text(SystemConfig.systemInfo)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
// TODO
|
||||
// VStack(alignment: .leading, spacing: 4) {
|
||||
// Text(self.appContext.networkContext?.hostname ?? "未定义")
|
||||
// .font(.title3.bold())
|
||||
//
|
||||
// Text(SystemConfig.systemInfo)
|
||||
// .font(.subheadline)
|
||||
// .foregroundColor(.secondary)
|
||||
// }
|
||||
}
|
||||
.padding(.horizontal, 4)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user