Refactor task usage and profile auto update

This commit is contained in:
世界
2023-09-22 11:32:51 +08:00
parent 390e063f20
commit 3374f8e727
49 changed files with 823 additions and 636 deletions
+23 -12
View File
@@ -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" {