Files
Pulse/Sources/PulseUI/Features/Console/Views/ConsoleRouterView.swift
2024-08-04 11:07:42 -04:00

156 lines
4.9 KiB
Swift

// The MIT License (MIT)
//
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).
import SwiftUI
import CoreData
import Pulse
import Combine
final class ConsoleRouter: ObservableObject {
#if os(macOS)
@Published var selection: ConsoleSelectedItem?
#endif
@Published var shareItems: ShareItems?
@Published var isShowingFilters = false
@Published var isShowingSettings = false
@Published var isShowingSessions = false
@Published var isShowingShareStore = false
}
#if os(macOS)
enum ConsoleSelectedItem: Hashable {
case entity(NSManagedObjectID)
case occurrence(NSManagedObjectID, ConsoleSearchOccurrence)
}
#endif
struct ConsoleRouterView: View {
@EnvironmentObject var environment: ConsoleEnvironment
@EnvironmentObject var router: ConsoleRouter
var body: some View {
if #available(iOS 15, macOS 13, *) {
contents
}
}
}
#if os(iOS) || os(visionOS)
@available(iOS 15, visionOS 1.0, *)
extension ConsoleRouterView {
var contents: some View {
Text("").invisible()
.sheet(isPresented: $router.isShowingFilters) { destinationFilters }
.sheet(isPresented: $router.isShowingSettings) { destinationSettings }
.sheet(isPresented: $router.isShowingSessions) { destinationSessions }
.sheet(isPresented: $router.isShowingShareStore) { destinationShareStore }
.sheet(item: $router.shareItems, content: ShareView.init)
}
private var destinationFilters: some View {
NavigationView {
ConsoleFiltersView()
.inlineNavigationTitle("Filters")
.navigationBarItems(trailing: Button("Done") {
router.isShowingFilters = false
})
.dynamicTypeSize(...DynamicTypeSize.xxxLarge)
}
}
@ViewBuilder
private var destinationSessions: some View {
NavigationView {
SessionsView()
.navigationTitle("Sessions")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
Button(action: { router.isShowingSessions = false }) {
Text("Close")
}
}
}
}
}
private var destinationSettings: some View {
NavigationView {
SettingsView(store: environment.store)
.navigationTitle("Settings")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(trailing: Button(action: { router.isShowingSettings = false }) {
Text("Done")
})
}
}
private var destinationShareStore: some View {
NavigationView {
ShareStoreView(onDismiss: { router.isShowingShareStore = false })
}.backport.presentationDetents([.medium, .large])
}
}
#elseif os(watchOS)
extension ConsoleRouterView {
var contents: some View {
Text("").invisible()
.sheet(isPresented: $router.isShowingSettings) {
NavigationView {
SettingsView(store: environment.store)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
router.isShowingSettings = false
}, label: {
Image(systemName: "xmark")
})
}
}
}
}
.sheet(isPresented: $router.isShowingFilters) {
NavigationView {
ConsoleFiltersView()
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
router.isShowingFilters = false
}, label: {
Image(systemName: "xmark")
})
}
}
}
}
}
}
#else
@available(macOS 13, *)
extension ConsoleRouterView {
var contents: some View {
Text("").invisible()
.sheet(isPresented: $router.isShowingSettings) { destinationSettings }
}
private var destinationSettings: some View {
SettingsView()
.frame(width: 320, height: UserSettings.shared.isRemoteLoggingHidden ? 175 : 420)
.navigationTitle("Settings")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: { router.isShowingSettings = false }) {
Text("Close")
}
}
}
}
}
#endif