From 63b6da1d5e636ef3f6cd95d976f8938f6b044b0c Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 26 Oct 2019 04:26:07 +0800 Subject: [PATCH] Changing the indicator API to use the dot syntax, instead of that closure syntax, which is more convenient and swifty --- .../SDWebImageSwiftUIDemo/ContentView.swift | 4 +-- .../SDWebImageSwiftUIDemo/DetailView.swift | 16 +++++----- .../SDWebImageSwiftUIDemo/ProgressBar.swift | 2 +- README.md | 10 +++--- .../Classes/Indicator/Indicator.swift | 32 +++++++++++++++---- SDWebImageSwiftUI/Classes/WebImage.swift | 8 ++--- 6 files changed, 43 insertions(+), 29 deletions(-) diff --git a/Example/SDWebImageSwiftUIDemo/ContentView.swift b/Example/SDWebImageSwiftUIDemo/ContentView.swift index df566ad..4c2287c 100644 --- a/Example/SDWebImageSwiftUIDemo/ContentView.swift +++ b/Example/SDWebImageSwiftUIDemo/ContentView.swift @@ -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) diff --git a/Example/SDWebImageSwiftUIDemo/DetailView.swift b/Example/SDWebImageSwiftUIDemo/DetailView.swift index 0b9b5a5..c5e1cf9 100644 --- a/Example/SDWebImageSwiftUIDemo/DetailView.swift +++ b/Example/SDWebImageSwiftUIDemo/DetailView.swift @@ -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 diff --git a/Example/SDWebImageSwiftUIDemo/ProgressBar.swift b/Example/SDWebImageSwiftUIDemo/ProgressBar.swift index d8ed095..14c231d 100644 --- a/Example/SDWebImageSwiftUIDemo/ProgressBar.swift +++ b/Example/SDWebImageSwiftUIDemo/ProgressBar.swift @@ -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) diff --git a/README.md b/README.md index 0e333c6..f087f55 100644 --- a/README.md +++ b/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) diff --git a/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift b/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift index 7e58837..f52b37e 100644 --- a/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift +++ b/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift @@ -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, Binding) -> 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(@ViewBuilder builder: @escaping (_ isAnimating: Binding, _ progress: Binding) -> 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 diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index 423d3a4..87f887d 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -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(_ builder: @escaping (_ isAnimating: Binding, _ progress: Binding) -> 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 } }