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
public struct ConnectionListView: View {
@EnvironmentObject private var commandClient: CommandClient
@EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = ConnectionListViewModel()
public init() {}
@@ -56,12 +56,9 @@ public struct ConnectionListView: View {
#endif
.alertBinding($viewModel.alert)
.onAppear {
viewModel.setCommandClient(commandClient)
viewModel.setCommandClient(environments.commandClient)
viewModel.connect()
}
.onDisappear {
viewModel.disconnect()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
#if os(iOS)
.background(Color(uiColor: .systemGroupedBackground))
@@ -74,29 +74,21 @@ public struct ActiveDashboardView: View {
OverviewView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled)
#endif
}
.environmentObject(viewModel.dashboardClient)
.onAppear {
if ApplicationLibrary.inPreview || profile.status.isConnected {
viewModel.dashboardClient.connect()
if ApplicationLibrary.inPreview {
environments.commandClient.connect()
} else {
environments.connect()
}
}
.onDisappear {
viewModel.dashboardClient.disconnect()
}
.onChangeCompat(of: scenePhase) { newPhase in
if newPhase == .active {
if profile.status.isConnected {
viewModel.dashboardClient.connect()
}
} else {
viewModel.dashboardClient.disconnect()
environments.connect()
}
}
.onChangeCompat(of: profile.status) { newStatus in
if newStatus.isConnected {
viewModel.dashboardClient.connect()
} else {
viewModel.dashboardClient.disconnect()
environments.connect()
}
}
.onReceive(environments.profileUpdate) { _ in
@@ -12,7 +12,6 @@ final class ActiveDashboardViewModel: ObservableObject {
@Published var selection = DashboardPage.overview
@Published var systemProxyAvailable = false
@Published var systemProxyEnabled = false
@Published var dashboardClient = CommandClient([.status, .groups, .clashMode, .connections])
var onEmptyProfilesChange: ((Bool) -> Void)?
@@ -4,7 +4,7 @@ import SwiftUI
@MainActor
public struct ClashModeView: View {
@EnvironmentObject private var commandClient: CommandClient
@EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = ClashModeViewModel()
public init() {}
@@ -29,7 +29,7 @@ public struct ClashModeView: View {
}
.padding([.leading, .trailing])
.onAppear {
viewModel.setCommandClient(commandClient)
viewModel.setCommandClient(environments.commandClient)
}
.alertBinding($viewModel.alert)
}
@@ -32,13 +32,8 @@ class DashboardViewModel: ObservableObject {
func handleStatusChange(_ status: NEVPNStatus, profile: ExtensionProfile) {
if status == .connected {
notStarted = false
}
if status == .disconnecting || status == .connected {
Task {
await checkServiceError()
if status == .connected {
await checkDeprecatedNotes()
}
await checkDeprecatedNotes()
}
} else if status == .connecting {
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, *)
nonisolated func checkLastDisconnectError(profile: ExtensionProfile) async {
var myError: NSError
@@ -34,6 +34,7 @@ public struct OverviewView: View {
VStack {
if ApplicationLibrary.inPreview || profile.status.isConnected {
ExtensionStatusView()
.environmentObject(environments.commandClient)
ClashModeView()
}
if profileList.isEmpty {
@@ -78,7 +78,7 @@ public struct StartStopButton: View {
do {
if isEnabled {
try await profile.start()
await environments.logClient.connect()
await environments.commandClient.connect()
} else {
try await profile.stop()
}
@@ -2,7 +2,7 @@ import Library
import SwiftUI
public struct GroupListView: View {
@EnvironmentObject private var commandClient: CommandClient
@EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel = GroupListViewModel()
public init() {}
@@ -23,7 +23,7 @@ public struct GroupListView: View {
}
}
.onAppear {
viewModel.setCommandClient(commandClient)
viewModel.setCommandClient(environments.commandClient)
viewModel.connect()
}
}
+8 -8
View File
@@ -8,12 +8,12 @@ public struct LogView: View {
public init() {}
public var body: some View {
LogView0().environmentObject(environments.logClient)
LogView0().environmentObject(environments.commandClient)
}
private struct LogView0: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@EnvironmentObject private var logClient: CommandClient
@EnvironmentObject private var commandClient: CommandClient
private let logFont = Font.system(.caption2, design: .monospaced)
var body: some View {
@@ -44,13 +44,13 @@ public struct LogView: View {
.focusEffectDisabled()
.focusSection()
#endif
} else if logClient.logList.isEmpty {
} else if commandClient.logList.isEmpty {
VStack {
if logClient.isConnected {
if commandClient.isConnected {
Text("Empty logs")
} else {
Text("Service not started").onAppear {
environments.connectLog()
environments.connect()
}
}
}
@@ -58,7 +58,7 @@ public struct LogView: View {
ScrollViewReader { reader in
ScrollView {
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))
.font(logFont)
#if os(tvOS)
@@ -67,7 +67,7 @@ public struct LogView: View {
Spacer(minLength: 8)
}
.onChangeCompat(of: logClient.logList.count) { newCount in
.onChangeCompat(of: commandClient.logList.count) { newCount in
withAnimation {
reader.scrollTo(newCount - 1)
}
@@ -81,7 +81,7 @@ public struct LogView: View {
.focusSection()
#endif
.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)
}
}
if connectionTypes.contains(.log) {
clientOptions.statusInterval = Int64(500 * NSEC_PER_MSEC)
} else {
clientOptions.statusInterval = Int64(NSEC_PER_SEC)
}
clientOptions.statusInterval = Int64(NSEC_PER_SEC)
let client = LibboxNewCommandClient(clientHandler(self), clientOptions)!
do {
for i in 0 ..< 10 {
+5 -5
View File
@@ -2,7 +2,7 @@ import Foundation
import SwiftUI
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 extensionProfile: ExtensionProfile?
@Published public var emptyProfiles = false
@@ -14,7 +14,7 @@ public class ExtensionEnvironments: ObservableObject {
public init() {}
deinit {
logClient.disconnect()
commandClient.disconnect()
}
public func postReload() {
@@ -37,12 +37,12 @@ public class ExtensionEnvironments: ObservableObject {
}
}
public func connectLog() {
public func connect() {
guard let profile = extensionProfile else {
return
}
if profile.status.isConnected, !logClient.isConnected {
logClient.connect()
if profile.status.isConnected, !commandClient.isConnected {
commandClient.connect()
}
}
}
-2
View File
@@ -14,8 +14,6 @@ open class ExtensionProvider: NEPacketTunnelProvider {
private var platformInterface: ExtensionPlatformInterface!
override open func startTunnel(options _: [String: NSObject]?) async throws {
LibboxClearServiceError()
let options = LibboxSetupOptions()
options.basePath = FilePath.sharedDirectory.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) {
if newValue == .logs {
environments.connectLog()
environments.connect()
}
}
+1 -1
View File
@@ -42,7 +42,7 @@ struct MainView: View {
}
.onChangeCompat(of: selection) { newValue in
if newValue == .logs {
environments.connectLog()
environments.connect()
}
}
.environment(\.selection, $selection)
+1 -1
View File
@@ -29,7 +29,7 @@ struct MainView: View {
}
.onChangeCompat(of: selection) { newValue in
if newValue == .logs {
environments.connectLog()
environments.connect()
}
}
.environment(\.selection, $selection)