Skip to content

Supports indicator with style and convenience methods #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import SwiftUI
/// An activity indicator (system style)
public struct ActivityIndicator: PlatformViewRepresentable {
@Binding var isAnimating: Bool
var style: Style

public init(_ isAnimating: Binding<Bool>) {
public init(_ isAnimating: Binding<Bool>, style: Style = .medium) {
self._isAnimating = isAnimating
self.style = style
}

#if os(macOS)
Expand All @@ -25,7 +27,14 @@ public struct ActivityIndicator: PlatformViewRepresentable {

#if os(iOS) || os(tvOS)
public func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
let indicator = UIActivityIndicatorView(style: .medium)
let activityStyle: UIActivityIndicatorView.Style
switch style {
case .medium:
activityStyle = .medium
case .large:
activityStyle = .large
}
let indicator = UIActivityIndicatorView(style: activityStyle)
indicator.hidesWhenStopped = true
return indicator
}
Expand All @@ -37,8 +46,16 @@ public struct ActivityIndicator: PlatformViewRepresentable {

#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
}
Expand All @@ -49,4 +66,11 @@ public struct ActivityIndicator: PlatformViewRepresentable {

#endif
}

extension ActivityIndicator {
public enum Style {
case medium
case large
}
}
#endif
16 changes: 16 additions & 0 deletions SDWebImageSwiftUI/Classes/Indicator/Indicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ extension Indicator {
}
}

/// Activity Indicator with style
/// - Parameter style: style
public static func activity(style: ActivityIndicator.Style) -> Indicator {
Indicator { isAnimating, _ in
ActivityIndicator(isAnimating, style: style)
}
}

/// Progress Indicator
public static var progress: Indicator {
Indicator { isAnimating, progress in
ProgressIndicator(isAnimating, progress: progress)
}
}

/// Progress Indicator with style
/// - Parameter style: style
public static func progress(style: ProgressIndicator.Style) -> Indicator {
Indicator { isAnimating, progress in
ProgressIndicator(isAnimating, progress: progress, style: style)
}
}
}
#endif
22 changes: 20 additions & 2 deletions SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import SwiftUI
public struct ProgressIndicator: PlatformViewRepresentable {
@Binding var isAnimating: Bool
@Binding var progress: CGFloat
var style: Style

public init(_ isAnimating: Binding<Bool>, progress: Binding<CGFloat>) {
public init(_ isAnimating: Binding<Bool>, progress: Binding<CGFloat>, style: Style = .default) {
self._isAnimating = isAnimating
self._progress = progress
self.style = style
}

#if os(macOS)
Expand All @@ -27,9 +29,18 @@ public struct ProgressIndicator: PlatformViewRepresentable {

#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
default:
progressStyle = .default
}
let uiView = ProgressIndicatorWrapper()
let view = uiView.wrapped
view.progressViewStyle = .default
view.progressViewStyle = progressStyle
return uiView
}

Expand Down Expand Up @@ -81,4 +92,11 @@ public struct ProgressIndicator: PlatformViewRepresentable {
}
#endif
}

extension ProgressIndicator {
public enum Style {
case `default`
case bar
}
}
#endif
3 changes: 2 additions & 1 deletion SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ extension WebImage {
}
}

// Indicator
extension WebImage {

/// Associate a indicator when loading image with url
/// - Parameter indicator: The indicator type, see `Indicator`
public func indicator(_ indicator: Indicator) -> WebImage {
public func indicator(_ indicator: Indicator?) -> WebImage {
var result = self
result.indicator = indicator
return result
Expand Down