vtourraine / AcknowList

Acknowledgements screen displaying a list of licenses, for example from CocoaPods and Swift Package Manager dependencies.
MIT License
798 stars 60 forks source link

SwiftUI #80

Closed vtourraine closed 3 years ago

vtourraine commented 3 years ago

How should we update this library to offer a SwiftUI interface?

Any ideas and suggestions are welcome!

sgade commented 3 years ago

Would love to see a SwiftUI interface. I'm currently using version 2.0 in a SwiftUI NavigationView. It already works well to embed it using an UIViewControllerRepresentable. You could add this to the library to make it usable directly.

The creation is pretty straight forward. However, it requires to set a navigationBarTitle and edgesIgnoringSafeArea on the represented view controller. This is why I have a wrapper around the representable. I don't see why there would need to be a Combine API but maybe I'm just simply not using any of those APIs.

My example:


struct AcknowListView: View {

    var plistName: String

    var body: some View {
        AcknowListViewControllerViewWrapper(plistName: plistName)
            .navigationBarTitle("Acknowledgements")
            .edgesIgnoringSafeArea(.all)
    }

}

private struct AcknowListViewControllerViewWrapper: UIViewControllerRepresentable {

    /// The name of the plist file without its extension.
    public let plistName: String
    /// The list style for the internal table view.
    public let listStyle: UITableView.Style = .insetGrouped

    func makeUIViewController(context: UIViewControllerRepresentableContext<AcknowListViewControllerViewWrapper>) -> AcknowListViewController {
        var viewController: AcknowListViewController
        if let plistPath = Bundle.main.path(forResource: plistName, ofType: "plist") {
            viewController = AcknowListViewController(plistPath: plistPath, style: listStyle)
        } else {
            viewController = AcknowListViewController(acknowledgements: [], style: listStyle)
        }

        return viewController
    }

    func updateUIViewController(_ uiViewController: AcknowListViewController, context: UIViewControllerRepresentableContext<AcknowListViewControllerViewWrapper>) {
    }

}
vtourraine commented 3 years ago

That’s very interesting, thank you for sharing that piece of code!

I imagine offering a “true” SwiftUI implementation of the same UI would have its benefits, but it’s great to know it already works with UIViewControllerRepresentable.

vtourraine commented 3 years ago

You can check out the swiftui branch for a work-in-progress SwiftUI implementation.