Changing the indicator API to use the dot syntax, instead of that closure syntax, which is more convenient and swifty
This commit is contained in:
parent
896627d381
commit
63b6da1d5e
|
@ -96,9 +96,7 @@ struct ContentView: View {
|
|||
} else {
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
WebImage(url: URL(string:url))
|
||||
.indicator { isAnimating, _ in
|
||||
ActivityIndicator(isAnimating)
|
||||
}
|
||||
.indicator(.activity)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
|
||||
|
|
|
@ -57,18 +57,18 @@ struct DetailView: View {
|
|||
} else {
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
WebImage(url: URL(string:url), options: [.progressiveLoad])
|
||||
.indicator { isAnimating, progress in
|
||||
ProgressIndicator(isAnimating, progress: progress)
|
||||
}
|
||||
.indicator(.progress)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
#else
|
||||
WebImage(url: URL(string:url), options: [.progressiveLoad])
|
||||
.indicator { isAnimating, progress in
|
||||
ProgressBar(value: progress)
|
||||
.foregroundColor(.blue)
|
||||
.frame(maxHeight: 6)
|
||||
}
|
||||
.indicator(
|
||||
Indicator { isAnimating, progress in
|
||||
ProgressBar(value: progress)
|
||||
.foregroundColor(.blue)
|
||||
.frame(maxHeight: 6)
|
||||
}
|
||||
)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
#endif
|
||||
|
|
|
@ -16,7 +16,7 @@ public struct ProgressBar: View {
|
|||
GeometryReader { geometry in
|
||||
ZStack(alignment: .leading) {
|
||||
Rectangle()
|
||||
.frame(width: geometry.size.width)
|
||||
.frame(width: geometry.size.width)
|
||||
.opacity(0.3)
|
||||
Rectangle()
|
||||
.frame(width: geometry.size.width * self.value)
|
||||
|
|
10
README.md
10
README.md
|
@ -61,9 +61,9 @@ let package = Package(
|
|||
|
||||
### Using `WebImage` to load network image
|
||||
|
||||
- [x] Supports the placeholder and detail options control for image loading as SDWebImage
|
||||
- [x] Supports the success/failure/progress changes event for custom handling
|
||||
- [x] Supports the indicator with activity/progress indicator and customization
|
||||
- [x] Supports placeholder and detail options control for image loading as SDWebImage
|
||||
- [x] Supports success/failure/progress changes event for custom handling
|
||||
- [x] Supports indicator with activity/progress indicator and customization
|
||||
|
||||
Note: This `WebImage` using `Image` for internal implementation, which is the best compatible for SwiftUI layout and animation system. But it supports static image format only, because unlike `UIImageView` in UIKit, SwiftUI's `Image` does not support animation.
|
||||
|
||||
|
@ -73,9 +73,7 @@ var body: some View {
|
|||
.onSuccess { image, cacheType in
|
||||
// Success
|
||||
}
|
||||
.indicator { isAnimating, _ in
|
||||
ActivityIndicator(isAnimating) // Activity Indicator
|
||||
}
|
||||
.indicator(.activity) // Activity Indicator
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(width: 300, height: 300, alignment: .center)
|
||||
|
|
|
@ -9,16 +9,36 @@
|
|||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
/// A container view to hold the indicator builder
|
||||
public struct Indicator : View {
|
||||
/// A type to build the indicator
|
||||
public struct Indicator {
|
||||
var builder: (Binding<Bool>, Binding<CGFloat>) -> AnyView
|
||||
public typealias Body = Never
|
||||
public var body: Never {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
/// Create a indicator with builder
|
||||
/// - Parameter builder: A builder to build indicator
|
||||
/// - Parameter isAnimating: A Binding to control the animation. If image is during loading, the value is true, else (like start loading) the value is false.
|
||||
/// - Parameter progress: A Binding to control the progress during loading. If no progress can be reported, the value is 0.
|
||||
/// Associate a indicator when loading image with url
|
||||
public init<T>(@ViewBuilder builder: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<CGFloat>) -> T) where T : View {
|
||||
self.builder = { isAnimating, progress in
|
||||
AnyView(builder(isAnimating, progress))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(iOS)
|
||||
extension Indicator {
|
||||
/// Activity Indicator
|
||||
public static var activity: Indicator {
|
||||
Indicator { isAnimating, _ in
|
||||
ActivityIndicator(isAnimating)
|
||||
}
|
||||
}
|
||||
|
||||
/// Progress Indicator
|
||||
public static var progress: Indicator {
|
||||
Indicator { isAnimating, progress in
|
||||
ProgressIndicator(isAnimating, progress: progress)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -172,12 +172,10 @@ extension WebImage {
|
|||
extension WebImage {
|
||||
|
||||
/// Associate a indicator when loading image with url
|
||||
/// - Parameter builder: builder description
|
||||
/// - Parameter isAnimating: A Binding to control the animation. If image is during loading, the value is true, else (like start loading) the value is false.
|
||||
/// - Parameter progress: A Binding to control the progress during loading. If no progress can be reported, the value is 0.
|
||||
public func indicator<T>(_ builder: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<CGFloat>) -> T) -> WebImage where T : View {
|
||||
/// - Parameter indicator: The indicator type, see `Indicator`
|
||||
public func indicator(_ indicator: Indicator) -> WebImage {
|
||||
var result = self
|
||||
result.indicator = Indicator(builder: builder)
|
||||
result.indicator = indicator
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue