From 63e1aebbf6dc3d435fc345460c70e816fd2016da Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 26 Dec 2022 17:46:33 +0800 Subject: [PATCH] Remove the legacy ActivityIndicator/ProgressIndicator, use ProrgessView --- ...eSwiftUIDemo-watchOS WatchKit App.xcscheme | 25 +--- .../SDWebImageSwiftUIDemo/ContentView.swift | 17 --- .../Classes/Indicator/ActivityIndicator.swift | 82 ------------- .../Classes/Indicator/Indicator.swift | 25 ++-- .../Classes/Indicator/ProgressIndicator.swift | 114 ------------------ 5 files changed, 18 insertions(+), 245 deletions(-) delete mode 100644 SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift delete mode 100644 SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift diff --git a/Example/SDWebImageSwiftUI.xcodeproj/xcshareddata/xcschemes/SDWebImageSwiftUIDemo-watchOS WatchKit App.xcscheme b/Example/SDWebImageSwiftUI.xcodeproj/xcshareddata/xcschemes/SDWebImageSwiftUIDemo-watchOS WatchKit App.xcscheme index 3d1f081..65df20b 100644 --- a/Example/SDWebImageSwiftUI.xcodeproj/xcshareddata/xcschemes/SDWebImageSwiftUIDemo-watchOS WatchKit App.xcscheme +++ b/Example/SDWebImageSwiftUI.xcodeproj/xcshareddata/xcschemes/SDWebImageSwiftUIDemo-watchOS WatchKit App.xcscheme @@ -54,10 +54,8 @@ debugDocumentVersioning = "YES" debugServiceExtension = "internal" allowLocationSimulation = "YES"> - + - + - + - - - - - + diff --git a/Example/SDWebImageSwiftUIDemo/ContentView.swift b/Example/SDWebImageSwiftUIDemo/ContentView.swift index a3619c9..418bad7 100644 --- a/Example/SDWebImageSwiftUIDemo/ContentView.swift +++ b/Example/SDWebImageSwiftUIDemo/ContentView.swift @@ -17,23 +17,6 @@ class UserSettings: ObservableObject { #endif } -#if os(watchOS) -@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -extension Indicator where T == ProgressView { - static var activity: Indicator { - Indicator { isAnimating, progress in - ProgressView() - } - } - - static var progress: Indicator { - Indicator { isAnimating, progress in - ProgressView(value: progress.wrappedValue) - } - } -} -#endif - // Test Switching url using @State struct ContentView2: View { @State var imageURLs = [ diff --git a/SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift b/SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift deleted file mode 100644 index 2592ebd..0000000 --- a/SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift +++ /dev/null @@ -1,82 +0,0 @@ -/* -* This file is part of the SDWebImage package. -* (c) DreamPiggy -* -* For the full copyright and license information, please view the LICENSE -* file that was distributed with this source code. -*/ - -import SwiftUI - -#if os(macOS) || os(iOS) || os(tvOS) -/// An activity indicator (system style) -@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -public struct ActivityIndicator: PlatformViewRepresentable { - @Binding var isAnimating: Bool - var style: Style - - /// Create the indicator with animation binding and style - /// - Parameters: - /// - isAnimating: The binding to control the animation - /// - style: The indicator style - public init(_ isAnimating: Binding, style: Style = .medium) { - self._isAnimating = isAnimating - self.style = style - } - - #if os(macOS) - public typealias NSViewType = NSProgressIndicator - #elseif os(iOS) || os(tvOS) - public typealias UIViewType = UIActivityIndicatorView - #endif - - #if os(iOS) || os(tvOS) - public func makeUIView(context: UIViewRepresentableContext) -> UIActivityIndicatorView { - let activityStyle: UIActivityIndicatorView.Style - switch style { - case .medium: - activityStyle = .medium - case .large: - activityStyle = .large - } - let indicator = UIActivityIndicatorView(style: activityStyle) - indicator.hidesWhenStopped = true - return indicator - } - - public func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext) { - isAnimating ? uiView.startAnimating() : uiView.stopAnimating() - } - #endif - - #if os(macOS) - public func makeNSView(context: NSViewRepresentableContext) -> NSProgressIndicator { - let controlSize: NSControl.ControlSize - switch style { - case .medium: - controlSize = .small - case .large: - controlSize = .regular - } - let indicator = NSProgressIndicator() - indicator.style = .spinning - indicator.controlSize = controlSize - indicator.isDisplayedWhenStopped = false - return indicator - } - - public func updateNSView(_ nsView: NSProgressIndicator, context: NSViewRepresentableContext) { - isAnimating ? nsView.startAnimation(nil) : nsView.stopAnimation(nil) - } - - #endif -} - -@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -extension ActivityIndicator { - public enum Style { - case medium - case large - } -} -#endif diff --git a/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift b/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift index 69fbb4d..499aafd 100644 --- a/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift +++ b/SDWebImageSwiftUI/Classes/Indicator/Indicator.swift @@ -54,45 +54,44 @@ public struct IndicatorViewModifier : ViewModifier where T : View { public func body(content: Content) -> some View { ZStack { - content.overlay(overlay, alignment: .center) + content + overlay } } } -#if os(macOS) || os(iOS) || os(tvOS) @available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -extension Indicator where T == ActivityIndicator { +extension Indicator where T == AnyView { /// Activity Indicator - public static var activity: Indicator { + public static var activity: Indicator { Indicator { isAnimating, _ in - ActivityIndicator(isAnimating) + AnyView(ProgressView().opacity(isAnimating.wrappedValue ? 1 : 0)) } } /// Activity Indicator with style /// - Parameter style: style - public static func activity(style: ActivityIndicator.Style) -> Indicator { + public static func activity(style: any ProgressViewStyle) -> Indicator { Indicator { isAnimating, _ in - ActivityIndicator(isAnimating, style: style) + AnyView(ProgressView().progressViewStyle(style).opacity(isAnimating.wrappedValue ? 1 : 0)) } } } @available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -extension Indicator where T == ProgressIndicator { +extension Indicator where T == AnyView { /// Progress Indicator - public static var progress: Indicator { + public static var progress: Indicator { Indicator { isAnimating, progress in - ProgressIndicator(isAnimating, progress: progress) + AnyView(ProgressView(value: progress.wrappedValue).opacity(isAnimating.wrappedValue ? 1 : 0)) } } /// Progress Indicator with style /// - Parameter style: style - public static func progress(style: ProgressIndicator.Style) -> Indicator { + public static func progress(style: any ProgressViewStyle) -> Indicator { Indicator { isAnimating, progress in - ProgressIndicator(isAnimating, progress: progress, style: style) + AnyView(ProgressView(value: progress.wrappedValue).progressViewStyle(style).opacity(isAnimating.wrappedValue ? 1 : 0)) } } } -#endif diff --git a/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift b/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift deleted file mode 100644 index a017066..0000000 --- a/SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift +++ /dev/null @@ -1,114 +0,0 @@ -/* -* This file is part of the SDWebImage package. -* (c) DreamPiggy -* -* For the full copyright and license information, please view the LICENSE -* file that was distributed with this source code. -*/ - -import SwiftUI - -#if os(macOS) || os(iOS) || os(tvOS) -/// A progress bar indicator (system style) -@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -public struct ProgressIndicator: PlatformViewRepresentable { - @Binding var isAnimating: Bool - @Binding var progress: Double - var style: Style - - /// Create indicator with animation binding, progress binding and the style - /// - Parameters: - /// - isAnimating: The binding to control the animation - /// - progress: The binding to update the progress - /// - style: The indicator style - public init(_ isAnimating: Binding, progress: Binding, style: Style = .default) { - self._isAnimating = isAnimating - self._progress = progress - self.style = style - } - - #if os(macOS) - public typealias NSViewType = ProgressIndicatorWrapper - #elseif os(iOS) || os(tvOS) - public typealias UIViewType = ProgressIndicatorWrapper - #endif - - #if os(iOS) || os(tvOS) - public func makeUIView(context: UIViewRepresentableContext) -> ProgressIndicatorWrapper { - let progressStyle: UIProgressView.Style - switch style { - #if os(iOS) - case .bar: - progressStyle = .bar - #endif - case .default: - progressStyle = .default - } - let uiView = ProgressIndicatorWrapper() - let view = uiView.wrapped - view.progressViewStyle = progressStyle - return uiView - } - - public func updateUIView(_ uiView: ProgressIndicatorWrapper, context: UIViewRepresentableContext) { - let view = uiView.wrapped - if isAnimating { - view.setProgress(Float(progress), animated: true) - } else { - if progress == 0 { - view.isHidden = false - view.progress = 0 - } else { - view.isHidden = true - view.progress = 1 - } - } - } - #endif - - #if os(macOS) - public func makeNSView(context: NSViewRepresentableContext) -> ProgressIndicatorWrapper { - let nsView = ProgressIndicatorWrapper() - let view = nsView.wrapped - view.style = .bar - view.isDisplayedWhenStopped = false - view.controlSize = .small - view.frame = CGRect(x: 0, y: 0, width: 160, height: 0) // Width from `UIProgressView` default width - view.sizeToFit() - view.autoresizingMask = [.maxXMargin, .minXMargin, .maxYMargin, .minYMargin] - return nsView - } - - public func updateNSView(_ nsView: ProgressIndicatorWrapper, context: NSViewRepresentableContext) { - let view = nsView.wrapped - if isAnimating { - view.isIndeterminate = false - view.doubleValue = Double(progress) * 100 - view.startAnimation(nil) - } else { - if progress == 0 { - view.isHidden = false - view.isIndeterminate = true - view.doubleValue = 0 - view.stopAnimation(nil) - } else { - view.isHidden = true - view.isIndeterminate = false - view.doubleValue = 100 - view.stopAnimation(nil) - } - } - } - #endif -} - -@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *) -extension ProgressIndicator { - public enum Style { - case `default` - #if os(iOS) - case bar - #endif - } -} -#endif