Refactor to MVVM architecture
This commit is contained in:
@@ -9,20 +9,17 @@ public struct ActiveDashboardView: View {
|
||||
@Environment(\.selection) private var parentSelection
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@EnvironmentObject private var profile: ExtensionProfile
|
||||
@State private var isLoading = true
|
||||
@State private var profileList: [ProfilePreview] = []
|
||||
@State private var selectedProfileID: Int64 = 0
|
||||
@State private var alert: Alert?
|
||||
@State private var selection = DashboardPage.overview
|
||||
@State private var systemProxyAvailable = false
|
||||
@State private var systemProxyEnabled = false
|
||||
@StateObject private var viewModel = ActiveDashboardViewModel()
|
||||
|
||||
public init() {}
|
||||
public var body: some View {
|
||||
if isLoading {
|
||||
if viewModel.isLoading {
|
||||
ProgressView().onAppear {
|
||||
viewModel.onEmptyProfilesChange = { isEmpty in
|
||||
environments.emptyProfiles = isEmpty
|
||||
}
|
||||
Task {
|
||||
await doReload()
|
||||
await viewModel.reload()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -32,13 +29,13 @@ public struct ActiveDashboardView: View {
|
||||
body1
|
||||
.onAppear {
|
||||
Task {
|
||||
await doReloadSystemProxy()
|
||||
await viewModel.reloadSystemProxy()
|
||||
}
|
||||
}
|
||||
.onChangeCompat(of: profile.status) { newStatus in
|
||||
if newStatus == .connected {
|
||||
Task {
|
||||
await doReloadSystemProxy()
|
||||
await viewModel.reloadSystemProxy()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +47,7 @@ public struct ActiveDashboardView: View {
|
||||
VStack {
|
||||
#if os(iOS) || os(tvOS)
|
||||
if ApplicationLibrary.inPreview || profile.status.isConnectedStrict {
|
||||
Picker("Page", selection: $selection) {
|
||||
Picker("Page", selection: $viewModel.selection) {
|
||||
ForEach(DashboardPage.enabledCases()) { page in
|
||||
page.label
|
||||
}
|
||||
@@ -60,9 +57,9 @@ public struct ActiveDashboardView: View {
|
||||
.padding([.leading, .trailing])
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
#endif
|
||||
TabView(selection: $selection) {
|
||||
TabView(selection: $viewModel.selection) {
|
||||
ForEach(DashboardPage.enabledCases()) { page in
|
||||
page.contentView($profileList, $selectedProfileID, $systemProxyAvailable, $systemProxyEnabled)
|
||||
page.contentView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled)
|
||||
.tag(page)
|
||||
}
|
||||
}
|
||||
@@ -71,75 +68,25 @@ public struct ActiveDashboardView: View {
|
||||
#endif
|
||||
.tabViewStyle(.page(indexDisplayMode: .never))
|
||||
} else {
|
||||
OverviewView($profileList, $selectedProfileID, $systemProxyAvailable, $systemProxyEnabled)
|
||||
OverviewView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled)
|
||||
}
|
||||
#elseif os(macOS)
|
||||
OverviewView($profileList, $selectedProfileID, $systemProxyAvailable, $systemProxyEnabled)
|
||||
OverviewView($viewModel.profileList, $viewModel.selectedProfileID, $viewModel.systemProxyAvailable, $viewModel.systemProxyEnabled)
|
||||
#endif
|
||||
}
|
||||
.onReceive(environments.profileUpdate) { _ in
|
||||
Task {
|
||||
await doReload()
|
||||
await viewModel.reload()
|
||||
}
|
||||
}
|
||||
.onReceive(environments.selectedProfileUpdate) { _ in
|
||||
Task {
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
await viewModel.updateSelectedProfile()
|
||||
if profile.status.isConnected {
|
||||
await doReloadSystemProxy()
|
||||
await viewModel.reloadSystemProxy()
|
||||
}
|
||||
}
|
||||
}
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func doReload() async {
|
||||
defer {
|
||||
isLoading = false
|
||||
}
|
||||
if ApplicationLibrary.inPreview {
|
||||
profileList = [
|
||||
ProfilePreview(Profile(id: 0, name: "profile local", type: .local, path: "")),
|
||||
ProfilePreview(Profile(id: 1, name: "profile remote", type: .remote, path: "", lastUpdated: Date(timeIntervalSince1970: 0))),
|
||||
]
|
||||
systemProxyAvailable = true
|
||||
systemProxyEnabled = true
|
||||
selectedProfileID = 0
|
||||
|
||||
} else {
|
||||
do {
|
||||
profileList = try await ProfileManager.list().map { ProfilePreview($0) }
|
||||
if profileList.isEmpty {
|
||||
return
|
||||
}
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
if profileList.filter({ profile in
|
||||
profile.id == selectedProfileID
|
||||
})
|
||||
.isEmpty {
|
||||
selectedProfileID = profileList[0].id
|
||||
await SharedPreferences.selectedProfileID.set(selectedProfileID)
|
||||
}
|
||||
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
}
|
||||
environments.emptyProfiles = profileList.isEmpty
|
||||
}
|
||||
|
||||
private nonisolated func doReloadSystemProxy() async {
|
||||
do {
|
||||
let status = try LibboxNewStandaloneCommandClient()!.getSystemProxyStatus()
|
||||
await MainActor.run {
|
||||
systemProxyAvailable = status.available
|
||||
systemProxyEnabled = status.enabled
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
.alertBinding($viewModel.alert)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import Foundation
|
||||
import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
final class ActiveDashboardViewModel: ObservableObject {
|
||||
@Published var isLoading = true
|
||||
@Published var profileList: [ProfilePreview] = []
|
||||
@Published var selectedProfileID: Int64 = 0
|
||||
@Published var alert: Alert?
|
||||
@Published var selection = DashboardPage.overview
|
||||
@Published var systemProxyAvailable = false
|
||||
@Published var systemProxyEnabled = false
|
||||
|
||||
var onEmptyProfilesChange: ((Bool) -> Void)?
|
||||
|
||||
func reload() async {
|
||||
defer {
|
||||
isLoading = false
|
||||
}
|
||||
if ApplicationLibrary.inPreview {
|
||||
profileList = [
|
||||
ProfilePreview(Profile(id: 0, name: "profile local", type: .local, path: "")),
|
||||
ProfilePreview(Profile(id: 1, name: "profile remote", type: .remote, path: "", lastUpdated: Date(timeIntervalSince1970: 0))),
|
||||
]
|
||||
systemProxyAvailable = true
|
||||
systemProxyEnabled = true
|
||||
selectedProfileID = 0
|
||||
|
||||
} else {
|
||||
do {
|
||||
profileList = try await ProfileManager.list().map { ProfilePreview($0) }
|
||||
if profileList.isEmpty {
|
||||
onEmptyProfilesChange?(true)
|
||||
return
|
||||
}
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
if profileList.filter({ profile in
|
||||
profile.id == selectedProfileID
|
||||
})
|
||||
.isEmpty {
|
||||
selectedProfileID = profileList[0].id
|
||||
await SharedPreferences.selectedProfileID.set(selectedProfileID)
|
||||
}
|
||||
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
}
|
||||
onEmptyProfilesChange?(profileList.isEmpty)
|
||||
}
|
||||
|
||||
nonisolated func reloadSystemProxy() async {
|
||||
do {
|
||||
let status = try LibboxNewStandaloneCommandClient()!.getSystemProxyStatus()
|
||||
await MainActor.run {
|
||||
systemProxyAvailable = status.available
|
||||
systemProxyEnabled = status.enabled
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateSelectedProfile() async {
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
}
|
||||
}
|
||||
@@ -5,57 +5,38 @@ import SwiftUI
|
||||
@MainActor
|
||||
public struct ClashModeView: View {
|
||||
@Environment(\.scenePhase) private var scenePhase
|
||||
@StateObject private var commandClient = CommandClient(.clashMode)
|
||||
@State private var clashMode = ""
|
||||
@State private var alert: Alert?
|
||||
@StateObject private var viewModel = ClashModeViewModel()
|
||||
|
||||
public init() {}
|
||||
public var body: some View {
|
||||
VStack {
|
||||
if commandClient.clashModeList.count > 1 {
|
||||
if viewModel.shouldShowPicker {
|
||||
Picker("", selection: Binding(get: {
|
||||
clashMode
|
||||
viewModel.clashMode
|
||||
}, set: { newMode in
|
||||
clashMode = newMode
|
||||
viewModel.clashMode = newMode
|
||||
Task {
|
||||
await setClashMode(newMode)
|
||||
await viewModel.setClashMode(newMode)
|
||||
}
|
||||
}), content: {
|
||||
ForEach(commandClient.clashModeList, id: \.self) { it in
|
||||
Text(it)
|
||||
ForEach(viewModel.clashModeList, id: \.self) { mode in
|
||||
Text(mode)
|
||||
}
|
||||
})
|
||||
.pickerStyle(.segmented)
|
||||
.padding([.top], 8)
|
||||
}
|
||||
}
|
||||
.onReceive(commandClient.$clashMode) { newMode in
|
||||
clashMode = newMode
|
||||
}
|
||||
.padding([.leading, .trailing])
|
||||
.onAppear {
|
||||
commandClient.connect()
|
||||
viewModel.connect()
|
||||
}
|
||||
.onDisappear {
|
||||
commandClient.disconnect()
|
||||
viewModel.disconnect()
|
||||
}
|
||||
.onChangeCompat(of: scenePhase) { newValue in
|
||||
if newValue == .active {
|
||||
commandClient.connect()
|
||||
} else {
|
||||
commandClient.disconnect()
|
||||
}
|
||||
}
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private nonisolated func setClashMode(_ newMode: String) async {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.setClashMode(newMode)
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
viewModel.handleScenePhase(newValue)
|
||||
}
|
||||
.alertBinding($viewModel.alert)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
final class ClashModeViewModel: ObservableObject {
|
||||
@Published var clashMode = ""
|
||||
@Published var alert: Alert?
|
||||
|
||||
private let commandClient = CommandClient(.clashMode)
|
||||
|
||||
var clashModeList: [String] {
|
||||
commandClient.clashModeList
|
||||
}
|
||||
|
||||
var shouldShowPicker: Bool {
|
||||
commandClient.clashModeList.count > 1
|
||||
}
|
||||
|
||||
init() {
|
||||
commandClient.$clashMode
|
||||
.assign(to: &$clashMode)
|
||||
}
|
||||
|
||||
func connect() {
|
||||
commandClient.connect()
|
||||
}
|
||||
|
||||
func disconnect() {
|
||||
commandClient.disconnect()
|
||||
}
|
||||
|
||||
func handleScenePhase(_ phase: ScenePhase) {
|
||||
if phase == .active {
|
||||
connect()
|
||||
} else {
|
||||
disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated func setClashMode(_ newMode: String) async {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.setClashMode(newMode)
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import SwiftUI
|
||||
public struct DashboardView: View {
|
||||
#if os(macOS)
|
||||
@Environment(\.controlActiveState) private var controlActiveState
|
||||
@State private var isLoading = true
|
||||
@State private var systemExtensionInstalled = true
|
||||
@StateObject private var viewModel = DashboardViewModel()
|
||||
#endif
|
||||
|
||||
public init() {}
|
||||
@@ -16,10 +15,10 @@ public struct DashboardView: View {
|
||||
#if os(macOS)
|
||||
if Variant.useSystemExtension {
|
||||
viewBuilder {
|
||||
if !systemExtensionInstalled {
|
||||
if !viewModel.systemExtensionInstalled {
|
||||
FormView {
|
||||
InstallSystemExtensionButton {
|
||||
await reload()
|
||||
await viewModel.reload()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -27,7 +26,7 @@ public struct DashboardView: View {
|
||||
}
|
||||
}.onAppear {
|
||||
Task {
|
||||
await reload()
|
||||
await viewModel.reload()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -41,9 +40,9 @@ public struct DashboardView: View {
|
||||
.onChangeCompat(of: controlActiveState) { newValue in
|
||||
if newValue != .inactive {
|
||||
if Variant.useSystemExtension {
|
||||
if !isLoading {
|
||||
if !viewModel.isLoading {
|
||||
Task {
|
||||
await reload()
|
||||
await viewModel.reload()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,16 +51,6 @@ public struct DashboardView: View {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
private nonisolated func reload() async {
|
||||
let systemExtensionInstalled = await SystemExtension.isInstalled()
|
||||
await MainActor.run {
|
||||
self.systemExtensionInstalled = systemExtensionInstalled
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
struct DashboardView0: View {
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
|
||||
@@ -86,146 +75,21 @@ public struct DashboardView: View {
|
||||
@Environment(\.openURL) var openURL
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@EnvironmentObject private var profile: ExtensionProfile
|
||||
@State private var alert: Alert?
|
||||
@State private var notStarted = false
|
||||
@StateObject private var viewModel = DashboardViewModel()
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
ActiveDashboardView()
|
||||
}
|
||||
.alertBinding($alert)
|
||||
.alertBinding($viewModel.alert)
|
||||
.onAppear {
|
||||
viewModel.setOpenURL { url in
|
||||
openURL(url)
|
||||
}
|
||||
}
|
||||
.onChangeCompat(of: profile.status) { newValue in
|
||||
if newValue == .connected {
|
||||
notStarted = false
|
||||
}
|
||||
if newValue == .disconnecting || newValue == .connected {
|
||||
Task {
|
||||
await checkServiceError()
|
||||
if newValue == .connected {
|
||||
await checkDeprecatedNotes()
|
||||
}
|
||||
}
|
||||
} else if newValue == .connecting {
|
||||
notStarted = true
|
||||
} else if newValue == .disconnected {
|
||||
if #available(iOS 16.0, macOS 13.0, tvOS 17.0, *) {
|
||||
if notStarted {
|
||||
Task {
|
||||
await checkLastDisconnectError()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
viewModel.handleStatusChange(newValue, profile: profile)
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated func checkDeprecatedNotes() async {
|
||||
if await SharedPreferences.disableDeprecatedWarnings.get() {
|
||||
return
|
||||
}
|
||||
do {
|
||||
let reports = try LibboxNewStandaloneCommandClient()!.getDeprecatedNotes()
|
||||
if reports.hasNext() {
|
||||
await MainActor.run {
|
||||
loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func loopShowDeprecateNotes(_ reports: any LibboxDeprecatedNoteIteratorProtocol) {
|
||||
if reports.hasNext() {
|
||||
let report = reports.next()!
|
||||
if report.migrationLink.isEmpty {
|
||||
alert = Alert(
|
||||
title: Text("Deprecated Warning"),
|
||||
message: Text(report.message()),
|
||||
dismissButton: .cancel(Text("Ok")) {
|
||||
Task.detached {
|
||||
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
alert = Alert(
|
||||
title: Text("Deprecated Warning"),
|
||||
message: Text(report.message()),
|
||||
primaryButton: .default(Text("Documentation")) {
|
||||
openURL(URL(string: report.migrationLink)!)
|
||||
Task.detached {
|
||||
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await loopShowDeprecateNotes(reports)
|
||||
}
|
||||
},
|
||||
secondaryButton: .cancel(Text("Ok")) {
|
||||
Task.detached {
|
||||
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private 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, *)
|
||||
private nonisolated func checkLastDisconnectError() async {
|
||||
var myError: NSError
|
||||
do {
|
||||
try await profile.fetchLastDisconnectError()
|
||||
return
|
||||
} catch {
|
||||
myError = error as NSError
|
||||
}
|
||||
#if os(macOS)
|
||||
if myError.domain == "Library.FullDiskAccessPermissionRequired" {
|
||||
await MainActor.run {
|
||||
alert = Alert(
|
||||
title: Text("Full Disk Access permission is required"),
|
||||
message: Text("Please grant the permission for **SFMExtension**, then we can continue."),
|
||||
primaryButton: .default(Text("Authorize"), action: openFDASettings),
|
||||
secondaryButton: .cancel()
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
#endif
|
||||
let message = myError.localizedDescription
|
||||
await MainActor.run {
|
||||
alert = Alert(title: Text("Service Error"), message: Text(message))
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
|
||||
private func openFDASettings() {
|
||||
if NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles")!) {
|
||||
return
|
||||
}
|
||||
if #available(macOS 13, *) {
|
||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Applications/System Settings.app"))
|
||||
} else {
|
||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Applications/System Preferences.app"))
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
import Libbox
|
||||
import Library
|
||||
import NetworkExtension
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
class DashboardViewModel: ObservableObject {
|
||||
#if os(macOS)
|
||||
@Published var isLoading = true
|
||||
@Published var systemExtensionInstalled = true
|
||||
#endif
|
||||
|
||||
@Published var alert: Alert?
|
||||
@Published var notStarted = false
|
||||
|
||||
private var openURL: ((URL) -> Void)?
|
||||
|
||||
func setOpenURL(_ openURL: @escaping (URL) -> Void) {
|
||||
self.openURL = openURL
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
nonisolated func reload() async {
|
||||
let systemExtensionInstalled = await SystemExtension.isInstalled()
|
||||
await MainActor.run {
|
||||
self.systemExtensionInstalled = systemExtensionInstalled
|
||||
self.isLoading = false
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func handleStatusChange(_ status: NEVPNStatus, profile: ExtensionProfile) {
|
||||
if status == .connected {
|
||||
notStarted = false
|
||||
}
|
||||
if status == .disconnecting || status == .connected {
|
||||
Task {
|
||||
await checkServiceError()
|
||||
if status == .connected {
|
||||
await checkDeprecatedNotes()
|
||||
}
|
||||
}
|
||||
} else if status == .connecting {
|
||||
notStarted = true
|
||||
} else if status == .disconnected {
|
||||
if #available(iOS 16.0, macOS 13.0, tvOS 17.0, *) {
|
||||
if notStarted {
|
||||
Task {
|
||||
await checkLastDisconnectError(profile: profile)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated func checkDeprecatedNotes() async {
|
||||
if await SharedPreferences.disableDeprecatedWarnings.get() {
|
||||
return
|
||||
}
|
||||
do {
|
||||
let reports = try LibboxNewStandaloneCommandClient()!.getDeprecatedNotes()
|
||||
if reports.hasNext() {
|
||||
await MainActor.run {
|
||||
loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func loopShowDeprecateNotes(_ reports: any LibboxDeprecatedNoteIteratorProtocol) {
|
||||
if reports.hasNext() {
|
||||
let report = reports.next()!
|
||||
if report.migrationLink.isEmpty {
|
||||
alert = Alert(
|
||||
title: Text("Deprecated Warning"),
|
||||
message: Text(report.message()),
|
||||
dismissButton: .cancel(Text("Ok")) {
|
||||
Task.detached { [weak self] in
|
||||
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await self?.loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
alert = Alert(
|
||||
title: Text("Deprecated Warning"),
|
||||
message: Text(report.message()),
|
||||
primaryButton: .default(Text("Documentation")) {
|
||||
self.openURL?(URL(string: report.migrationLink)!)
|
||||
Task.detached { [weak self] in
|
||||
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await self?.loopShowDeprecateNotes(reports)
|
||||
}
|
||||
},
|
||||
secondaryButton: .cancel(Text("Ok")) {
|
||||
Task.detached { [weak self] in
|
||||
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await self?.loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
do {
|
||||
try await profile.fetchLastDisconnectError()
|
||||
return
|
||||
} catch {
|
||||
myError = error as NSError
|
||||
}
|
||||
#if os(macOS)
|
||||
if myError.domain == "Library.FullDiskAccessPermissionRequired" {
|
||||
await MainActor.run {
|
||||
alert = Alert(
|
||||
title: Text("Full Disk Access permission is required"),
|
||||
message: Text("Please grant the permission for **SFMExtension**, then we can continue."),
|
||||
primaryButton: .default(Text("Authorize"), action: openFDASettings),
|
||||
secondaryButton: .cancel()
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
#endif
|
||||
let message = myError.localizedDescription
|
||||
await MainActor.run {
|
||||
alert = Alert(title: Text("Service Error"), message: Text(message))
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
private func openFDASettings() {
|
||||
if NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles")!) {
|
||||
return
|
||||
}
|
||||
if #available(macOS 13, *) {
|
||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Applications/System Settings.app"))
|
||||
} else {
|
||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Applications/System Preferences.app"))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -12,14 +12,13 @@ public struct OverviewView: View {
|
||||
@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
|
||||
@StateObject private var viewModel = OverviewViewModel()
|
||||
|
||||
private var selectedProfileIDLocal: Binding<Int64> {
|
||||
$selectedProfileID.withSetter { newValue in
|
||||
reasserting = true
|
||||
viewModel.reasserting = true
|
||||
Task { [self] in
|
||||
await switchProfile(newValue)
|
||||
await viewModel.switchProfile(newValue, profile: profile, environments: environments)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +46,7 @@ public struct OverviewView: View {
|
||||
Toggle("HTTP Proxy", isOn: $systemProxyEnabled)
|
||||
.onChangeCompat(of: systemProxyEnabled) { newValue in
|
||||
Task {
|
||||
await setSystemProxyEnabled(newValue)
|
||||
await viewModel.setSystemProxyEnabled(newValue, profile: profile)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,7 +63,7 @@ public struct OverviewView: View {
|
||||
Toggle("HTTP Proxy", isOn: $systemProxyEnabled)
|
||||
.onChangeCompat(of: systemProxyEnabled) { newValue in
|
||||
Task {
|
||||
await setSystemProxyEnabled(newValue)
|
||||
await viewModel.setSystemProxyEnabled(newValue, profile: profile)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,55 +79,7 @@ public struct OverviewView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.alertBinding($alert)
|
||||
.disabled(!ApplicationLibrary.inPreview && (!profile.status.isSwitchable || reasserting))
|
||||
}
|
||||
|
||||
private func switchProfile(_ newProfileID: Int64) async {
|
||||
await SharedPreferences.selectedProfileID.set(newProfileID)
|
||||
environments.selectedProfileUpdate.send()
|
||||
if profile.status.isConnected {
|
||||
do {
|
||||
try await serviceReload()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
reasserting = false
|
||||
}
|
||||
|
||||
private nonisolated func serviceReload() async throws {
|
||||
try LibboxNewStandaloneCommandClient()!.serviceReload()
|
||||
}
|
||||
|
||||
private nonisolated func setSystemProxyEnabled(_ isEnabled: Bool) async {
|
||||
do {
|
||||
await SharedPreferences.systemProxyEnabled.set(isEnabled)
|
||||
if isEnabled {
|
||||
try LibboxNewStandaloneCommandClient()!.setSystemProxyEnabled(isEnabled)
|
||||
} else {
|
||||
// Apple BUG: HTTP Proxy cannot be disabled via setTunnelNetworkSettings, so we can only restart the Network Extension
|
||||
await MainActor.run {
|
||||
reasserting = true
|
||||
}
|
||||
try await profile.stop()
|
||||
var waitSeconds = 0
|
||||
while await profile.status != .disconnected {
|
||||
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
|
||||
waitSeconds += 1
|
||||
if waitSeconds >= 5 {
|
||||
throw NSError(domain: "Restart service timeout", code: 0)
|
||||
}
|
||||
}
|
||||
try await profile.start()
|
||||
await MainActor.run {
|
||||
reasserting = false
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
.alertBinding($viewModel.alert)
|
||||
.disabled(!ApplicationLibrary.inPreview && (!profile.status.isSwitchable || viewModel.reasserting))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import Foundation
|
||||
import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public final class OverviewViewModel: ObservableObject {
|
||||
@Published var alert: Alert?
|
||||
@Published var reasserting = false
|
||||
|
||||
public init() {}
|
||||
|
||||
func switchProfile(_ newProfileID: Int64, profile: ExtensionProfile, environments: ExtensionEnvironments) async {
|
||||
await SharedPreferences.selectedProfileID.set(newProfileID)
|
||||
environments.selectedProfileUpdate.send()
|
||||
if profile.status.isConnected {
|
||||
do {
|
||||
try await serviceReload()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
reasserting = false
|
||||
}
|
||||
|
||||
nonisolated func serviceReload() async throws {
|
||||
try LibboxNewStandaloneCommandClient()!.serviceReload()
|
||||
}
|
||||
|
||||
nonisolated func setSystemProxyEnabled(_ isEnabled: Bool, profile: ExtensionProfile) async {
|
||||
do {
|
||||
await SharedPreferences.systemProxyEnabled.set(isEnabled)
|
||||
if isEnabled {
|
||||
try LibboxNewStandaloneCommandClient()!.setSystemProxyEnabled(isEnabled)
|
||||
} else {
|
||||
// Apple BUG: HTTP Proxy cannot be disabled via setTunnelNetworkSettings, so we can only restart the Network Extension
|
||||
await MainActor.run {
|
||||
reasserting = true
|
||||
}
|
||||
try await profile.stop()
|
||||
var waitSeconds = 0
|
||||
while await profile.status != .disconnected {
|
||||
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
|
||||
waitSeconds += 1
|
||||
if waitSeconds >= 5 {
|
||||
throw NSError(domain: "Restart service timeout", code: 0)
|
||||
}
|
||||
}
|
||||
try await profile.start()
|
||||
await MainActor.run {
|
||||
reasserting = false
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user