Skip to content

Commit

Permalink
Merge pull request #152 from hayek/Accumulated-Labels-Hosts
Browse files Browse the repository at this point in the history
Fix Labels and Hosts filter selections.
  • Loading branch information
kean authored Jan 29, 2023
2 parents 904b235 + 086ee2b commit 86ba2b3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Sources/PulseUI/Features/Console/ConsoleView-tvos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ private struct ConsoleMenuView: View {
NavigationLink(destination: destinationStoreDetails) {
Label("Store Info", systemImage: "info.circle")
}
Button.destructive(action: store.removeAll) {
Button.destructive {
viewModel.accumulatedLabels = []
viewModel.accumulatedHosts = []
store.removeAll()
} label: {
Label("Remove Logs", systemImage: "trash")
}
} header: { Text("Store") }
Expand Down
2 changes: 2 additions & 0 deletions Sources/PulseUI/Features/Console/ConsoleViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ final class ConsoleViewModel: ObservableObject {

prepare(for: mode)
searchCriteriaViewModel.bind(list.$entities)
searchCriteriaViewModel.bind(accumulatedLabels: list.$accumulatedLabels)
searchCriteriaViewModel.bind(accumulatedHosts: list.$accumulatedHosts)
}

private func prepare(for mode: ConsoleMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ final class ConsoleListViewModel: NSObject, NSFetchedResultsControllerDelegate,
@Published private(set) var pins: [NSManagedObject] = []
@Published private(set) var entities: [NSManagedObject] = []
@Published private(set) var sections: [NSFetchedResultsSectionInfo]?
@Published private(set) var accumulatedLabels: Set<String> = []
@Published private(set) var accumulatedHosts: Set<String> = []
@Published var options = ConsoleListOptions()

let entitiesSubject = CurrentValueSubject<[NSManagedObject], Never>([])
Expand Down Expand Up @@ -251,6 +253,12 @@ final class ConsoleListViewModel: NSObject, NSFetchedResultsControllerDelegate,
}

private func reloadMessages(isMandatory: Bool = true) {
if let entities = entities as? [LoggerMessageEntity] {
accumulatedLabels = accumulatedLabels.union(Set(entities.map(\.label)))
}
if let entities = entities as? [NetworkTaskEntity] {
accumulatedHosts = accumulatedHosts.union(Set(entities.compactMap(\.host)))
}
entities = controller?.fetchedObjects ?? []
sections = controller?.sectionNameKeyPath == nil ? nil : controller?.sections
if isMandatory || scrollPosition == .nearTop {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ struct ConsoleContextMenu: View {

private func buttonRemoveAllTapped() {
viewModel.store.removeAll()


viewModel.searchCriteriaViewModel.accumulatedLabels = []
viewModel.searchCriteriaViewModel.accumulatedHosts = []

runHapticFeedback(.success)
ToastView {
HStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension ConsoleSearchCriteriaView {
}, content: {
ConsoleSearchListSelectionView(
title: "Labels",
items: viewModel.labels,
items: Array(viewModel.accumulatedLabels),
selection: $viewModel.selectedLabels,
description: { $0 },
label: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class ConsoleSearchCriteriaViewModel: ObservableObject {
private let store: LoggerStore
private var isScreenVisible = false
private var entities: [NSManagedObject] = []
public var accumulatedLabels: Set<String> = []
public var accumulatedHosts: Set<String> = []
private var cancellables: [AnyCancellable] = []

init(store: LoggerStore, source: ConsoleSource) {
Expand All @@ -51,6 +53,20 @@ final class ConsoleSearchCriteriaViewModel: ObservableObject {
}
}.store(in: &cancellables)
}

func bind(accumulatedLabels : some Publisher<Set<String>, Never>) {
accumulatedLabels.sink { [weak self] in
guard let self else { return }
self.accumulatedLabels = $0
}.store(in: &cancellables)
}

func bind(accumulatedHosts : some Publisher<Set<String>, Never>) {
accumulatedHosts.sink { [weak self] in
guard let self else { return }
self.accumulatedHosts = $0
}.store(in: &cancellables)
}

// MARK: Appearance

Expand Down Expand Up @@ -130,7 +146,7 @@ final class ConsoleSearchCriteriaViewModel: ObservableObject {
if let focused = criteria.messages.labels.focused {
return [focused]
} else {
return Set(labels).subtracting(criteria.messages.labels.hidden)
return Set(accumulatedLabels).subtracting(criteria.messages.labels.hidden)
}
}
set {
Expand All @@ -140,7 +156,7 @@ final class ConsoleSearchCriteriaViewModel: ObservableObject {
case 1:
criteria.messages.labels.focused = newValue.first!
default:
criteria.messages.labels.hidden = Set(labels).subtracting(newValue)
criteria.messages.labels.hidden = Set(accumulatedLabels).subtracting(newValue)
}
}
}
Expand All @@ -149,10 +165,10 @@ final class ConsoleSearchCriteriaViewModel: ObservableObject {

var selectedHost: Set<String> {
get {
Set(domains).subtracting(criteria.network.host.ignoredHosts)
Set(accumulatedHosts).subtracting(criteria.network.host.ignoredHosts)
}
set {
criteria.network.host.ignoredHosts = Set(domains).subtracting(newValue)
criteria.network.host.ignoredHosts = Set(accumulatedHosts).subtracting(newValue)
}
}
}

0 comments on commit 86ba2b3

Please sign in to comment.