Skip to content

Commit 4f203c8

Browse files
committed
Modified navigation of UIKit
1 parent 60f2bab commit 4f203c8

File tree

2 files changed

+29
-55
lines changed

2 files changed

+29
-55
lines changed

Sources/LicenseList/LicenseListView.swift

+14-51
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ import SwiftUI
1111
public struct LicenseListView: View {
1212
let libraries: [Library]
1313
let useUINavigationController: Bool
14-
let id: UUID?
14+
let navigationHandler: ((Library) -> Void)?
1515

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+
) {
1721
self.useUINavigationController = useUINavigationController
18-
self.id = id
22+
self.navigationHandler = navigationHandler
1923
guard let data = try? Data(contentsOf: fileURL),
2024
let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
2125
let dict = plist as? [String: Any] else {
@@ -36,7 +40,12 @@ public struct LicenseListView: View {
3640
ForEach(libraries) { library in
3741
if useUINavigationController {
3842
HStack {
39-
libraryButton(library)
43+
Button {
44+
navigationHandler?(library)
45+
} label: {
46+
Text(library.name)
47+
}
48+
.foregroundColor(.primary)
4049
Spacer()
4150
Image(systemName: "chevron.right")
4251
.font(.subheadline.bold())
@@ -50,52 +59,6 @@ public struct LicenseListView: View {
5059
.listStyleInsetGroupedIfPossible()
5160
}
5261

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-
9962
@ViewBuilder
10063
func libraryNavigationLink(_ library: Library) -> some View {
10164
if #available(iOS 15, *) {
@@ -113,6 +76,6 @@ public struct LicenseListView: View {
11376
public extension LicenseListView {
11477
init(bundle: Bundle = .main, useUINavigationController: Bool = false) {
11578
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 })
11780
}
11881
}

Sources/LicenseList/LicenseListViewController.swift

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import SwiftUI
1010

1111
public class LicenseListViewController: UIViewController {
1212
let fileURL: URL
13-
let id: UUID
1413

1514
public init(fileURL: URL) {
1615
self.fileURL = fileURL
17-
self.id = UUID()
1816
super.init(nibName: nil, bundle: nil)
1917
}
2018

@@ -31,8 +29,9 @@ public class LicenseListViewController: UIViewController {
3129

3230
public override func viewDidLoad() {
3331
super.viewDidLoad()
34-
35-
let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true, id: id)
32+
let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true) { [weak self] library in
33+
self?.navigateTo(library: library)
34+
}
3635
let vc = UIHostingController(rootView: licenseListView)
3736
self.addChild(vc)
3837
self.view.addSubview(vc.view)
@@ -42,4 +41,16 @@ public class LicenseListViewController: UIViewController {
4241
vc.view.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
4342
vc.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
4443
}
44+
45+
private func navigateTo(library: Library) {
46+
let hostingController = UIHostingController(rootView: Group {
47+
if #available(iOS 15, *) {
48+
LicenseView(library: library)
49+
} else {
50+
LegacyLicenseView(library: library)
51+
}
52+
})
53+
hostingController.title = library.name
54+
self.navigationController?.pushViewController(hostingController, animated: true)
55+
}
4556
}

0 commit comments

Comments
 (0)