Minor fixes

This commit is contained in:
世界
2024-09-17 18:49:49 +08:00
parent 479bd593af
commit 8b5ad1928f
7 changed files with 86 additions and 33 deletions
@@ -7,20 +7,18 @@
@MainActor
public struct ImportProfileView: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@Environment(\.dismiss) private var dismiss
@State private var isLoading = false
@State private var selected = false
@State private var alert: Alert?
@State private var connection: NWSocket?
@State private var connection: NWConnection?
@State private var socket: NWSocket?
@State private var profiles: [LibboxProfilePreview]?
@State private var isImporting = false
private let callback: () async -> Void
public init(callback: @escaping () async -> Void) {
self.callback = callback
}
public init() {}
public var body: some View {
VStack(alignment: .center) {
if !selected {
@@ -73,17 +71,23 @@
}
private func reset() {
selected = false
profiles = nil
if let connection {
connection.stateUpdateHandler = nil
connection.cancel()
self.connection = nil
}
if let socket {
socket.cancel()
self.socket = nil
}
selected = false
profiles = nil
}
private func handleEndpoint(_ endpoint: NWEndpoint) async {
let connection = NWConnection(to: endpoint, using: NWParameters.applicationService)
self.connection = NWSocket(connection)
self.connection = connection
socket = NWSocket(connection)
connection.stateUpdateHandler = { state in
switch state {
case let .failed(error):
@@ -104,13 +108,13 @@
}
private nonisolated func loopMessages() async throws {
guard let connection = await connection else {
guard let socket = await socket else {
return
}
var message: Data
while true {
do {
message = try connection.read()
message = try socket.read()
} catch {
throw NSError(domain: "read from connection: \(error.localizedDescription)", code: 0)
}
@@ -147,6 +151,7 @@
throw error
}
try await importProfile(content!)
return
default:
throw NSError(domain: "unknown message type \(message[0])", code: 0)
}
@@ -157,10 +162,14 @@
guard let connection else {
return
}
guard let socket else {
return
}
connection.stateUpdateHandler = nil
let request = LibboxProfileContentRequest()
request.profileID = profileID
do {
try connection.write(request.encode())
try socket.write(request.encode())
isImporting = true
} catch {
alert = Alert(error)
@@ -191,8 +200,8 @@
}
try await ProfileManager.create(Profile(name: content.name, type: type, path: profileConfig.relativePath, remoteURL: content.remotePath, autoUpdate: content.autoUpdate, lastUpdated: lastUpdated))
await reset()
await callback()
await MainActor.run {
environments.profileUpdate.send()
dismiss()
}
}
@@ -66,9 +66,7 @@ public struct ProfileView: View {
}
if ApplicationLibrary.inPreview || devicePickerSupports(.applicationService(name: "sing-box"), parameters: { .applicationService }) {
FormNavigationLink {
ImportProfileView {
await doReload()
}
ImportProfileView()
} label: {
Text("Import Profile").foregroundColor(.accentColor)
}
@@ -188,15 +186,15 @@ public struct ProfileView: View {
}
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))),
]
} else {
defer {
isLoading = false
}
do {
profileList = try await ProfileManager.list().map { ProfilePreview($0) }
} catch {