Skip to content

Commit 895fb6f

Browse files
authored
Merge pull request #276 from AvdLee/feature/console-configuration
Console configuration
2 parents 9c5f3fc + c99e4c7 commit 895fb6f

8 files changed

+90
-15
lines changed

Pulse.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
0CFF79E229EC1FA200BE767B /* ImageProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CFF79E029EC1F7800BE767B /* ImageProcessor.swift */; };
276276
25E740012BE0FA58008C6DC3 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 25E740002BE0FA58008C6DC3 /* PrivacyInfo.xcprivacy */; };
277277
25E740072BE10B4C008C6DC3 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 25E740062BE10B4C008C6DC3 /* PrivacyInfo.xcprivacy */; };
278+
849DD7D92C32A9CE002D6787 /* ConsoleConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849DD7D82C32A9CE002D6787 /* ConsoleConfiguration.swift */; };
278279
E95D6C562B7314E6004D28E4 /* HARDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = E95D6C552B7314E6004D28E4 /* HARDocument.swift */; };
279280
/* End PBXBuildFile section */
280281

@@ -737,6 +738,7 @@
737738
25E740062BE10B4C008C6DC3 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
738739
49E82A8526D107A00070244F /* AlamofireIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlamofireIntegration.swift; sourceTree = "<group>"; };
739740
49E82A8826D1083D0070244F /* MoyaIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoyaIntegration.swift; sourceTree = "<group>"; };
741+
849DD7D82C32A9CE002D6787 /* ConsoleConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleConfiguration.swift; sourceTree = "<group>"; };
740742
E95D6C552B7314E6004D28E4 /* HARDocument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HARDocument.swift; sourceTree = "<group>"; };
741743
/* End PBXFileReference section */
742744

@@ -1398,6 +1400,7 @@
13981400
0CA2632D2BFA3855003C8E97 /* ConsoleDelegate.swift */,
13991401
0CF0D60A296F189600EED9D4 /* ConsoleEnvironment.swift */,
14001402
0CECF63C2996BE12000F9CCD /* ConsoleDataSource.swift */,
1403+
849DD7D82C32A9CE002D6787 /* ConsoleConfiguration.swift */,
14011404
);
14021405
path = Console;
14031406
sourceTree = "<group>";
@@ -2136,6 +2139,7 @@
21362139
0CDC3BED2971B936003BD1FF /* ConsoleSearchLogLevelsCell.swift in Sources */,
21372140
0CF0D620296F189600EED9D4 /* Foundation+Extensions.swift in Sources */,
21382141
0CF0D66C296F189600EED9D4 /* NetworkRequestInfoCell.swift in Sources */,
2142+
849DD7D92C32A9CE002D6787 /* ConsoleConfiguration.swift in Sources */,
21392143
0CB63A15297438A600525165 /* ConsoleSearchViewModel.swift in Sources */,
21402144
0CF0D632296F189600EED9D4 /* ShareStoreView.swift in Sources */,
21412145
0C9751582A32CCC400DC46FF /* RemoteLoggerEnterPasswordView.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The MIT License (MIT)
2+
//
3+
// Created by A.J. van der Lee on 01/07/2024.
4+
// Copyright © 2024 kean. All rights reserved.
5+
//
6+
7+
import Foundation
8+
9+
/// Defines UI configurations to enable/disable elements or change behavior.
10+
public struct ConsoleConfiguration {
11+
public static let `default` = ConsoleConfiguration()
12+
13+
let shareStoreOutputs: [ShareStoreOutput]
14+
let allowRemoteLogging: Bool
15+
16+
/// Creates a new `ConsoleConfiguration`
17+
/// - Parameter shareStoreOutputs: The available store share outputs. Defaults to `allCases`.
18+
/// - Parameter allowRemoteLogging: Enable/disable the remote logging option.
19+
public init(
20+
shareStoreOutputs: [ShareStoreOutput] = ShareStoreOutput.allCases,
21+
allowRemoteLogging: Bool = true
22+
) {
23+
self.shareStoreOutputs = shareStoreOutputs
24+
self.allowRemoteLogging = allowRemoteLogging
25+
}
26+
}

Sources/PulseUI/Features/Console/ConsoleEnvironment.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class ConsoleEnvironment: ObservableObject {
2525

2626
let initialMode: ConsoleMode
2727
let delegate: ConsoleViewDelegate
28+
let configuration: ConsoleConfiguration
2829

2930
@Published var mode: ConsoleMode
3031
@Published var listOptions: ConsoleListOptions = .init()
@@ -39,7 +40,7 @@ final class ConsoleEnvironment: ObservableObject {
3940

4041
private var cancellables: [AnyCancellable] = []
4142

42-
init(store: LoggerStore, mode: ConsoleMode = .all, delegate: ConsoleViewDelegate = DefaultConsoleViewDelegate()) {
43+
init(store: LoggerStore, mode: ConsoleMode = .all, configuration: ConsoleConfiguration = .default, delegate: ConsoleViewDelegate = DefaultConsoleViewDelegate()) {
4344
self.store = store
4445
switch mode {
4546
case .all: self.title = "Console"
@@ -55,6 +56,7 @@ final class ConsoleEnvironment: ObservableObject {
5556
}
5657

5758
self.delegate = delegate
59+
self.configuration = configuration
5860

5961
func makeDefaultOptions() -> ConsoleDataSource.PredicateOptions {
6062
var options = ConsoleDataSource.PredicateOptions()

Sources/PulseUI/Features/Console/ConsoleView.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import Combine
99

1010
extension ConsoleView {
1111
/// Initializes the console view.
12-
///
12+
///
1313
/// - parameters:
1414
/// - store: The store to display. By default, `LoggerStore/shared`.
1515
/// - mode: The initial console mode. By default, ``ConsoleMode/all``. If you change
1616
/// the mode to ``ConsoleMode/network``, the console will display the
1717
/// network messages up on appearance.
18+
/// - configuration: Configuration options to alter the UI behavior and settings.
1819
/// - delegate: The delegate that allows you to customize multiple aspects
1920
/// of the console view.
2021
public init(
2122
store: LoggerStore = .shared,
2223
mode: ConsoleMode = .all,
24+
configuration: ConsoleConfiguration = .default,
2325
delegate: ConsoleViewDelegate? = nil
2426
) {
25-
self.init(environment: .init(store: store, mode: mode, delegate: delegate ?? DefaultConsoleViewDelegate()))
27+
self.init(environment: .init(store: store, mode: mode, configuration: configuration, delegate: delegate ?? DefaultConsoleViewDelegate()))
2628
}
2729
}

Sources/PulseUI/Features/Settings/SettingsView-macos.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ struct SettingsView: View {
1212
@State private var shareItems: ShareItems?
1313

1414
@Environment(\.store) private var store
15+
@EnvironmentObject private var environment: ConsoleEnvironment
1516

1617
var body: some View {
1718
List {
18-
if store === RemoteLogger.shared.store {
19-
RemoteLoggerSettingsView(viewModel: .shared)
20-
} else {
21-
Text("Not available")
22-
.foregroundColor(.secondary)
19+
if environment.configuration.allowRemoteLogging {
20+
if store === RemoteLogger.shared.store {
21+
RemoteLoggerSettingsView(viewModel: .shared)
22+
} else {
23+
Text("Not available")
24+
.foregroundColor(.secondary)
25+
}
2326
}
2427
Section(header: Text("Store")) {
2528
if #available(macOS 13, *), let info = try? store.info() {

Sources/PulseUI/Helpers/ShareItems.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Foundation
66
import Pulse
77
import CoreData
88

9-
public enum ShareStoreOutput: String, RawRepresentable {
9+
public enum ShareStoreOutput: String, RawRepresentable, CaseIterable {
1010
case store, package, text, html, har
1111

1212
var fileExtension: String {
@@ -17,6 +17,16 @@ public enum ShareStoreOutput: String, RawRepresentable {
1717
case .har: return "har"
1818
}
1919
}
20+
21+
var interfaceTitle: String {
22+
switch self {
23+
case .store: "Pulse"
24+
case .package: "Pulse (Package)"
25+
case .text: "Plain Text"
26+
case .html: "HTML"
27+
case .har: "HAR"
28+
}
29+
}
2030
}
2131

2232
struct ShareItems: Identifiable {

Sources/PulseUI/Views/ShareStoreView.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct ShareStoreView: View {
2020
@StateObject private var viewModel = ShareStoreViewModel()
2121

2222
@Environment(\.store) private var store: LoggerStore
23+
@EnvironmentObject private var environment: ConsoleEnvironment
2324

2425
var body: some View {
2526
content
@@ -30,6 +31,7 @@ struct ShareStoreView: View {
3031
viewModel.sessions = [store.session.id]
3132
}
3233
viewModel.store = store
34+
viewModel.environment = environment
3335
}
3436
}
3537

@@ -95,12 +97,12 @@ struct ShareStoreView: View {
9597
}
9698
Section {
9799
Picker("Output", selection: $viewModel.output) {
98-
Text("Pulse").tag(ShareStoreOutput.store)
99-
Text("Plain Text").tag(ShareStoreOutput.text)
100-
Text("HTML").tag(ShareStoreOutput.html)
101-
Text("HAR").tag(ShareStoreOutput.har)
102-
Divider()
103-
Text("Pulse (Package)").tag(ShareStoreOutput.package)
100+
ForEach(viewModel.shareStoreOutputs, id: \.rawValue) { shareOutput in
101+
if shareOutput == .package {
102+
Divider()
103+
}
104+
Text(shareOutput.interfaceTitle).tag(shareOutput)
105+
}
104106
}
105107
#if os(macOS)
106108
.labelsHidden()

Sources/PulseUI/Views/ShareStoreViewModel.swift

+26
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ import Combine
1313
// Sharing options
1414
@Published var sessions: Set<UUID> = []
1515
@Published var logLevels = Set(LoggerStore.Level.allCases)
16+
@Published var shareStoreOutputs: [ShareStoreOutput] = ShareStoreOutput.allCases
1617
@Published var output: ShareStoreOutput
1718

1819
@Published private(set) var isPreparingForSharing = false
1920
@Published private(set) var errorMessage: String?
2021
@Published var shareItems: ShareItems?
2122

2223
var store: LoggerStore?
24+
var environment: ConsoleEnvironment? {
25+
didSet {
26+
updateForCurrentEnvironment()
27+
}
28+
}
2329

2430
init() {
2531
output = UserSettings.shared.sharingOutput
@@ -31,6 +37,26 @@ import Combine
3137
saveSharingOptions()
3238
prepareForSharing()
3339
}
40+
41+
private func updateForCurrentEnvironment() {
42+
guard let environment else { return }
43+
shareStoreOutputs = environment.configuration.shareStoreOutputs.sorted(by: { lhs, rhs in
44+
/// Make sure the .package is always last since we show a Divider() in front of it.
45+
switch (lhs, rhs) {
46+
case (.package, _):
47+
return false
48+
case (_, .package):
49+
return true
50+
default:
51+
return lhs.rawValue < rhs.rawValue
52+
}
53+
})
54+
55+
if !shareStoreOutputs.contains(output), let shareStoreOutput = shareStoreOutputs.first {
56+
/// Update the selected output to one of the available options.
57+
output = shareStoreOutput
58+
}
59+
}
3460

3561
private func saveSharingOptions() {
3662
UserSettings.shared.sharingOutput = output

0 commit comments

Comments
 (0)