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