Remove the legacy ActivityIndicator/ProgressIndicator, use ProrgessView

This commit is contained in:
DreamPiggy 2022-12-26 17:46:33 +08:00
parent 2909b0027a
commit 63e1aebbf6
5 changed files with 18 additions and 245 deletions

View File

@ -54,10 +54,8 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/SDWebImageSwiftUIDemo-watchOS WatchKit App">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "32E529362348A0DD00EA46FF"
@ -65,7 +63,7 @@
BlueprintName = "SDWebImageSwiftUIDemo-watchOS WatchKit App"
ReferencedContainer = "container:SDWebImageSwiftUI.xcodeproj">
</BuildableReference>
</RemoteRunnable>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
@ -73,10 +71,8 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/SDWebImageSwiftUIDemo-watchOS WatchKit App">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "32E529362348A0DD00EA46FF"
@ -84,16 +80,7 @@
BlueprintName = "SDWebImageSwiftUIDemo-watchOS WatchKit App"
ReferencedContainer = "container:SDWebImageSwiftUI.xcodeproj">
</BuildableReference>
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "32E529362348A0DD00EA46FF"
BuildableName = "SDWebImageSwiftUIDemo-watchOS WatchKit App.app"
BlueprintName = "SDWebImageSwiftUIDemo-watchOS WatchKit App"
ReferencedContainer = "container:SDWebImageSwiftUI.xcodeproj">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">

View File

@ -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<EmptyView, EmptyView> {
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 = [

View File

@ -1,82 +0,0 @@
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <lizhuoli1126@126.com>
*
* 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<Bool>, 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<ActivityIndicator>) -> 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<ActivityIndicator>) {
isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
}
#endif
#if os(macOS)
public func makeNSView(context: NSViewRepresentableContext<ActivityIndicator>) -> 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<ActivityIndicator>) {
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

View File

@ -54,45 +54,44 @@ public struct IndicatorViewModifier<T> : 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<T> {
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<T> {
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<T> {
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<T> {
Indicator { isAnimating, progress in
ProgressIndicator(isAnimating, progress: progress, style: style)
AnyView(ProgressView(value: progress.wrappedValue).progressViewStyle(style).opacity(isAnimating.wrappedValue ? 1 : 0))
}
}
}
#endif

View File

@ -1,114 +0,0 @@
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <lizhuoli1126@126.com>
*
* 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<Bool>, progress: Binding<Double>, 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<ProgressIndicator>) -> 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<ProgressIndicator>) {
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<ProgressIndicator>) -> 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<ProgressIndicator>) {
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