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 {
|
} else {
|
||||||
#if os(macOS) || os(iOS) || os(tvOS)
|
#if os(macOS) || os(iOS) || os(tvOS)
|
||||||
WebImage(url: URL(string:url))
|
WebImage(url: URL(string:url))
|
||||||
.indicator { isAnimating, _ in
|
.indicator(.activity)
|
||||||
ActivityIndicator(isAnimating)
|
|
||||||
}
|
|
||||||
.resizable()
|
.resizable()
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
|
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
|
||||||
|
|
|
@ -57,18 +57,18 @@ struct DetailView: View {
|
||||||
} else {
|
} else {
|
||||||
#if os(macOS) || os(iOS) || os(tvOS)
|
#if os(macOS) || os(iOS) || os(tvOS)
|
||||||
WebImage(url: URL(string:url), options: [.progressiveLoad])
|
WebImage(url: URL(string:url), options: [.progressiveLoad])
|
||||||
.indicator { isAnimating, progress in
|
.indicator(.progress)
|
||||||
ProgressIndicator(isAnimating, progress: progress)
|
|
||||||
}
|
|
||||||
.resizable()
|
.resizable()
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
#else
|
#else
|
||||||
WebImage(url: URL(string:url), options: [.progressiveLoad])
|
WebImage(url: URL(string:url), options: [.progressiveLoad])
|
||||||
.indicator { isAnimating, progress in
|
.indicator(
|
||||||
|
Indicator { isAnimating, progress in
|
||||||
ProgressBar(value: progress)
|
ProgressBar(value: progress)
|
||||||
.foregroundColor(.blue)
|
.foregroundColor(.blue)
|
||||||
.frame(maxHeight: 6)
|
.frame(maxHeight: 6)
|
||||||
}
|
}
|
||||||
|
)
|
||||||
.resizable()
|
.resizable()
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
#endif
|
#endif
|
||||||
|
|
10
README.md
10
README.md
|
@ -61,9 +61,9 @@ let package = Package(
|
||||||
|
|
||||||
### Using `WebImage` to load network image
|
### Using `WebImage` to load network image
|
||||||
|
|
||||||
- [x] Supports the placeholder and detail options control for image loading as SDWebImage
|
- [x] Supports placeholder and detail options control for image loading as SDWebImage
|
||||||
- [x] Supports the success/failure/progress changes event for custom handling
|
- [x] Supports success/failure/progress changes event for custom handling
|
||||||
- [x] Supports the indicator with activity/progress indicator and customization
|
- [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.
|
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
|
.onSuccess { image, cacheType in
|
||||||
// Success
|
// Success
|
||||||
}
|
}
|
||||||
.indicator { isAnimating, _ in
|
.indicator(.activity) // Activity Indicator
|
||||||
ActivityIndicator(isAnimating) // Activity Indicator
|
|
||||||
}
|
|
||||||
.resizable()
|
.resizable()
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
.frame(width: 300, height: 300, alignment: .center)
|
.frame(width: 300, height: 300, alignment: .center)
|
||||||
|
|
|
@ -9,16 +9,36 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
/// A container view to hold the indicator builder
|
/// A type to build the indicator
|
||||||
public struct Indicator : View {
|
public struct Indicator {
|
||||||
var builder: (Binding<Bool>, Binding<CGFloat>) -> AnyView
|
var builder: (Binding<Bool>, Binding<CGFloat>) -> AnyView
|
||||||
public typealias Body = Never
|
|
||||||
public var body: Never {
|
/// Create a indicator with builder
|
||||||
fatalError()
|
/// - 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 {
|
public init<T>(@ViewBuilder builder: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<CGFloat>) -> T) where T : View {
|
||||||
self.builder = { isAnimating, progress in
|
self.builder = { isAnimating, progress in
|
||||||
AnyView(builder(isAnimating, progress))
|
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 {
|
extension WebImage {
|
||||||
|
|
||||||
/// Associate a indicator when loading image with url
|
/// Associate a indicator when loading image with url
|
||||||
/// - Parameter builder: builder description
|
/// - Parameter indicator: The indicator type, see `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.
|
public func indicator(_ indicator: Indicator) -> WebImage {
|
||||||
/// - 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 {
|
|
||||||
var result = self
|
var result = self
|
||||||
result.indicator = Indicator(builder: builder)
|
result.indicator = indicator
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue