Fix dashboard refresh
This commit is contained in:
@@ -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,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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user