Fix preview

This commit is contained in:
世界
2023-07-30 15:35:10 +08:00
parent 2ca6c515b9
commit fed5419ea0
8 changed files with 94 additions and 73 deletions
@@ -79,48 +79,36 @@ public struct ActiveDashboardView: View {
}
}
.alertBinding($alert)
.onChangeCompat(of: profile.status) { newValue in
if newValue == .disconnecting || newValue == .connected {
Task.detached {
if let serviceError = try? String(contentsOf: ExtensionProvider.errorFile) {
DispatchQueue.main.async {
alert = Alert(errorMessage: serviceError)
}
try? FileManager.default.removeItem(at: ExtensionProvider.errorFile)
}
}
}
}
#if os(iOS) || os(tvOS)
.onChangeCompat(of: scenePhase) { newValue in
if newValue == .active {
Task.detached {
await doReload()
}
}
}
.onChangeCompat(of: selection.wrappedValue) { 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
.onChangeCompat(of: scenePhase) { newValue in
if newValue == .active {
Task.detached {
await doReload()
}
})
}
}
}
.onDisappear {
if let observer {
NotificationCenter.default.removeObserver(observer)
.onChangeCompat(of: selection.wrappedValue) { 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)
}
}
}
#endif
}
@@ -168,4 +156,15 @@ public struct ActiveDashboardView: View {
}
reasserting = false
}
private struct ServiceErrorReporter: View {
private let parent: ActiveDashboardView
init(_ parent: ActiveDashboardView) {
self.parent = parent
}
var body: some View {
EmptyView()
}
}
}
@@ -58,7 +58,7 @@ public struct DashboardView: View {
if ApplicationLibrary.inPreview {
ActiveDashboardView()
} else if let profile = extensionProfile.wrappedValue {
ActiveDashboardView().environmentObject(profile)
DashboardView1().environmentObject(profile)
} else {
FormView {
InstallProfileButton()
@@ -66,4 +66,28 @@ public struct DashboardView: View {
}
}
}
struct DashboardView1: View {
@EnvironmentObject private var profile: ExtensionProfile
@State private var alert: Alert?
var body: some View {
ActiveDashboardView()
.environmentObject(profile)
EmptyView()
.alertBinding($alert)
.onChangeCompat(of: profile.status) { newValue in
if newValue == .disconnecting || newValue == .connected {
Task.detached {
if let serviceError = try? String(contentsOf: ExtensionProvider.errorFile) {
DispatchQueue.main.async {
alert = Alert(errorMessage: serviceError)
}
try? FileManager.default.removeItem(at: ExtensionProvider.errorFile)
}
}
}
}
}
}
}
@@ -71,15 +71,15 @@ public extension NavigationPage {
}
func visible(_ profile: ExtensionProfile?) -> Bool {
if ApplicationLibrary.inPreview {
return true
}
switch self {
case .groups:
#if os(tvOS)
// TODO: fix groups ui
return false
#else
if ApplicationLibrary.inPreview {
return true
}
return profile?.status.isConnectedStrict == true
#endif
default:
@@ -66,7 +66,7 @@ public struct ProfileView: View {
.disabled(editMode.isEditing)
#endif
#if os(tvOS)
if devicePickerSupports(.applicationService(name: "sing-box"), parameters: { .applicationService }) {
if ApplicationLibrary.inPreview || devicePickerSupports(.applicationService(name: "sing-box"), parameters: { .applicationService }) {
NavigationLink {
ImportProfileView {
Task.detached {