Add system proxy toggle

This commit is contained in:
世界
2023-09-03 22:59:51 +08:00
parent 4e5b251d6f
commit 30bff41517
7 changed files with 120 additions and 41 deletions
@@ -12,6 +12,8 @@ public struct ActiveDashboardView: View {
@State private var selectedProfileID: Int64!
@State private var alert: Alert?
@State private var selection = DashboardPage.overview
@State private var systemProxyAvailable = false
@State private var systemProxyEnabled = false
public init() {}
public var body: some View {
@@ -37,16 +39,16 @@ public struct ActiveDashboardView: View {
#endif
TabView(selection: $selection) {
ForEach(DashboardPage.allCases) { page in
page.contentView($profileList, $selectedProfileID)
page.contentView($profileList, $selectedProfileID, $systemProxyAvailable, $systemProxyEnabled)
.tag(page)
}
}
.tabViewStyle(.page(indexDisplayMode: .always))
} else {
OverviewView($profileList, $selectedProfileID)
OverviewView($profileList, $selectedProfileID, $systemProxyAvailable, $systemProxyEnabled)
}
#elseif os(macOS)
OverviewView($profileList, $selectedProfileID)
OverviewView($profileList, $selectedProfileID, $systemProxyAvailable, $systemProxyEnabled)
#endif
}
#if os(iOS) || os(tvOS)
@@ -65,6 +67,13 @@ public struct ActiveDashboardView: View {
}
}
#endif
.onChangeCompat(of: profile.status) { newStatus in
if newStatus == .connected {
Task.detached {
await doReloadSystemProxy()
}
}
}
.alertBinding($alert)
}
}
@@ -100,4 +109,14 @@ public struct ActiveDashboardView: View {
}
}
}
private func doReloadSystemProxy() {
do {
let status = try LibboxNewStandaloneCommandClient()!.getSystemProxyStatus()
systemProxyAvailable = status.available
systemProxyEnabled = status.enabled
} catch {
alert = Alert(error)
}
}
}
@@ -30,11 +30,11 @@ public extension DashboardPage {
}
}
func contentView(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>) -> some View {
func contentView(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>, _ systemProxyAvailable: Binding<Bool>, _ systemProxyEnabled: Binding<Bool>) -> some View {
viewBuilder {
switch self {
case .overview:
OverviewView(profileList, selectedProfileID)
OverviewView(profileList, selectedProfileID, systemProxyAvailable, systemProxyEnabled)
case .groups:
GroupListView()
}
@@ -10,13 +10,17 @@ public struct OverviewView: View {
@EnvironmentObject private var profile: ExtensionProfile
@Binding private var profileList: [Profile]
@Binding private var selectedProfileID: Int64!
@Binding private var systemProxyAvailable: Bool
@Binding private var systemProxyEnabled: Bool
@State private var alert: Alert?
@State private var reasserting = false
@State private var observer: Any?
public init(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>) {
public init(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>, _ systemProxyAvailable: Binding<Bool>, _ systemProxyEnabled: Binding<Bool>) {
_profileList = profileList
_selectedProfileID = selectedProfileID
_systemProxyAvailable = systemProxyAvailable
_systemProxyEnabled = systemProxyEnabled
}
public var body: some View {
@@ -31,6 +35,14 @@ public struct OverviewView: View {
FormView {
#if os(iOS) || os(tvOS)
StartStopButton()
if profile.status.isConnectedStrict, systemProxyAvailable {
Toggle("HTTP Proxy", isOn: $systemProxyEnabled)
.onChangeCompat(of: systemProxyEnabled) { newValue in
Task.detached {
await setSystemProxyEnabled(newValue)
}
}
}
Section("Profile") {
Picker(selection: $selectedProfileID) {
ForEach(profileList, id: \.id) { profile in
@@ -40,6 +52,14 @@ public struct OverviewView: View {
.pickerStyle(.inline)
}
#elseif os(macOS)
if profile.status.isConnectedStrict, systemProxyAvailable {
Toggle("HTTP Proxy", isOn: $systemProxyEnabled)
.onChangeCompat(of: systemProxyEnabled) { newValue in
Task.detached {
await setSystemProxyEnabled(newValue)
}
}
}
Section("Profile") {
ForEach(profileList, id: \.id) { profile in
Picker(profile.name, selection: $selectedProfileID) {
@@ -89,4 +109,13 @@ public struct OverviewView: View {
}
reasserting = false
}
private func setSystemProxyEnabled(_ isEnabled: Bool) {
do {
try LibboxNewStandaloneCommandClient()!.setSystemProxyEnabled(isEnabled)
SharedPreferences.systemProxyEnabled = isEnabled
} catch {
alert = Alert(error)
}
}
}