Skip to content

Commit

Permalink
Merge pull request #196 from tahirmt/add-header-to-main
Browse files Browse the repository at this point in the history
Add option to display custom headers in the list view
  • Loading branch information
kean authored Jun 6, 2023
2 parents fe02451 + f785f91 commit 09723a0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
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 @@ -76,10 +76,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
}
}

0 comments on commit 09723a0

Please sign in to comment.