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

Use generics to replace AnyView #22

Closed
johnnythedoggie opened this issue Feb 10, 2021 · 6 comments
Closed

Use generics to replace AnyView #22

johnnythedoggie opened this issue Feb 10, 2021 · 6 comments
Assignees
Labels
enhancement Feature request that enhances features and will be worked on wontfix This will not be worked on

Comments

@johnnythedoggie
Copy link

johnnythedoggie commented Feb 10, 2021

Using AnyView is bad practice, and if it can be replaced (and I believe it can in this case) it should be. With just a few changes, MainView.swift would be modified to look like this:

struct MainView<BodyView: View>: View {
    private var showModal: Binding<Bool>
    private var bodyView: BodyView
    init(for bodyView: BodyView, show showModal: Binding<Bool>) {
        self.bodyView = bodyView
        self.showModal = showModal
    }

    var body: some View {
        bodyView
            .sheet(isPresented: showModal, content: {
                ModalView(showModal: showModal)
            })
            
    }
}

AnyView is used in many places and most (if not all) of them can be replaced with generics. This won't break anyone's existing code either, the type will not be MainView, it will be MainView<AnyView>. AnyView should be switched for generics in JMPermission (for the imageIcon) as well. This helps SwiftUI update views more efficiently and prevents always casting the image to AnyView like is currently in the README:

.setPermissionComponent(for: .camera, 
                        image: AnyView(Image(systemName: "camera.fill")), 
                        title: "Camcorder",
                        description: "App needs to record videos")
@jevonmao jevonmao added enhancement Feature request that enhances features and will be worked on help wanted Extra attention is needed labels Feb 11, 2021
@jevonmao jevonmao self-assigned this Feb 11, 2021
@jevonmao jevonmao modified the milestones: 1.0.1 release, 1.0.2 release Feb 11, 2021
@jevonmao
Copy link
Owner

@johnnythedoggie Thank you for bringing this up! I absolutely agree with you. Would you like to tackle this and open a pull request? If not, I will take care of this.

@jevonmao
Copy link
Owner

@johnnythedoggie I would appreciate it if you can work on this and open a PR. It looks nice to have more than 1 contributor. New version is out, my goal is to implement this by v1.1.1

@jevonmao jevonmao added the good first issue Good for newcomers label Feb 11, 2021
@johnnythedoggie
Copy link
Author

@jevonmao I'm still learning GitHub, so it may take me a bit. I do plan to open a PR and make the change, but I'm confused how to do that. I'll figure it out soon (hopefully) and go for it. I'll ask if I have questions.

@jevonmao
Copy link
Owner

jevonmao commented Feb 11, 2021

@johnnythedoggie No problem at all. Pull request is actually a Github exclusive feature (not Git), and everything related your will do on the Github website. A pull request or PR is simply a request to contribute your code changes. As the owner, I will be able to see all your additions and deletions to the code. At the end, I will "merge" your code into this existing repository so your hard work pays off.

If you are not sure, read this article that guides you through the entire process.

For step 3, simply name the branch #22-any-view to reference this issue number.

Make sure you also have Xcode installed to code on the project.

@jevonmao
Copy link
Owner

@johnnythedoggie Are you still interested in working on this issue? Just give me a status update please. If not, I will go ahead and do it.

@jevonmao jevonmao added easy quest and removed help wanted Extra attention is needed labels Feb 16, 2021
@jevonmao jevonmao removed this from the 1.2.1 release milestone Feb 20, 2021
@jevonmao jevonmao added wontfix This will not be worked on and removed easy quest good first issue Good for newcomers labels Feb 20, 2021
@jevonmao
Copy link
Owner

I tried to do this, but for variety reasons, the JMPermission structure simply will not allow the pass in of another type other than Image if a generic is used to replace AnyView. AnyView is a completelu type erased view, while a generic only works with 1 type that cannot be changed at runtime. Will close for now due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request that enhances features and will be worked on wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants