Prepare airdrop support

This commit is contained in:
世界
2023-07-26 20:38:51 +08:00
parent 486a685bb5
commit 2bd462c789
52 changed files with 488 additions and 455 deletions
@@ -16,9 +16,7 @@ public struct ActiveDashboardView: View {
@State private var selectedProfileID: Int64!
@State private var reasserting = false
@State private var observer: Any?
@State private var errorPresented = false
@State private var errorMessage = ""
@State private var alert: Alert?
public init() {}
@@ -78,43 +76,37 @@ public struct ActiveDashboardView: View {
}
}
}
.alert(isPresented: $errorPresented) {
Alert(
title: Text("Error"),
message: Text(errorMessage),
dismissButton: .default(Text("Ok"))
)
}
.alertBinding($alert)
#if os(iOS)
.onChange(of: scenePhase, perform: { newValue in
if newValue == .active {
Task.detached {
await doReload()
}
}
})
.onChange(of: selection.wrappedValue, perform: { newValue in
if newValue == .dashboard {
Task.detached {
await doReload()
}
}
})
#elseif os(macOS)
.onAppear {
if observer == nil {
observer = NotificationCenter.default.addObserver(forName: ActiveDashboardView.NotificationUpdateSelectedProfile, object: nil, queue: nil, using: { _ in
.onChange(of: scenePhase, perform: { newValue in
if newValue == .active {
Task.detached {
await doReload()
}
})
}
})
.onChange(of: selection.wrappedValue, perform: { newValue in
if newValue == .dashboard {
Task.detached {
await doReload()
}
}
})
#elseif os(macOS)
.onAppear {
if observer == nil {
observer = NotificationCenter.default.addObserver(forName: ActiveDashboardView.NotificationUpdateSelectedProfile, object: nil, queue: nil, using: { _ in
Task.detached {
await doReload()
}
})
}
}
}
.onDisappear {
if let observer {
NotificationCenter.default.removeObserver(observer)
.onDisappear {
if let observer {
NotificationCenter.default.removeObserver(observer)
}
}
}
#endif
}
@@ -132,8 +124,7 @@ public struct ActiveDashboardView: View {
do {
profileList = try ProfileManager.list()
} catch {
errorMessage = error.localizedDescription
errorPresented = true
alert = Alert(error)
return
}
if profileList.isEmpty {
@@ -158,8 +149,7 @@ public struct ActiveDashboardView: View {
do {
try LibboxNewStandaloneCommandClient(FilePath.sharedDirectory.relativePath)?.serviceReload()
} catch {
errorMessage = error.localizedDescription
errorPresented = true
alert = Alert(error)
}
}
reasserting = false
@@ -7,8 +7,7 @@ public struct ExtensionStatusView: View {
@State private var message: LibboxStatusMessage?
@State private var connectTask: Task<Void, Error>?
@State private var columnCount: Int = 4
@State private var errorPresented = false
@State private var errorMessage = ""
@State private var alert: Alert?
private let infoFont = Font.system(.caption, design: .monospaced)
@@ -67,13 +66,7 @@ public struct ExtensionStatusView: View {
}
commandClient = nil
}
.alert(isPresented: $errorPresented) {
Alert(
title: Text("Error"),
message: Text(errorMessage),
dismissButton: .default(Text("Ok"))
)
}
.alertBinding($alert)
}
private func doReload() {
@@ -125,8 +118,7 @@ public struct ExtensionStatusView: View {
do {
try LibboxNewStandaloneCommandClient(FilePath.sharedDirectory.relativePath)?.closeConnections()
} catch {
errorMessage = error.localizedDescription
errorPresented = true
alert = Alert(error)
}
}
@@ -4,8 +4,7 @@ import SwiftUI
public struct InstallProfileButton: View {
@Environment(\.extensionProfile) private var extensionProfile
@State private var errorPresented = false
@State private var errorMessage = ""
@State private var alert: Alert?
public init() {}
@@ -15,21 +14,14 @@ public struct InstallProfileButton: View {
await installProfile()
}
}
.alert(isPresented: $errorPresented) {
Alert(
title: Text("Error"),
message: Text(errorMessage),
dismissButton: .default(Text("Ok"))
)
}
.alertBinding($alert)
}
private func installProfile() async {
do {
try await ExtensionProfile.install()
} catch {
errorMessage = error.localizedDescription
errorPresented = true
alert = Alert(error)
}
}
}
@@ -4,8 +4,7 @@
import SwiftUI
public struct InstallSystemExtensionButton: View {
@State private var errorPresented = false
@State private var errorMessage = ""
@State private var alert: Alert?
private let callback: () -> Void
public init(_ callback: @escaping () -> Void) {
self.callback = callback
@@ -17,27 +16,19 @@
await installSystemExtension()
}
}
.alert(isPresented: $errorPresented) {
Alert(
title: Text("Error"),
message: Text(errorMessage),
dismissButton: .default(Text("Ok"))
)
}
.alertBinding($alert)
}
private func installSystemExtension() async {
do {
if let result = try await SystemExtension.install() {
if result == .willCompleteAfterReboot {
errorMessage = "Need reboot"
errorPresented = true
alert = Alert(errorMessage: "Need Reboot")
}
}
callback()
} catch {
errorMessage = error.localizedDescription
errorPresented = true
alert = Alert(error)
}
}
}
@@ -41,8 +41,7 @@ public struct StartStopButton: View {
private struct Button0: View {
@Environment(\.logClient) private var logClient
@ObservedObject private var profile: ExtensionProfile
@State private var errorPresented = false
@State private var errorMessage = ""
@State private var alert: Alert?
init(_ profile: ExtensionProfile) {
self.profile = profile
@@ -75,13 +74,7 @@ public struct StartStopButton: View {
#endif
}
.disabled(!profile.status.isEnabled)
.alert(isPresented: $errorPresented) {
Alert(
title: Text("Error"),
message: Text(errorMessage),
dismissButton: .default(Text("Ok"))
)
}
.alertBinding($alert)
}
private func switchProfile(_ isEnabled: Bool) async {
@@ -93,8 +86,7 @@ public struct StartStopButton: View {
profile.stop()
}
} catch {
errorMessage = error.localizedDescription
errorPresented = true
alert = Alert(error)
return
}
}