Fix dashboard refresh

This commit is contained in:
世界
2025-11-26 22:25:48 +08:00
parent 437fe7ac31
commit f5a02d9da0
15 changed files with 32 additions and 65 deletions
@@ -3,7 +3,7 @@ import SwiftUI
@MainActor @MainActor
public struct ConnectionListView: View { public struct ConnectionListView: View {
@EnvironmentObject private var commandClient: CommandClient @EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = ConnectionListViewModel() @StateObject private var viewModel = ConnectionListViewModel()
public init() {} public init() {}
@@ -56,12 +56,9 @@ public struct ConnectionListView: View {
#endif #endif
.alertBinding($viewModel.alert) .alertBinding($viewModel.alert)
.onAppear { .onAppear {
viewModel.setCommandClient(commandClient) viewModel.setCommandClient(environments.commandClient)
viewModel.connect() viewModel.connect()
} }
.onDisappear {
viewModel.disconnect()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
#if os(iOS) #if os(iOS)
.background(Color(uiColor: .systemGroupedBackground)) .background(Color(uiColor: .systemGroupedBackground))
@@ -74,29 +74,21 @@ public struct ActiveDashboardView: View {
OverviewView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled) OverviewView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled)
#endif #endif
} }
.environmentObject(viewModel.dashboardClient)
.onAppear { .onAppear {
if ApplicationLibrary.inPreview || profile.status.isConnected { if ApplicationLibrary.inPreview {
viewModel.dashboardClient.connect() environments.commandClient.connect()
} else {
environments.connect()
} }
} }
.onDisappear {
viewModel.dashboardClient.disconnect()
}
.onChangeCompat(of: scenePhase) { newPhase in .onChangeCompat(of: scenePhase) { newPhase in
if newPhase == .active { if newPhase == .active {
if profile.status.isConnected { environments.connect()
viewModel.dashboardClient.connect()
}
} else {
viewModel.dashboardClient.disconnect()
} }
} }
.onChangeCompat(of: profile.status) { newStatus in .onChangeCompat(of: profile.status) { newStatus in
if newStatus.isConnected { if newStatus.isConnected {
viewModel.dashboardClient.connect() environments.connect()
} else {
viewModel.dashboardClient.disconnect()
} }
} }
.onReceive(environments.profileUpdate) { _ in .onReceive(environments.profileUpdate) { _ in
@@ -12,7 +12,6 @@ final class ActiveDashboardViewModel: ObservableObject {
@Published var selection = DashboardPage.overview @Published var selection = DashboardPage.overview
@Published var systemProxyAvailable = false @Published var systemProxyAvailable = false
@Published var systemProxyEnabled = false @Published var systemProxyEnabled = false
@Published var dashboardClient = CommandClient([.status, .groups, .clashMode, .connections])
var onEmptyProfilesChange: ((Bool) -> Void)? var onEmptyProfilesChange: ((Bool) -> Void)?
@@ -4,7 +4,7 @@ import SwiftUI
@MainActor @MainActor
public struct ClashModeView: View { public struct ClashModeView: View {
@EnvironmentObject private var commandClient: CommandClient @EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = ClashModeViewModel() @StateObject private var viewModel = ClashModeViewModel()
public init() {} public init() {}
@@ -29,7 +29,7 @@ public struct ClashModeView: View {
} }
.padding([.leading, .trailing]) .padding([.leading, .trailing])
.onAppear { .onAppear {
viewModel.setCommandClient(commandClient) viewModel.setCommandClient(environments.commandClient)
} }
.alertBinding($viewModel.alert) .alertBinding($viewModel.alert)
} }
@@ -32,13 +32,8 @@ class DashboardViewModel: ObservableObject {
func handleStatusChange(_ status: NEVPNStatus, profile: ExtensionProfile) { func handleStatusChange(_ status: NEVPNStatus, profile: ExtensionProfile) {
if status == .connected { if status == .connected {
notStarted = false notStarted = false
}
if status == .disconnecting || status == .connected {
Task { Task {
await checkServiceError() await checkDeprecatedNotes()
if status == .connected {
await checkDeprecatedNotes()
}
} }
} else if status == .connecting { } else if status == .connecting {
notStarted = true notStarted = true
@@ -107,17 +102,6 @@ class DashboardViewModel: ObservableObject {
} }
} }
nonisolated func checkServiceError() async {
var error: NSError?
let message = LibboxReadServiceError(&error)
if error != nil {
return
}
await MainActor.run {
alert = Alert(title: Text("Service Error"), message: Text(message!.value))
}
}
@available(iOS 16.0, macOS 13.0, tvOS 17.0, *) @available(iOS 16.0, macOS 13.0, tvOS 17.0, *)
nonisolated func checkLastDisconnectError(profile: ExtensionProfile) async { nonisolated func checkLastDisconnectError(profile: ExtensionProfile) async {
var myError: NSError var myError: NSError
@@ -34,6 +34,7 @@ public struct OverviewView: View {
VStack { VStack {
if ApplicationLibrary.inPreview || profile.status.isConnected { if ApplicationLibrary.inPreview || profile.status.isConnected {
ExtensionStatusView() ExtensionStatusView()
.environmentObject(environments.commandClient)
ClashModeView() ClashModeView()
} }
if profileList.isEmpty { if profileList.isEmpty {
@@ -78,7 +78,7 @@ public struct StartStopButton: View {
do { do {
if isEnabled { if isEnabled {
try await profile.start() try await profile.start()
await environments.logClient.connect() await environments.commandClient.connect()
} else { } else {
try await profile.stop() try await profile.stop()
} }
@@ -2,7 +2,7 @@ import Library
import SwiftUI import SwiftUI
public struct GroupListView: View { public struct GroupListView: View {
@EnvironmentObject private var commandClient: CommandClient @EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = GroupListViewModel() @StateObject private var viewModel = GroupListViewModel()
public init() {} public init() {}
@@ -23,7 +23,7 @@ public struct GroupListView: View {
} }
} }
.onAppear { .onAppear {
viewModel.setCommandClient(commandClient) viewModel.setCommandClient(environments.commandClient)
viewModel.connect() viewModel.connect()
} }
} }
+8 -8
View File
@@ -8,12 +8,12 @@ public struct LogView: View {
public init() {} public init() {}
public var body: some View { public var body: some View {
LogView0().environmentObject(environments.logClient) LogView0().environmentObject(environments.commandClient)
} }
private struct LogView0: View { private struct LogView0: View {
@EnvironmentObject private var environments: ExtensionEnvironments @EnvironmentObject private var environments: ExtensionEnvironments
@EnvironmentObject private var logClient: CommandClient @EnvironmentObject private var commandClient: CommandClient
private let logFont = Font.system(.caption2, design: .monospaced) private let logFont = Font.system(.caption2, design: .monospaced)
var body: some View { var body: some View {
@@ -44,13 +44,13 @@ public struct LogView: View {
.focusEffectDisabled() .focusEffectDisabled()
.focusSection() .focusSection()
#endif #endif
} else if logClient.logList.isEmpty { } else if commandClient.logList.isEmpty {
VStack { VStack {
if logClient.isConnected { if commandClient.isConnected {
Text("Empty logs") Text("Empty logs")
} else { } else {
Text("Service not started").onAppear { Text("Service not started").onAppear {
environments.connectLog() environments.connect()
} }
} }
} }
@@ -58,7 +58,7 @@ public struct LogView: View {
ScrollViewReader { reader in ScrollViewReader { reader in
ScrollView { ScrollView {
LazyVGrid(columns: [GridItem(.flexible())], alignment: .leading, spacing: 0) { LazyVGrid(columns: [GridItem(.flexible())], alignment: .leading, spacing: 0) {
ForEach(Array(logClient.logList.enumerated()), id: \.offset) { it in ForEach(Array(commandClient.logList.enumerated()), id: \.offset) { it in
Text(ANSIColors.parseAnsiString(it.element)) Text(ANSIColors.parseAnsiString(it.element))
.font(logFont) .font(logFont)
#if os(tvOS) #if os(tvOS)
@@ -67,7 +67,7 @@ public struct LogView: View {
Spacer(minLength: 8) Spacer(minLength: 8)
} }
.onChangeCompat(of: logClient.logList.count) { newCount in .onChangeCompat(of: commandClient.logList.count) { newCount in
withAnimation { withAnimation {
reader.scrollTo(newCount - 1) reader.scrollTo(newCount - 1)
} }
@@ -81,7 +81,7 @@ public struct LogView: View {
.focusSection() .focusSection()
#endif #endif
.onAppear { .onAppear {
reader.scrollTo(logClient.logList.count - 1) reader.scrollTo(commandClient.logList.count - 1)
} }
} }
} }
+1 -5
View File
@@ -117,11 +117,7 @@ public class CommandClient: ObservableObject {
clientOptions.addCommand(LibboxCommandConnections) clientOptions.addCommand(LibboxCommandConnections)
} }
} }
if connectionTypes.contains(.log) { clientOptions.statusInterval = Int64(NSEC_PER_SEC)
clientOptions.statusInterval = Int64(500 * NSEC_PER_MSEC)
} else {
clientOptions.statusInterval = Int64(NSEC_PER_SEC)
}
let client = LibboxNewCommandClient(clientHandler(self), clientOptions)! let client = LibboxNewCommandClient(clientHandler(self), clientOptions)!
do { do {
for i in 0 ..< 10 { for i in 0 ..< 10 {
+5 -5
View File
@@ -2,7 +2,7 @@ import Foundation
import SwiftUI import SwiftUI
public class ExtensionEnvironments: ObservableObject { public class ExtensionEnvironments: ObservableObject {
@Published public var logClient = CommandClient(.log) @Published public var commandClient = CommandClient([.log, .status, .groups, .clashMode, .connections])
@Published public var extensionProfileLoading = true @Published public var extensionProfileLoading = true
@Published public var extensionProfile: ExtensionProfile? @Published public var extensionProfile: ExtensionProfile?
@Published public var emptyProfiles = false @Published public var emptyProfiles = false
@@ -14,7 +14,7 @@ public class ExtensionEnvironments: ObservableObject {
public init() {} public init() {}
deinit { deinit {
logClient.disconnect() commandClient.disconnect()
} }
public func postReload() { public func postReload() {
@@ -37,12 +37,12 @@ public class ExtensionEnvironments: ObservableObject {
} }
} }
public func connectLog() { public func connect() {
guard let profile = extensionProfile else { guard let profile = extensionProfile else {
return return
} }
if profile.status.isConnected, !logClient.isConnected { if profile.status.isConnected, !commandClient.isConnected {
logClient.connect() commandClient.connect()
} }
} }
} }
-2
View File
@@ -14,8 +14,6 @@ open class ExtensionProvider: NEPacketTunnelProvider {
private var platformInterface: ExtensionPlatformInterface! private var platformInterface: ExtensionPlatformInterface!
override open func startTunnel(options _: [String: NSObject]?) async throws { override open func startTunnel(options _: [String: NSObject]?) async throws {
LibboxClearServiceError()
let options = LibboxSetupOptions() let options = LibboxSetupOptions()
options.basePath = FilePath.sharedDirectory.relativePath options.basePath = FilePath.sharedDirectory.relativePath
options.workingPath = FilePath.workingDirectory.relativePath options.workingPath = FilePath.workingDirectory.relativePath
+1 -1
View File
@@ -30,7 +30,7 @@ public class MainViewModel: ObservableObject {
public func onSelectionChange(_ newValue: NavigationPage, environments: ExtensionEnvironments) { public func onSelectionChange(_ newValue: NavigationPage, environments: ExtensionEnvironments) {
if newValue == .logs { if newValue == .logs {
environments.connectLog() environments.connect()
} }
} }
+1 -1
View File
@@ -42,7 +42,7 @@ struct MainView: View {
} }
.onChangeCompat(of: selection) { newValue in .onChangeCompat(of: selection) { newValue in
if newValue == .logs { if newValue == .logs {
environments.connectLog() environments.connect()
} }
} }
.environment(\.selection, $selection) .environment(\.selection, $selection)
+1 -1
View File
@@ -29,7 +29,7 @@ struct MainView: View {
} }
.onChangeCompat(of: selection) { newValue in .onChangeCompat(of: selection) { newValue in
if newValue == .logs { if newValue == .logs {
environments.connectLog() environments.connect()
} }
} }
.environment(\.selection, $selection) .environment(\.selection, $selection)