From a6a6d58073c23511009c63e9fbd5d8064e7dfc52 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 28 Oct 2019 13:04:18 +0800 Subject: [PATCH 1/2] Use View Modifier instead of new view type for indicator, simplify the code --- SDWebImageSwiftUI/Classes/Indicator/Indicator.swift | 13 +++---------- SDWebImageSwiftUI/Classes/WebImage.swift | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift b/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift index c0ebede1..3dc0b5fe 100644 --- a/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift +++ b/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift @@ -23,16 +23,15 @@ public struct Indicator where T : View { } } -/// A implementation detail View with indicator +/// A implementation detail View Modifier with indicator /// SwiftUI View Modifier construced by using a internal View type which modify the `body` /// It use type system to represent the view hierarchy, and Swift `some View` syntax to hide the type detail for users -struct IndicatorView : View where T : View, Content : View { +struct IndicatorViewModifier : ViewModifier where T : View { @ObservedObject var imageManager: ImageManager var indicator: Indicator - var content: Content - var body: some View { + func body(content: Content) -> some View { if (imageManager.image != nil) && !imageManager.isLoading { // Disable Indiactor return AnyView(content) @@ -46,12 +45,6 @@ struct IndicatorView : View where T : View, Content : View { ) } } - - public init(_ view: Content, indicator: Indicator, imageManager: ImageManager) { - self.content = view - self.indicator = indicator - self.imageManager = imageManager - } } #if os(macOS) || os(iOS) || os(tvOS) diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index 0a33cf4b..7a6453f8 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -138,7 +138,7 @@ extension WebImage { /// Associate a indicator when loading image with url /// - Parameter indicator: The indicator type, see `Indicator` public func indicator(_ indicator: Indicator) -> some View where T : View { - return IndicatorView(self, indicator: indicator, imageManager: imageManager) + return self.modifier(IndicatorViewModifier(imageManager: imageManager, indicator: indicator)) } /// Associate a indicator when loading image with url, convenient method with block From 14bb4be6a8fb5b2b367a46b4151acadae4064877 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 28 Oct 2019 13:04:47 +0800 Subject: [PATCH 2/2] The progress indicator bar style is only available on iOS --- SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift b/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift index 2cd26973..58c15d64 100644 --- a/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift +++ b/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift @@ -35,7 +35,7 @@ public struct ProgressIndicator: PlatformViewRepresentable { case .bar: progressStyle = .bar #endif - default: + case .default: progressStyle = .default } let uiView = ProgressIndicatorWrapper() @@ -99,7 +99,9 @@ public struct ProgressIndicator: PlatformViewRepresentable { extension ProgressIndicator { public enum Style { case `default` + #if os(iOS) case bar + #endif } } #endif