Skip to content

Commit

Permalink
Modified navigation of UIKit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyome22 committed Sep 20, 2023
1 parent 60f2bab commit 4f203c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 55 deletions.
65 changes: 14 additions & 51 deletions Sources/LicenseList/LicenseListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import SwiftUI
public struct LicenseListView: View {
let libraries: [Library]
let useUINavigationController: Bool
let id: UUID?
let navigationHandler: ((Library) -> Void)?

public init(fileURL: URL, useUINavigationController: Bool = false, id: UUID? = nil) {
public init(
fileURL: URL,
useUINavigationController: Bool = false,
navigationHandler: ((Library) -> Void)? = nil
) {
self.useUINavigationController = useUINavigationController
self.id = id
self.navigationHandler = navigationHandler
guard let data = try? Data(contentsOf: fileURL),
let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
let dict = plist as? [String: Any] else {
Expand All @@ -36,7 +40,12 @@ public struct LicenseListView: View {
ForEach(libraries) { library in
if useUINavigationController {
HStack {
libraryButton(library)
Button {
navigationHandler?(library)
} label: {
Text(library.name)
}
.foregroundColor(.primary)
Spacer()
Image(systemName: "chevron.right")
.font(.subheadline.bold())
Expand All @@ -50,52 +59,6 @@ public struct LicenseListView: View {
.listStyleInsetGroupedIfPossible()
}

var navigationController: UINavigationController? {
guard let id else { return nil }
let windowScenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
let viewControllers = windowScenes
.flatMap { $0.windows }
.flatMap { $0.rootViewController?.children ?? [] }
.compactMap { $0 as? LicenseListViewController }
guard let viewController = viewControllers.first(where: { $0.id == id }) else {
return nil
}
var controller: UIViewController = viewController
while true {
if let navigationController = controller as? UINavigationController,
let visibleViewController = navigationController.visibleViewController {
controller = visibleViewController
continue
}
if let tabBarController = controller as? UITabBarController,
let selectedViewController = tabBarController.selectedViewController {
controller = selectedViewController
continue
}
if let presentedViewController = controller.presentedViewController {
controller = presentedViewController
continue
}
break
}
return controller.navigationController
}

func libraryButton(_ library: Library) -> some View {
return Button(library.name) {
let hostingController = UIHostingController(rootView: Group {
if #available(iOS 15, *) {
LicenseView(library: library)
} else {
LegacyLicenseView(library: library)
}
})
hostingController.title = library.name
navigationController?.pushViewController(hostingController, animated: true)
}
.foregroundColor(.primary)
}

@ViewBuilder
func libraryNavigationLink(_ library: Library) -> some View {
if #available(iOS 15, *) {
Expand All @@ -113,6 +76,6 @@ public struct LicenseListView: View {
public extension LicenseListView {
init(bundle: Bundle = .main, useUINavigationController: Bool = false) {
let url = bundle.url(forResource: "license-list", withExtension: "plist") ?? URL(fileURLWithPath: "/")
self.init(fileURL: url, useUINavigationController: useUINavigationController)
self.init(fileURL: url, useUINavigationController: useUINavigationController, navigationHandler: { _ in })
}
}
19 changes: 15 additions & 4 deletions Sources/LicenseList/LicenseListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import SwiftUI

public class LicenseListViewController: UIViewController {
let fileURL: URL
let id: UUID

public init(fileURL: URL) {
self.fileURL = fileURL
self.id = UUID()
super.init(nibName: nil, bundle: nil)
}

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

public override func viewDidLoad() {
super.viewDidLoad()

let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true, id: id)
let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true) { [weak self] library in
self?.navigateTo(library: library)
}
let vc = UIHostingController(rootView: licenseListView)
self.addChild(vc)
self.view.addSubview(vc.view)
Expand All @@ -42,4 +41,16 @@ public class LicenseListViewController: UIViewController {
vc.view.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
vc.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}

private func navigateTo(library: Library) {
let hostingController = UIHostingController(rootView: Group {
if #available(iOS 15, *) {
LicenseView(library: library)
} else {
LegacyLicenseView(library: library)
}
})
hostingController.title = library.name
self.navigationController?.pushViewController(hostingController, animated: true)
}
}

0 comments on commit 4f203c8

Please sign in to comment.