@@ -50,69 +50,66 @@ internal class ModalSwizzler {
5050}
5151
5252extension UIViewController {
53+ private func extractBasicName( from vc: UIViewController ? ) -> String ? {
54+ return vc? . accessibilityLabel ?? vc? . title ?? vc? . navigationItem. title
55+ }
56+
57+ private func extractSwiftUIHostingName( ) -> String ? {
58+ guard let hostingController = self as? UIHostingController < AnyView > else { return nil }
59+
60+ let mirror = Mirror ( reflecting: hostingController. rootView)
61+ let typeName = String ( describing: mirror. subjectType)
62+ . replacingOccurrences ( of: " ModifiedContent< " , with: " " )
63+ . replacingOccurrences ( of: " , RootModifier> " , with: " " )
64+ . replacingOccurrences ( of: " , _TraitWritingModifier<.*>> " , with: " " , options: . regularExpression)
65+ . replacingOccurrences ( of: " AnyView " , with: " " )
66+ . trimmingCharacters ( in: . whitespacesAndNewlines)
67+
68+ return typeName. isEmpty ? nil : typeName
69+ }
70+
5371 var meaningfulName : String {
54- // Try all our previous methods first
55- if let name = self . accessibilityLabel ?? self . title ?? self . navigationItem. title {
56- return name
57- }
72+ // Try basic name extraction first
73+ if let name = extractBasicName ( from: self ) { return name }
5874
59- // If we're in a navigation controller, try to get its title
75+ // If we're a nav controller, try the top VC
6076 if let nav = self as? UINavigationController ,
61- let topVC = nav. topViewController,
62- let navTitle = topVC. accessibilityLabel ?? topVC. title ?? topVC. navigationItem. title {
63- return navTitle
64- }
77+ let name = extractBasicName ( from: nav. topViewController) { return name }
6578
66- // If we have a navigation controller, try its top view controller
67- if let nav = self . navigationController,
68- let navTitle = nav. topViewController? . accessibilityLabel ??
69- nav. topViewController? . title ??
70- nav. topViewController? . navigationItem. title {
71- return navTitle
72- }
79+ // If we have a nav controller, try its top VC
80+ if let name = extractBasicName ( from: navigationController? . topViewController) { return name }
7381
74- // If we've got a hosting controller, try to get the SwiftUI view name
75- if let hostingController = self as? UIHostingController < AnyView > {
76- let mirror = Mirror ( reflecting: hostingController. rootView)
77- let value = String ( describing: mirror. subjectType)
78- . replacingOccurrences ( of: " ModifiedContent< " , with: " " )
79- . replacingOccurrences ( of: " , RootModifier> " , with: " " )
80- . replacingOccurrences ( of: " , _TraitWritingModifier<.*>> " , with: " " , options: . regularExpression)
81- . replacingOccurrences ( of: " AnyView " , with: " " )
82- . trimmingCharacters ( in: . whitespacesAndNewlines)
83- if value. isEmpty == false {
84- return value
85- }
86- }
82+ // Try SwiftUI hosting controller
83+ if let name = extractSwiftUIHostingName ( ) { return name }
8784
88- // Clean up the type name if we have to use it
85+ // Fall back to type name
8986 return String ( describing: type ( of: self ) )
9087 }
9188
9289 @objc dynamic func swizzled_present( _ viewControllerToPresent: UIViewController , animated: Bool , completion: ( ( ) -> Void ) ? = nil ) {
90+ let fromScreen = self . meaningfulName
9391 let modalScreen = viewControllerToPresent. meaningfulName
9492
95- // Call original implementation
9693 self . swizzled_present ( viewControllerToPresent, animated: animated, completion: completion)
9794
98- // Just emit the modal signal and save the name
99- let modalSignal = NavigationSignal ( action : . modal , screen : modalScreen )
100- Signals . emit ( signal : modalSignal , source : . autoSwiftUI )
95+ let navSignal = NavigationSignal ( currentScreen : modalScreen , previousScreen : fromScreen )
96+ Signals . emit ( signal : navSignal , source : . autoUIKit )
97+
10198 ModalSwizzler . shared. currentModalName = modalScreen
10299 }
103100
104101 @objc dynamic func swizzled_dismiss( animated: Bool , completion: ( ( ) -> Void ) ? = nil ) {
105- // Get the saved modal name
106102 let modalScreen = ModalSwizzler . shared. currentModalName ?? self . meaningfulName
103+ let backToScreen = presentingViewController? . meaningfulName
107104
108- // Call original implementation
109105 self . swizzled_dismiss ( animated: animated, completion: completion)
110106
111- // Just emit leaving for the modal
112- let leavingSignal = NavigationSignal ( action: . leaving, screen: modalScreen)
113- Signals . emit ( signal: leavingSignal, source: . autoSwiftUI)
107+ let navSignal = NavigationSignal (
108+ currentScreen: backToScreen ?? " Unknown <dismissed> " ,
109+ previousScreen: modalScreen
110+ )
111+ Signals . emit ( signal: navSignal, source: . autoUIKit)
114112
115- // Clear the saved name
116113 ModalSwizzler . shared. currentModalName = nil
117114 }
118115}
0 commit comments