dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.1k stars 96 forks source link

Use ViewBuilder - please comment #133

Closed dmytro-anokhin closed 3 years ago

dmytro-anokhin commented 3 years ago

Is your feature request related to a problem? Please describe. Since the initial release of URLImage package, ViewBuilder attribute gone public, and there are some benefits for using it. Using it will allow conditionally return views of different types.

In this example if the image is wider than 100px we make it resizable.

URLImage(url: url) { image, info in
    Group {
        if info.size.width > 100.0 {
            image
                .resizable()
                .aspectRatio(contentMode: .fit)
        }
        else {
            image
        }
    }
}

Using ViewBuilder attribute will allow to return different views:

URLImage(url: url) { image, info in
    if info.size.width > 100.0 {
        image
            .resizable()
            .aspectRatio(contentMode: .fit)
    }
    else {
        image
    }
}

Describe the solution you'd like Add @ViewBuilder to URLImage initializers.

Current:

init(url: URL, content: @escaping (_ image: Image) -> Content)

New:

init(url: URL, @ViewBuilder content: @escaping (_ image: Image) -> Content)

Describe alternatives you've considered Keep it as is.

Additional context ViewBuilder attribute actually allows to provide multiple child views. It is unclear what should be expected behaviour when this is the case. Simply ignore it, wrap in a Group, or a ZStack, etc. Let me know what you think.