Skip to content

Commit 20826dd

Browse files
committed
RUM-8415 - Address Simao's comments
1 parent e605d75 commit 20826dd

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

DatadogRUM/Sources/Instrumentation/Views/SwiftUI/SwiftUIControllerType.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
/// Controller type enum to identify different SwiftUI hosting controllers
88
internal enum ControllerType {
9+
case tabItem
910
case hostingController
1011
case navigationController
1112
case modal
12-
case tabItem
1313
case unknown
1414

1515
/// Determines the controller type from the class name

DatadogRUM/Sources/Instrumentation/Views/SwiftUI/SwiftUIViewNameExtractor.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal protocol SwiftUIViewNameExtractor {
1616
// MARK: - SwiftUIReflectionBasedViewNameExtractor
1717
/// Default implementation that extracts SwiftUI view names using reflection and string parsing
1818
internal struct SwiftUIReflectionBasedViewNameExtractor: SwiftUIViewNameExtractor {
19-
internal let createReflector: (Any) -> TopLevelReflector
19+
private let createReflector: (Any) -> TopLevelReflector
2020

2121
init(
2222
reflectorFactory: @escaping (Any) -> TopLevelReflector = { subject in
@@ -34,10 +34,6 @@ internal struct SwiftUIReflectionBasedViewNameExtractor: SwiftUIViewNameExtracto
3434
/// - Parameter viewController: The `UIViewController` potentially hosting a SwiftUI view
3535
/// - Returns: The extracted view name or nil if extraction failed
3636
func extractName(from viewController: UIViewController) -> String? {
37-
return extractViewNameFrom(from: viewController)
38-
}
39-
40-
private func extractViewNameFrom(from viewController: UIViewController) -> String? {
4137
// Skip known container controllers that shouldn't be tracked
4238
if shouldSkipViewController(viewController: viewController) {
4339
return nil
@@ -52,7 +48,7 @@ internal struct SwiftUIReflectionBasedViewNameExtractor: SwiftUIViewNameExtracto
5248
)
5349
}
5450

55-
internal func extractViewName(
51+
private func extractViewName(
5652
from viewController: UIViewController,
5753
withReflector reflector: TopLevelReflector
5854
) -> String? {
@@ -61,7 +57,7 @@ internal struct SwiftUIReflectionBasedViewNameExtractor: SwiftUIViewNameExtracto
6157

6258
switch controllerType {
6359
case .tabItem:
64-
return extractTabViewName(viewController: viewController)
60+
return extractTabViewName(from: viewController)
6561

6662
case .hostingController:
6763
if let output = SwiftUIViewPath.hostingController.traverse(with: reflector) {
@@ -123,7 +119,7 @@ internal struct SwiftUIReflectionBasedViewNameExtractor: SwiftUIViewNameExtracto
123119
return input
124120
}
125121

126-
internal func extractTabViewName(viewController: UIViewController) -> String? {
122+
private func extractTabViewName(from viewController: UIViewController) -> String? {
127123
// We fetch the parent, which corresponds to the TabBarController
128124
guard let parent = viewController.parent as? UITabBarController,
129125
let container = parent.parent else {

DatadogRUM/Sources/Instrumentation/Views/SwiftUI/SwiftUIViewPath.swift

+21-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@
77
import DatadogInternal
88

99
/// Common path components for SwiftUI view traversal
10-
internal enum RootPath {
11-
static let hostingBase = ["host", "_rootView"]
12-
static let navigationBase = ["host", "_rootView", "storage", "view", "content", "content", "content"]
13-
static let sheetBase = ["host", "_rootView", "storage", "view", "content"]
10+
internal enum ViewNode: String {
11+
case host
12+
case rootView = "_rootView"
13+
case root
14+
case storage
15+
case view
16+
case content
17+
case list
18+
case item
19+
case type
20+
21+
static let hostingBase: [ViewNode] = [.host, .rootView]
22+
static let navigationBase: [ViewNode] = [.host, .rootView, .storage, .view, .content, .content, .content]
23+
static let sheetBase: [ViewNode] = [.host, .rootView, .storage, .view, .content]
1424
}
1525

1626
/// Defines the various traversal paths for different SwiftUI view structures
@@ -25,18 +35,18 @@ internal enum SwiftUIViewPath {
2535
case sheetContent
2636

2737
/// The sequence of property names to traverse for this view type
28-
var pathComponents: [String] {
38+
var pathComponents: [ViewNode] {
2939
switch self {
3040
case .hostingController:
31-
return RootPath.hostingBase + ["content", "storage", "view"]
41+
return ViewNode.hostingBase + [.content, .storage, .view]
3242
case .navigationStack:
33-
return RootPath.navigationBase
43+
return ViewNode.navigationBase
3444
case .navigationStackDetail:
35-
return RootPath.navigationBase + ["content", "list", "item", "type"]
45+
return ViewNode.navigationBase + [.content, .list, .item, .type]
3646
case .navigationStackContainer:
37-
return RootPath.navigationBase + ["root"]
47+
return ViewNode.navigationBase + [.root]
3848
case .sheetContent:
39-
return RootPath.sheetBase
49+
return ViewNode.sheetBase
4050
}
4151
}
4252

@@ -46,7 +56,7 @@ internal enum SwiftUIViewPath {
4656
/// - Returns: The object found at the end of the path, or nil if not found
4757
func traverse(with reflector: TopLevelReflector) -> Any? {
4858
// Convert string components to path objects
49-
let paths = pathComponents.map { ReflectionMirror.Path.key($0) }
59+
let paths = pathComponents.map { ReflectionMirror.Path.key($0.rawValue) }
5060

5161
// Use descendant directly
5262
return reflector.descendant(paths)

DatadogRUM/Tests/Instrumentation/Views/SwiftUIViewNameExtractorTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ class SwiftUIViewNameExtractorTests: XCTestCase {
4545
func testSwiftUIViewPathComponents() {
4646
XCTAssertEqual(
4747
SwiftUIViewPath.hostingController.pathComponents,
48-
["host", "_rootView", "content", "storage", "view"]
48+
[.host, .rootView, .content, .storage, .view]
4949
)
5050
XCTAssertEqual(
5151
SwiftUIViewPath.navigationStack.pathComponents,
52-
["host", "_rootView", "storage", "view", "content", "content", "content"]
52+
[.host, .rootView, .storage, .view, .content, .content, .content]
5353
)
5454
XCTAssertEqual(
5555
SwiftUIViewPath.navigationStackDetail.pathComponents,
56-
["host", "_rootView", "storage", "view", "content", "content", "content", "content", "list", "item", "type"]
56+
[.host, .rootView, .storage, .view, .content, .content, .content, .content, .list, .item, .type]
5757
)
5858
XCTAssertEqual(
5959
SwiftUIViewPath.navigationStackContainer.pathComponents,
60-
["host", "_rootView", "storage", "view", "content", "content", "content", "root"]
60+
[.host, .rootView, .storage, .view, .content, .content, .content, .root]
6161
)
6262
XCTAssertEqual(
6363
SwiftUIViewPath.sheetContent.pathComponents,
64-
["host", "_rootView", "storage", "view", "content"]
64+
[.host, .rootView, .storage, .view, .content]
6565
)
6666
}
6767

0 commit comments

Comments
 (0)