Refactor task usage and profile auto update
This commit is contained in:
@@ -12,17 +12,17 @@ open class ApplicationDelegate: NSObject, NSApplicationDelegate {
|
||||
let launchedAsLogInItem =
|
||||
event?.eventID == kAEOpenApplication &&
|
||||
event?.paramDescriptor(forKeyword: keyAEPropData)?.enumCodeValue == keyAELaunchedAsLogInItem
|
||||
if !launchedAsLogInItem || !SharedPreferences.showMenuBarExtra || !SharedPreferences.menuBarExtraInBackground {
|
||||
if !launchedAsLogInItem || !SharedPreferences.showMenuBarExtra.getBlocking() || !SharedPreferences.menuBarExtraInBackground.getBlocking() {
|
||||
NSApp.setActivationPolicy(.regular)
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
} else {
|
||||
NSApp.windows.first?.close()
|
||||
}
|
||||
Task.detached {
|
||||
Task {
|
||||
do {
|
||||
try ProfileUpdateTask.setup()
|
||||
try await ProfileUpdateTask.configure()
|
||||
if launchedAsLogInItem {
|
||||
if SharedPreferences.startedByUser {
|
||||
if await SharedPreferences.startedByUser.get() {
|
||||
if let profile = try await ExtensionProfile.load() {
|
||||
try await profile.start()
|
||||
}
|
||||
@@ -35,7 +35,7 @@ open class ApplicationDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
|
||||
public func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {
|
||||
!SharedPreferences.menuBarExtraInBackground
|
||||
!SharedPreferences.menuBarExtraInBackground.getBlocking()
|
||||
}
|
||||
|
||||
public func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
||||
|
||||
@@ -12,7 +12,7 @@ public struct MacApplication: Scene {
|
||||
Window("sing-box", id: "main", content: {
|
||||
MainView()
|
||||
.onAppear {
|
||||
Task.detached {
|
||||
Task {
|
||||
await initialize()
|
||||
}
|
||||
}
|
||||
@@ -61,26 +61,19 @@ public struct MacApplication: Scene {
|
||||
.menuBarExtraAccess(isPresented: $isMenuPresented)
|
||||
}
|
||||
|
||||
private func initialize() {
|
||||
let initialShowMenuBarExtra = SharedPreferences.showMenuBarExtra
|
||||
DispatchQueue.main.async {
|
||||
showMenuBarExtra = initialShowMenuBarExtra
|
||||
}
|
||||
private func initialize() async {
|
||||
showMenuBarExtra = await SharedPreferences.showMenuBarExtra.get()
|
||||
}
|
||||
|
||||
private func hide(closeApp: Bool) {
|
||||
Task.detached {
|
||||
if SharedPreferences.menuBarExtraInBackground {
|
||||
DispatchQueue.main.async {
|
||||
hide0(closeApp: closeApp)
|
||||
}
|
||||
Task {
|
||||
if await SharedPreferences.menuBarExtraInBackground.get() {
|
||||
hide0(closeApp: closeApp)
|
||||
} else {
|
||||
DispatchQueue.main.async {
|
||||
if closeApp {
|
||||
NSApp.terminate(nil)
|
||||
} else {
|
||||
NSApp.keyWindow?.close()
|
||||
}
|
||||
if closeApp {
|
||||
NSApp.terminate(nil)
|
||||
} else {
|
||||
NSApp.keyWindow?.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+23
-12
@@ -3,6 +3,7 @@ import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct MainView: View {
|
||||
@Environment(\.controlActiveState) private var controlActiveState
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
@@ -32,8 +33,8 @@ public struct MainView: View {
|
||||
environments.postReload()
|
||||
#if !DEBUG
|
||||
if Variant.useSystemExtension {
|
||||
Task.detached {
|
||||
await checkApplicationPath()
|
||||
Task {
|
||||
checkApplicationPath()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -73,22 +74,32 @@ public struct MainView: View {
|
||||
selection = .profiles
|
||||
}
|
||||
} else if url.pathExtension == "bpf" {
|
||||
do {
|
||||
_ = url.startAccessingSecurityScopedResource()
|
||||
importProfile = try .from(Data(contentsOf: url))
|
||||
url.stopAccessingSecurityScopedResource()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
if selection != .profiles {
|
||||
selection = .profiles
|
||||
Task {
|
||||
await importURLProfile(url)
|
||||
}
|
||||
} else {
|
||||
alert = Alert(errorMessage: "Handled unknown URL \(url.absoluteString)")
|
||||
}
|
||||
}
|
||||
|
||||
private func importURLProfile(_ url: URL) async {
|
||||
do {
|
||||
_ = url.startAccessingSecurityScopedResource()
|
||||
importProfile = try await .from(readURL(url))
|
||||
url.stopAccessingSecurityScopedResource()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
if selection != .profiles {
|
||||
selection = .profiles
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated func readURL(_ url: URL) async throws -> Data {
|
||||
try Data(contentsOf: url)
|
||||
}
|
||||
|
||||
private func checkApplicationPath() {
|
||||
let directoryName = URL(filePath: Bundle.main.bundlePath).deletingLastPathComponent().pathComponents.last
|
||||
if directoryName != "Applications" {
|
||||
|
||||
+16
-12
@@ -6,6 +6,7 @@ import MacControlCenterUI
|
||||
import MenuBarExtraAccess
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct MenuView: View {
|
||||
@Environment(\.openWindow) private var openWindow
|
||||
|
||||
@@ -25,7 +26,7 @@ public struct MenuView: View {
|
||||
MenuHeader("sing-box") {
|
||||
if isLoading {
|
||||
Text("Loading...").foregroundColor(.secondary).onAppear {
|
||||
Task.detached {
|
||||
Task {
|
||||
await loadProfile()
|
||||
}
|
||||
}
|
||||
@@ -81,7 +82,7 @@ public struct MenuView: View {
|
||||
Toggle(isOn: Binding(get: {
|
||||
profile.status.isConnected
|
||||
}, set: { _ in
|
||||
Task.detached {
|
||||
Task {
|
||||
await switchProfile(!profile.status.isConnected)
|
||||
}
|
||||
})) {}
|
||||
@@ -122,7 +123,7 @@ public struct MenuView: View {
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
@@ -139,7 +140,7 @@ public struct MenuView: View {
|
||||
.pickerStyle(.inline)
|
||||
.onChangeCompat(of: selectedProfileID) {
|
||||
reasserting = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await switchProfile(selectedProfileID!)
|
||||
}
|
||||
}
|
||||
@@ -162,12 +163,12 @@ public struct MenuView: View {
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func doReload() {
|
||||
private func doReload() async {
|
||||
defer {
|
||||
isLoading = false
|
||||
}
|
||||
do {
|
||||
profileList = try ProfileManager.list()
|
||||
profileList = try await ProfileManager.list()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
@@ -175,28 +176,31 @@ public struct MenuView: View {
|
||||
if profileList.isEmpty {
|
||||
return
|
||||
}
|
||||
|
||||
selectedProfileID = SharedPreferences.selectedProfileID
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
if profileList.filter({ profile in
|
||||
profile.id == selectedProfileID
|
||||
})
|
||||
.isEmpty {
|
||||
selectedProfileID = profileList[0].id!
|
||||
SharedPreferences.selectedProfileID = selectedProfileID
|
||||
await SharedPreferences.selectedProfileID.set(selectedProfileID)
|
||||
}
|
||||
}
|
||||
|
||||
private func switchProfile(_ newProfileID: Int64) {
|
||||
SharedPreferences.selectedProfileID = newProfileID
|
||||
private func switchProfile(_ newProfileID: Int64) async {
|
||||
await SharedPreferences.selectedProfileID.set(newProfileID)
|
||||
NotificationCenter.default.post(name: OverviewView.NotificationUpdateSelectedProfile, object: newProfileID)
|
||||
if profile.status.isConnected {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()?.serviceReload()
|
||||
try await serviceReload()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
reasserting = false
|
||||
}
|
||||
|
||||
private nonisolated func serviceReload() async throws {
|
||||
try LibboxNewStandaloneCommandClient()?.serviceReload()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user