@@ -11,11 +11,15 @@ import SwiftUI
11
11
public struct LicenseListView : View {
12
12
let libraries : [ Library ]
13
13
let useUINavigationController : Bool
14
- let id : UUID ?
14
+ let navigationHandler : ( ( Library ) -> Void ) ?
15
15
16
- public init ( fileURL: URL , useUINavigationController: Bool = false , id: UUID ? = nil ) {
16
+ public init (
17
+ fileURL: URL ,
18
+ useUINavigationController: Bool = false ,
19
+ navigationHandler: ( ( Library ) -> Void ) ? = nil
20
+ ) {
17
21
self . useUINavigationController = useUINavigationController
18
- self . id = id
22
+ self . navigationHandler = navigationHandler
19
23
guard let data = try ? Data ( contentsOf: fileURL) ,
20
24
let plist = try ? PropertyListSerialization . propertyList ( from: data, format: nil ) ,
21
25
let dict = plist as? [ String : Any ] else {
@@ -36,7 +40,12 @@ public struct LicenseListView: View {
36
40
ForEach ( libraries) { library in
37
41
if useUINavigationController {
38
42
HStack {
39
- libraryButton ( library)
43
+ Button {
44
+ navigationHandler ? ( library)
45
+ } label: {
46
+ Text ( library. name)
47
+ }
48
+ . foregroundColor ( . primary)
40
49
Spacer ( )
41
50
Image ( systemName: " chevron.right " )
42
51
. font ( . subheadline. bold ( ) )
@@ -50,52 +59,6 @@ public struct LicenseListView: View {
50
59
. listStyleInsetGroupedIfPossible ( )
51
60
}
52
61
53
- var navigationController : UINavigationController ? {
54
- guard let id else { return nil }
55
- let windowScenes = UIApplication . shared. connectedScenes. compactMap { $0 as? UIWindowScene }
56
- let viewControllers = windowScenes
57
- . flatMap { $0. windows }
58
- . flatMap { $0. rootViewController? . children ?? [ ] }
59
- . compactMap { $0 as? LicenseListViewController }
60
- guard let viewController = viewControllers. first ( where: { $0. id == id } ) else {
61
- return nil
62
- }
63
- var controller : UIViewController = viewController
64
- while true {
65
- if let navigationController = controller as? UINavigationController ,
66
- let visibleViewController = navigationController. visibleViewController {
67
- controller = visibleViewController
68
- continue
69
- }
70
- if let tabBarController = controller as? UITabBarController ,
71
- let selectedViewController = tabBarController. selectedViewController {
72
- controller = selectedViewController
73
- continue
74
- }
75
- if let presentedViewController = controller. presentedViewController {
76
- controller = presentedViewController
77
- continue
78
- }
79
- break
80
- }
81
- return controller. navigationController
82
- }
83
-
84
- func libraryButton( _ library: Library ) -> some View {
85
- return Button ( library. name) {
86
- let hostingController = UIHostingController ( rootView: Group {
87
- if #available( iOS 15 , * ) {
88
- LicenseView ( library: library)
89
- } else {
90
- LegacyLicenseView ( library: library)
91
- }
92
- } )
93
- hostingController. title = library. name
94
- navigationController? . pushViewController ( hostingController, animated: true )
95
- }
96
- . foregroundColor ( . primary)
97
- }
98
-
99
62
@ViewBuilder
100
63
func libraryNavigationLink( _ library: Library ) -> some View {
101
64
if #available( iOS 15 , * ) {
@@ -113,6 +76,6 @@ public struct LicenseListView: View {
113
76
public extension LicenseListView {
114
77
init ( bundle: Bundle = . main, useUINavigationController: Bool = false ) {
115
78
let url = bundle. url ( forResource: " license-list " , withExtension: " plist " ) ?? URL ( fileURLWithPath: " / " )
116
- self . init ( fileURL: url, useUINavigationController: useUINavigationController)
79
+ self . init ( fileURL: url, useUINavigationController: useUINavigationController, navigationHandler : { _ in } )
117
80
}
118
81
}
0 commit comments