From 4a0d683d7cb46823ec0aec8b1906bcd76e7ade8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 1 Oct 2023 20:31:03 +0800 Subject: [PATCH] Minor fixes for tvOS --- .../Views/Dashboard/DashboardView.swift | 4 +++- .../Dashboard/InstallProfileButton.swift | 6 +++++- .../Views/Profile/ImportProfileView.swift | 20 ++++++++++++++----- .../Views/Profile/ProfileView.swift | 20 ++++++++++++++++++- Library/Network/ExtensionEnvironments.swift | 14 +++++-------- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/ApplicationLibrary/Views/Dashboard/DashboardView.swift b/ApplicationLibrary/Views/Dashboard/DashboardView.swift index e1f072c..321f9b9 100644 --- a/ApplicationLibrary/Views/Dashboard/DashboardView.swift +++ b/ApplicationLibrary/Views/Dashboard/DashboardView.swift @@ -73,7 +73,9 @@ public struct DashboardView: View { DashboardView1().environmentObject(profile) } else { FormView { - InstallProfileButton() + InstallProfileButton { + await environments.reload() + } } } } diff --git a/ApplicationLibrary/Views/Dashboard/InstallProfileButton.swift b/ApplicationLibrary/Views/Dashboard/InstallProfileButton.swift index f87cb95..d791e0e 100644 --- a/ApplicationLibrary/Views/Dashboard/InstallProfileButton.swift +++ b/ApplicationLibrary/Views/Dashboard/InstallProfileButton.swift @@ -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) } diff --git a/ApplicationLibrary/Views/Profile/ImportProfileView.swift b/ApplicationLibrary/Views/Profile/ImportProfileView.swift index bf94a4a..6073569 100644 --- a/ApplicationLibrary/Views/Profile/ImportProfileView.swift +++ b/ApplicationLibrary/Views/Profile/ImportProfileView.swift @@ -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) } - self.profiles = profiles + 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() diff --git a/ApplicationLibrary/Views/Profile/ProfileView.swift b/ApplicationLibrary/Views/Profile/ProfileView.swift index 7821605..b914b14 100644 --- a/ApplicationLibrary/Views/Profile/ProfileView.swift +++ b/ApplicationLibrary/Views/Profile/ProfileView.swift @@ -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 } diff --git a/Library/Network/ExtensionEnvironments.swift b/Library/Network/ExtensionEnvironments.swift index 07e9da8..c70aa4c 100644 --- a/Library/Network/ExtensionEnvironments.swift +++ b/Library/Network/ExtensionEnvironments.swift @@ -17,20 +17,16 @@ 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 + extensionProfile = newProfile extensionProfileLoading = false } + } else { + extensionProfile = nil + extensionProfileLoading = false } }