Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI #80

Closed
vtourraine opened this issue Mar 18, 2021 · 3 comments · Fixed by #83
Closed

SwiftUI #80

vtourraine opened this issue Mar 18, 2021 · 3 comments · Fixed by #83

Comments

@vtourraine
Copy link
Owner

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

  • Is it better to offer it as a new option, or create a distinct library?
  • Do we need to update the model for a Combine API?

Any ideas and suggestions are welcome!

@sgade
Copy link

sgade commented Apr 9, 2021

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
Copy link
Owner Author

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
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants