Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to display custom headers in the list view #196

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions Sources/PulseUI/Features/Console/ConsoleDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ protocol ConsoleDataSourceDelegate: AnyObject {
}

final class ConsoleDataSource: NSObject, NSFetchedResultsControllerDelegate {
private(set) var entities: [NSManagedObject] = []
private(set) var sections: [NSFetchedResultsSectionInfo]?

weak var delegate: ConsoleDataSourceDelegate?

/// - warning: Incompatible with the "group by" option.
Expand Down Expand Up @@ -81,6 +78,7 @@ final class ConsoleDataSource: NSObject, NSFetchedResultsControllerDelegate {
NSSortDescriptor(key: sortKey, ascending: options.order == .ascending)
].compactMap { $0 }
request.fetchBatchSize = ConsoleDataSource.fetchBatchSize
request.relationshipKeyPathsForPrefetching = ["request"]
controller = NSFetchedResultsController(
fetchRequest: request,
managedObjectContext: store.viewContext,
Expand Down Expand Up @@ -113,27 +111,37 @@ final class ConsoleDataSource: NSObject, NSFetchedResultsControllerDelegate {

func refresh() {
try? controller.performFetch()
refreshEntities()
delegate?.dataSourceDidRefresh(self)
}


// MARK: Accessing Entities

var numberOfObjects: Int {
controller.fetchedObjects?.count ?? 0
}

func object(at indexPath: IndexPath) -> NSManagedObject {
controller.object(at: indexPath)
}

var entities: [NSManagedObject] {
controller.fetchedObjects ?? []
}

var sections: [NSFetchedResultsSectionInfo]? {
controller.sectionNameKeyPath == nil ? nil : controller.sections
}

// MARK: NSFetchedResultsControllerDelegate

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
refreshEntities()
delegate?.dataSource(self, didUpdateWith: nil)
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith diff: CollectionDifference<NSManagedObjectID>) {
refreshEntities()
delegate?.dataSource(self, didUpdateWith: diff)
}

private func refreshEntities() {
entities = controller.fetchedObjects ?? []
sections = controller.sectionNameKeyPath == nil ? nil : controller.sections
}

// MARK: Predicate

private func refreshPredicate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct ConsoleEntityDetailsRouterView: View {
}

struct ButtonChangeContentModeLayout: View {
@SceneStorage("is-details-vertical") private var isVertical = false
@SceneStorage("scene-is-details-vertical") private var isVertical = AppSettings.shared.isVertical

var body: some View {
Button(action: { isVertical.toggle() }, label: {
Expand All @@ -55,6 +55,9 @@ struct ButtonChangeContentModeLayout: View {
})
.help(isVertical ? "Switch to Horizontal Layout" : "Switch to Vertical Layout")
.buttonStyle(.plain)
.onChange(of: isVertical) {
AppSettings.shared.isVertical = $0
}
}
}

Expand Down
31 changes: 27 additions & 4 deletions Sources/PulseUI/Features/Console/Views/ConsoleTaskCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,33 @@ struct ConsoleTaskCell: View {
}

private var message: some View {
Text(task.url ?? "–")
.font(ConsoleConstants.fontBody)
.foregroundColor(.primary)
.lineLimit(settings.lineLimit)
VStack(spacing: 3) {
HStack {
Text(task.url ?? "–")
.font(ConsoleConstants.fontBody)
.foregroundColor(.primary)
.lineLimit(settings.lineLimit)

Spacer()
}

let headerValueMap = settings.displayHeaders.reduce(into: [String: String]()) { partialResult, header in
partialResult[header] = task.originalRequest?.headers[header]
}

ForEach(headerValueMap.keys.sorted(), id: \.self) { key in
HStack {
Text(key)
.font(.caption)
.foregroundColor(.secondary)

Text(headerValueMap[key] ?? "-")
.font(.callout)
.bold()
Spacer()
}
}
}
}

private var details: some View {
Expand Down
24 changes: 23 additions & 1 deletion Sources/PulseUI/Features/Settings/SettingsView-ios.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UniformTypeIdentifiers

public struct SettingsView: View {
@ObservedObject var viewModel: SettingsViewModel

@State private var newHeaderName = ""
@EnvironmentObject private var settings: UserSettings

public init(store: LoggerStore = .shared) {
Expand All @@ -35,6 +35,28 @@ public struct SettingsView: View {
RemoteLoggerSettingsView(viewModel: .shared)
}
}

Section(header: Text("List headers"), footer: Text("These headers will be included in the list view")) {
ForEach(settings.displayHeaders, id: \.self) {
Text($0)
}
.onDelete { indices in
settings.displayHeaders.remove(atOffsets: indices)
}
HStack {
TextField("New Header", text: $newHeaderName)
Button(action: {
withAnimation {
settings.displayHeaders.append(newHeaderName)
newHeaderName = ""
}
}) {
Image(systemName: "plus.circle.fill")
.accessibilityLabel("Add header")
}
.disabled(newHeaderName.isEmpty)
}
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/PulseUI/Features/Settings/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,29 @@ final class UserSettings: ObservableObject {

@AppStorage("sharing-output")
var sharingOutput: ShareStoreOutput = .store

@AppStorage("display-headers")
var displayHeaders: [String] = []
}

// MARK: - Array + RawREpresentable

extension Array: RawRepresentable where Element: Codable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode([Element].self, from: data)
else {
return nil
}
self = result
}

public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
3 changes: 0 additions & 3 deletions Sources/PulseUI/Views/ContextMenus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ enum ContextMenu {
// NetworkTaskFilterMenu(task: task)
// }
// }
#if PULSE_STANDALONE_APP
StandaloneNetworkTaskContextMenu(task: task)
#endif
if let message = task.message {
Section {
PinButton(viewModel: .init(message))
Expand Down