Skip to content

Commit

Permalink
Allow selecting console item using notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Jul 3, 2024
1 parent e0bd05a commit c21442e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
15 changes: 2 additions & 13 deletions Sources/PulseUI/Features/Console/List/ConsoleListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,13 @@ private struct _ConsoleListView: View {
#if os(macOS)
@available(iOS 15, macOS 13, visionOS 1.0, *)
private struct _ConsoleListView: View {
@EnvironmentObject private var environment: ConsoleEnvironment
@EnvironmentObject private var listViewModel: ConsoleListViewModel
@EnvironmentObject private var searchViewModel: ConsoleSearchViewModel

@State private var selectedObjectID: NSManagedObjectID? // Has to use for Table
@State private var selection: ConsoleSelectedItem?
@State private var shareItems: ShareItems?
@EnvironmentObject private var router: ConsoleRouter

@Environment(\.isSearching) private var isSearching

var body: some View {
content
.onChange(of: selectedObjectID) {
environment.router.selection = $0.map(ConsoleSelectedItem.entity)
}
.onChange(of: selection) {
environment.router.selection = $0
}
.onChange(of: isSearching) {
searchViewModel.isSearchActive = $0
}
Expand All @@ -168,7 +157,7 @@ private struct _ConsoleListView: View {
}
Divider()
ScrollViewReader { proxy in
List(selection: $selection) {
List(selection: $router.selection) {
if isSearching && !searchViewModel.parameters.isEmpty {
ConsoleSearchResultsListContentView()
} else {
Expand Down
18 changes: 18 additions & 0 deletions Sources/PulseUI/Features/Console/Views/ConsoleRouterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import CoreData
import Pulse
import Combine

#if os(macOS)
public extension Notification.Name {
static let selectNetworkConsoleEntity = Notification.Name("PulseUI.Console.Router.Select.Item")
}
#endif

final class ConsoleRouter: ObservableObject {
#if os(macOS)
@Published var selection: ConsoleSelectedItem?
Expand All @@ -16,6 +22,18 @@ final class ConsoleRouter: ObservableObject {
@Published var isShowingSettings = false
@Published var isShowingSessions = false
@Published var isShowingShareStore = false

#if os(macOS)
init() {
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveSelectItemNotification), name: .selectNetworkConsoleEntity, object: nil)
}

@objc private func didReceiveSelectItemNotification(_ notification: Notification) {
guard let managedObjectID = notification.object as? NSManagedObjectID else { return }
selection = ConsoleSelectedItem.entity(managedObjectID)
}
#endif

}

#if os(macOS)
Expand Down

0 comments on commit c21442e

Please sign in to comment.