Skip to content

Commit

Permalink
Remove ConsoleDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Aug 4, 2024
1 parent e2c05ff commit 141ba2d
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 26 deletions.
14 changes: 3 additions & 11 deletions Sources/PulseUI/Features/Console/ConsoleDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@
import Foundation
import Pulse

/// Allows you to customize the console behavior.
public protocol ConsoleViewDelegate {
/// Returns a title for the given task.
func getTitle(for task: NetworkTaskEntity) -> String?
}

extension ConsoleViewDelegate {
func getTitle(for task: NetworkTaskEntity) -> String? {
enum ConsoleViewDelegate {
static func getTitle(for task: NetworkTaskEntity) -> String? {
if let taskDescription = task.taskDescription, !taskDescription.isEmpty {
return taskDescription
}
return task.url
}

func getShortTitle(for task: NetworkTaskEntity) -> String {
static func getShortTitle(for task: NetworkTaskEntity) -> String {
guard let title = getTitle(for: task) else {
return ""
}
return URL(string: title)?.lastPathComponent ?? title
}
}

struct DefaultConsoleViewDelegate: ConsoleViewDelegate {}
5 changes: 1 addition & 4 deletions Sources/PulseUI/Features/Console/ConsoleEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ final class ConsoleEnvironment: ObservableObject {
let router = ConsoleRouter()

let initialMode: ConsoleMode
let delegate: ConsoleViewDelegate

@Published var mode: ConsoleMode
@Published var listOptions: ConsoleListOptions = .init()
Expand All @@ -38,7 +37,7 @@ final class ConsoleEnvironment: ObservableObject {

private var cancellables: [AnyCancellable] = []

init(store: LoggerStore, mode: ConsoleMode = .all, delegate: ConsoleViewDelegate = DefaultConsoleViewDelegate()) {
init(store: LoggerStore, mode: ConsoleMode = .all) {
self.store = store
switch mode {
case .all: self.title = "Console"
Expand All @@ -53,8 +52,6 @@ final class ConsoleEnvironment: ObservableObject {
case .network: self.mode = .network
}

self.delegate = delegate

func makeDefaultOptions() -> ConsoleDataSource.PredicateOptions {
var options = ConsoleDataSource.PredicateOptions()
options.filters.shared.sessions.selection = [store.session.id]
Expand Down
2 changes: 1 addition & 1 deletion Sources/PulseUI/Features/Console/ConsoleView-ios.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct ConsoleView_Previews: PreviewProvider {
static var previews: some View {
Group {
NavigationView {
ConsoleView(environment: .init(store: .mock, delegate: DefaultConsoleViewDelegate()))
ConsoleView(environment: .init(store: .mock))
}.previewDisplayName("Console")
NavigationView {
ConsoleView(store: .mock, mode: .network)
Expand Down
5 changes: 2 additions & 3 deletions Sources/PulseUI/Features/Console/ConsoleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ extension ConsoleView {
/// of the console view.
public init(
store: LoggerStore = .shared,
mode: ConsoleMode = .all,
delegate: ConsoleViewDelegate? = nil
mode: ConsoleMode = .all
) {
self.init(environment: .init(store: store, mode: mode, delegate: delegate ?? DefaultConsoleViewDelegate()))
self.init(environment: .init(store: store, mode: mode))
}
}
3 changes: 1 addition & 2 deletions Sources/PulseUI/Features/Console/Views/ConsoleTaskCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct ConsoleTaskCell: View {

@ObservedObject private var settings: UserSettings = .shared
@Environment(\.store) private var store: LoggerStore
@EnvironmentObject var environment: ConsoleEnvironment

var body: some View {
#if os(macOS)
Expand Down Expand Up @@ -82,7 +81,7 @@ struct ConsoleTaskCell: View {
private var message: some View {
VStack(spacing: 3) {
HStack {
Text(environment.delegate.getTitle(for: task) ?? "")
Text(ConsoleViewDelegate.getTitle(for: task) ?? "")
.font(ConsoleConstants.fontBody)
.foregroundColor(.primary)
.lineLimit(settings.lineLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ struct NetworkInspectorView: View {
@State private var shareItems: ShareItems?
@State private var sharedTask: NetworkTaskEntity?
@ObservedObject private var settings: UserSettings = .shared
@EnvironmentObject private var environment: ConsoleEnvironment
@Environment(\.store) private var store

#if os(iOS) || os(visionOS)
Expand All @@ -29,7 +28,7 @@ struct NetworkInspectorView: View {
.safeAreaInset(edge: .bottom) {
OpenOnMacOverlay(entity: task)
}
.inlineNavigationTitle(environment.delegate.getShortTitle(for: task))
.inlineNavigationTitle(ConsoleViewDelegate.getShortTitle(for: task))
.sheet(item: $shareItems, content: ShareView.init)
.toolbar {
ToolbarItemGroup(placement: .automatic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct ConsoleSearchSuggestionsView: View {
#if DEBUG
@available(iOS 15, visionOS 1.0, *)
struct Previews_ConsoleSearchSuggestionsView_Previews: PreviewProvider {
static let environment = ConsoleEnvironment(store: .mock, delegate: DefaultConsoleViewDelegate())
static let environment = ConsoleEnvironment(store: .mock)

static var previews: some View {
List {
Expand Down
2 changes: 0 additions & 2 deletions Sources/PulseUI/PulseUI.docc/PulseUI.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ NavigationLink(destination: ConsoleView()) {

> tip: If you use Pulse to log only network requests, and not text messages, use `ConsoleView(mode: .network)` to show a view specialized to only display network requests.
The console can be customized using ``ConsoleViewDelegate`` and ``UserSettings``.

## UIKit

To present the console from `UIKit`, use `UIHostingController`:
Expand Down

0 comments on commit 141ba2d

Please sign in to comment.