refactor: Fix macOS standalone application
This commit is contained in:
@@ -495,10 +495,10 @@ extension ProfileCard {
|
||||
@Published var profileToEdit: Profile?
|
||||
@Published var shareItemType: ShareItemType?
|
||||
#if !os(tvOS)
|
||||
@Published var profileExportDocument: ProfileExportDocument?
|
||||
@Published var showProfileExporter = false
|
||||
@Published var profileJSONExportDocument: ProfileJSONExportDocument?
|
||||
@Published var showJSONExporter = false
|
||||
@Published var profileExportDocument: ProfileExportDocument?
|
||||
@Published var showProfileExporter = false
|
||||
@Published var profileJSONExportDocument: ProfileJSONExportDocument?
|
||||
@Published var showJSONExporter = false
|
||||
#endif
|
||||
#if os(macOS)
|
||||
var shareButtonView: NSView?
|
||||
@@ -511,10 +511,7 @@ extension ProfileCard {
|
||||
try await profile.updateRemoteProfile()
|
||||
environments.profileUpdate.send()
|
||||
} catch {
|
||||
alert = AlertState(
|
||||
title: String(localized: "Update Failed"),
|
||||
message: error.localizedDescription
|
||||
)
|
||||
alert = AlertState(error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,10 +402,7 @@ struct ProfilePickerSheet: View {
|
||||
try await profile.origin.updateRemoteProfile()
|
||||
environments.profileUpdate.send()
|
||||
} catch {
|
||||
alert = AlertState(
|
||||
title: String(localized: "Update Failed"),
|
||||
message: error.localizedDescription
|
||||
)
|
||||
alert = AlertState(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,8 @@ import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct DashboardView: View {
|
||||
@Environment(\.openURL) private var openURL
|
||||
@Environment(\.importProfile) private var importProfile
|
||||
@Environment(\.importRemoteProfile) private var importRemoteProfile
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@StateObject private var coordinator = DashboardViewModel()
|
||||
@State private var importRemoteProfileRequest: NewProfileView.ImportRequest?
|
||||
|
||||
#if os(macOS)
|
||||
@Environment(\.controlActiveState) private var controlActiveState
|
||||
@@ -19,24 +15,16 @@ public struct DashboardView: View {
|
||||
|
||||
public var body: some View {
|
||||
content
|
||||
.alert($coordinator.alert)
|
||||
.onAppear {
|
||||
coordinator.setOpenURL { openURL($0) }
|
||||
coordinator.setEnvironments(environments)
|
||||
#if os(macOS)
|
||||
Task { await coordinator.reload() }
|
||||
#endif
|
||||
handleImportProfile()
|
||||
handleImportRemoteProfile()
|
||||
}
|
||||
.onChangeCompat(of: importProfile.wrappedValue) { _ in
|
||||
handleImportProfile()
|
||||
}
|
||||
.onChangeCompat(of: importRemoteProfile.wrappedValue) { _ in
|
||||
handleImportRemoteProfile()
|
||||
}
|
||||
#if os(tvOS)
|
||||
.navigationDestination(item: $importRemoteProfileRequest) { request in
|
||||
NewProfileView(request)
|
||||
.navigationDestination(item: $environments.pendingImportRemoteProfile) { request in
|
||||
NewProfileView(.init(name: request.name, url: request.url))
|
||||
.environmentObject(environments)
|
||||
.onDisappear {
|
||||
environments.profileUpdate.send()
|
||||
@@ -48,7 +36,7 @@ public struct DashboardView: View {
|
||||
}
|
||||
}
|
||||
#else
|
||||
.sheet(item: $importRemoteProfileRequest) { request in
|
||||
.sheet(item: $environments.pendingImportRemoteProfile) { request in
|
||||
importRemoteProfileSheet(for: request)
|
||||
}
|
||||
#endif
|
||||
@@ -60,48 +48,12 @@ public struct DashboardView: View {
|
||||
#endif
|
||||
}
|
||||
|
||||
private func handleImportProfile() {
|
||||
if let profile = importProfile.wrappedValue {
|
||||
importProfile.wrappedValue = nil
|
||||
coordinator.alert = AlertState(
|
||||
title: String(localized: "Import Profile"),
|
||||
message: String(localized: "Are you sure to import profile \(profile.name)?"),
|
||||
primaryButton: .default(String(localized: "Import")) {
|
||||
Task {
|
||||
do {
|
||||
try await profile.importProfile()
|
||||
} catch {
|
||||
coordinator.alert = AlertState(error: error)
|
||||
return
|
||||
}
|
||||
environments.profileUpdate.send()
|
||||
}
|
||||
},
|
||||
secondaryButton: .cancel()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleImportRemoteProfile() {
|
||||
if let remoteProfile = importRemoteProfile.wrappedValue {
|
||||
importRemoteProfile.wrappedValue = nil
|
||||
coordinator.alert = AlertState(
|
||||
title: String(localized: "Import Remote Profile"),
|
||||
message: String(localized: "Are you sure to import remote profile \(remoteProfile.name)? You will connect to \(remoteProfile.host) to download the configuration."),
|
||||
primaryButton: .default(String(localized: "Import")) {
|
||||
importRemoteProfileRequest = .init(name: remoteProfile.name, url: remoteProfile.url)
|
||||
},
|
||||
secondaryButton: .cancel()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func importRemoteProfileSheet(for request: NewProfileView.ImportRequest) -> some View {
|
||||
private func importRemoteProfileSheet(for request: ImportRemoteProfileRequest) -> some View {
|
||||
NavigationSheet(title: "Import Profile", onDismiss: {
|
||||
environments.profileUpdate.send()
|
||||
}, content: {
|
||||
NewProfileView(request)
|
||||
NewProfileView(.init(name: request.name, url: request.url))
|
||||
.environmentObject(environments)
|
||||
})
|
||||
}
|
||||
@@ -132,9 +84,13 @@ public struct DashboardView: View {
|
||||
} else if let profile = environments.extensionProfile {
|
||||
activeDashboardView
|
||||
.environmentObject(profile)
|
||||
.alert($coordinator.alert)
|
||||
.onChangeCompat(of: profile.status) { status in
|
||||
coordinator.handleStatusChange(status, profile: profile)
|
||||
#if os(macOS)
|
||||
if Variant.useSystemExtension, status == .connected {
|
||||
UserServiceEndpointPublisher.shared.refreshEndpointRegistration()
|
||||
UserServiceEndpointPublisher.shared.checkExtensionRequirements()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
FormView {
|
||||
|
||||
@@ -2,12 +2,15 @@ import Foundation
|
||||
import Libbox
|
||||
import Library
|
||||
import NetworkExtension
|
||||
import os
|
||||
import SwiftUI
|
||||
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#endif
|
||||
|
||||
private let logger = Logger(category: "DashboardViewModel")
|
||||
|
||||
@MainActor
|
||||
public final class DashboardViewModel: BaseViewModel {
|
||||
@Published public var profileList: [ProfilePreview] = []
|
||||
@@ -15,14 +18,12 @@ public final class DashboardViewModel: BaseViewModel {
|
||||
@Published public var selection = DashboardPage.overview
|
||||
@Published public var systemProxyAvailable = false
|
||||
@Published public var systemProxyEnabled = false
|
||||
@Published public var notStarted = false
|
||||
|
||||
#if os(macOS)
|
||||
@Published public var systemExtensionInstalled = true
|
||||
#endif
|
||||
|
||||
private weak var environments: ExtensionEnvironments?
|
||||
private var openURL: ((URL) -> Void)?
|
||||
|
||||
public func setEnvironments(_ environments: ExtensionEnvironments) {
|
||||
self.environments = environments
|
||||
@@ -33,10 +34,6 @@ public final class DashboardViewModel: BaseViewModel {
|
||||
isLoading = true
|
||||
}
|
||||
|
||||
public func setOpenURL(_ openURL: @escaping (URL) -> Void) {
|
||||
self.openURL = openURL
|
||||
}
|
||||
|
||||
public func reload() async {
|
||||
#if os(macOS)
|
||||
if Variant.useSystemExtension {
|
||||
@@ -86,84 +83,13 @@ public final class DashboardViewModel: BaseViewModel {
|
||||
systemProxyAvailable = status.available
|
||||
systemProxyEnabled = status.enabled
|
||||
} catch {
|
||||
NSLog("reloadSystemProxy: \(error)")
|
||||
logger.debug("reloadSystemProxy: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
public func updateSelectedProfile() async {
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
}
|
||||
|
||||
public func handleStatusChange(_ status: NEVPNStatus, profile: ExtensionProfile) {
|
||||
if status == .connected {
|
||||
notStarted = false
|
||||
Task { 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 {
|
||||
let disableWarnings = await SharedPreferences.disableDeprecatedWarnings.get()
|
||||
guard !disableWarnings else { return }
|
||||
|
||||
do {
|
||||
let reports = try LibboxNewStandaloneCommandClient()!.getDeprecatedNotes()
|
||||
if reports.hasNext() {
|
||||
await MainActor.run {
|
||||
loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
NSLog("checkDeprecatedNotes: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
private func loopShowDeprecateNotes(_ reports: any LibboxDeprecatedNoteIteratorProtocol) {
|
||||
guard reports.hasNext() else { return }
|
||||
|
||||
let report = reports.next()!
|
||||
let continueChain: () -> Void = { [weak self] in
|
||||
_ = Task.detached {
|
||||
try? await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)
|
||||
await self?.loopShowDeprecateNotes(reports)
|
||||
}
|
||||
}
|
||||
|
||||
if report.migrationLink.isEmpty {
|
||||
alert = AlertState(
|
||||
title: String(localized: "Deprecated Warning"),
|
||||
message: report.message(),
|
||||
dismissButton: .cancel(String(localized: "Ok"))
|
||||
)
|
||||
alert?.onDismiss = continueChain
|
||||
} else {
|
||||
alert = AlertState(
|
||||
title: String(localized: "Deprecated Warning"),
|
||||
message: report.message(),
|
||||
primaryButton: .default(String(localized: "Documentation")) {
|
||||
self.openURL?(URL(string: report.migrationLink)!)
|
||||
},
|
||||
secondaryButton: .cancel(String(localized: "Ok")),
|
||||
onDismiss: continueChain
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 16.0, macOS 13.0, tvOS 17.0, *)
|
||||
nonisolated func checkLastDisconnectError(profile: ExtensionProfile) async {
|
||||
if let alertState = await profile.checkLastDisconnectError() {
|
||||
await MainActor.run {
|
||||
alert = alertState
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 16.0, macOS 13.0, tvOS 17.0, *)
|
||||
@@ -184,7 +110,7 @@ extension ExtensionProfile {
|
||||
)
|
||||
}
|
||||
#endif
|
||||
return AlertState(title: String(localized: "Service Error"), message: nsError.localizedDescription)
|
||||
return AlertState(error: nsError)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public final class OverviewViewModel: BaseViewModel {
|
||||
|
||||
if profile.status.isConnected {
|
||||
do {
|
||||
try await serviceReload()
|
||||
try await profile.reloadService()
|
||||
} catch {
|
||||
alert = AlertState(error: error)
|
||||
}
|
||||
@@ -21,10 +21,6 @@ public final class OverviewViewModel: BaseViewModel {
|
||||
reasserting = false
|
||||
}
|
||||
|
||||
public nonisolated func serviceReload() async throws {
|
||||
try LibboxNewStandaloneCommandClient()!.serviceReload()
|
||||
}
|
||||
|
||||
public nonisolated func setSystemProxyEnabled(_ enabled: Bool, profile: ExtensionProfile) async {
|
||||
do {
|
||||
await SharedPreferences.systemProxyEnabled.set(enabled)
|
||||
|
||||
Reference in New Issue
Block a user