Minor fixes for tvOS

This commit is contained in:
世界
2023-10-01 20:31:03 +08:00
parent 495b7d8805
commit 4a0d683d7c
5 changed files with 47 additions and 17 deletions
@@ -73,7 +73,9 @@ public struct DashboardView: View {
DashboardView1().environmentObject(profile)
} else {
FormView {
InstallProfileButton()
InstallProfileButton {
await environments.reload()
}
}
}
}
@@ -5,7 +5,10 @@ import SwiftUI
public struct InstallProfileButton: View {
@State private var alert: Alert?
public init() {}
private let callback: () async -> Void
public init(_ callback: @escaping (() async -> Void)) {
self.callback = callback
}
public var body: some View {
Button("Install NetworkExtension") {
@@ -19,6 +22,7 @@ public struct InstallProfileButton: View {
private func installProfile() async {
do {
try await ExtensionProfile.install()
await callback()
} catch {
alert = Alert(error)
}
@@ -14,6 +14,7 @@
@State private var alert: Alert?
@State private var connection: NWSocket?
@State private var profiles: [LibboxProfilePreview]?
@State private var isImporting = false
private let callback: () async -> Void
public init(callback: @escaping () async -> Void) {
@@ -47,7 +48,7 @@
selectProfile(profileID: profile.profileID)
isLoading = false
}
}.disabled(isLoading)
}.disabled(isLoading || isImporting)
}
}
} else {
@@ -76,12 +77,17 @@
}
}
private func loopMessages() async throws {
guard let connection else {
private nonisolated func loopMessages() async throws {
guard let connection = await connection else {
return
}
var message: Data
while true {
let message = try connection.read()
do {
message = try connection.read()
} catch {
throw NSError(domain: "read from connection: \(error.localizedDescription)", code: 0)
}
var error: NSError?
switch Int64(message[0]) {
case LibboxMessageTypeError:
@@ -105,7 +111,10 @@
}
profiles.append(profile)
}
await MainActor.run { [self, profiles] in
self.profiles = profiles
isImporting = false
}
case LibboxMessageTypeProfileContent:
let content = LibboxDecodeProfileContent(message, &error)
if let error {
@@ -126,6 +135,7 @@
request.profileID = profileID
do {
try connection.write(request.encode())
isImporting = true
} catch {
alert = Alert(error)
reset()
@@ -166,12 +166,30 @@ public struct ProfileView: View {
}
}
}
#elseif os(iOS)
#endif
#if os(iOS)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton().disabled(profileList.isEmpty)
}
}
#elseif os(tvOS)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
if editMode == .inactive {
Button("Edit") {
editMode = .active
}
.disabled(profileList.isEmpty)
} else {
Button("Done") {
editMode = .inactive
}
}
}
}
#endif
#if os(iOS) || os(tvOS)
.environment(\.editMode, $editMode)
#endif
}
+1 -5
View File
@@ -17,22 +17,18 @@ public class ExtensionEnvironments: ObservableObject {
}
}
public nonisolated func reload() async {
public func reload() async {
if let newProfile = try? await ExtensionProfile.load() {
if extensionProfile == nil || extensionProfile?.status == .invalid {
newProfile.register()
await MainActor.run {
extensionProfile = newProfile
extensionProfileLoading = false
}
}
} else {
await MainActor.run {
extensionProfile = nil
extensionProfileLoading = false
}
}
}
public func connectLog() {
guard let profile = extensionProfile else {