Skip to content

Commit df00213

Browse files
committed
Added loading state to button.
1 parent 92414eb commit df00213

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

Example Apps/SparrowKit.xcodeproj/xcshareddata/xcschemes/watchOS Example.xcscheme

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,33 @@
5454
debugDocumentVersioning = "YES"
5555
debugServiceExtension = "internal"
5656
allowLocationSimulation = "YES">
57-
<RemoteRunnable
58-
runnableDebuggingMode = "2"
59-
BundleIdentifier = "com.apple.Carousel"
60-
RemotePath = "/(null)">
57+
<BuildableProductRunnable
58+
runnableDebuggingMode = "0">
6159
<BuildableReference
6260
BuildableIdentifier = "primary"
6361
BlueprintIdentifier = "F43F8319265791A0001D9B3D"
6462
BuildableName = "watchOS Example.app"
6563
BlueprintName = "watchOS Example"
6664
ReferencedContainer = "container:SparrowKit.xcodeproj">
6765
</BuildableReference>
68-
</RemoteRunnable>
66+
</BuildableProductRunnable>
6967
</LaunchAction>
7068
<ProfileAction
7169
buildConfiguration = "Release"
7270
shouldUseLaunchSchemeArgsEnv = "YES"
7371
savedToolIdentifier = ""
7472
useCustomWorkingDirectory = "NO"
7573
debugDocumentVersioning = "YES">
76-
<RemoteRunnable
77-
runnableDebuggingMode = "2"
78-
BundleIdentifier = "com.apple.Carousel"
79-
RemotePath = "/(null)">
74+
<BuildableProductRunnable
75+
runnableDebuggingMode = "0">
8076
<BuildableReference
8177
BuildableIdentifier = "primary"
8278
BlueprintIdentifier = "F43F8319265791A0001D9B3D"
8379
BuildableName = "watchOS Example.app"
8480
BlueprintName = "watchOS Example"
8581
ReferencedContainer = "container:SparrowKit.xcodeproj">
8682
</BuildableReference>
87-
</RemoteRunnable>
88-
<MacroExpansion>
89-
<BuildableReference
90-
BuildableIdentifier = "primary"
91-
BlueprintIdentifier = "F43F8319265791A0001D9B3D"
92-
BuildableName = "watchOS Example.app"
93-
BlueprintName = "watchOS Example"
94-
ReferencedContainer = "container:SparrowKit.xcodeproj">
95-
</BuildableReference>
96-
</MacroExpansion>
83+
</BuildableProductRunnable>
9784
</ProfileAction>
9885
<AnalyzeAction
9986
buildConfiguration = "Debug">

Sources/SparrowKit/UIKit/Classes/Buttons/SPButton.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import UIKit
3131
*/
3232
open class SPButton: UIButton {
3333

34+
// MARK: - Views
35+
36+
internal var activityIndicatorView: UIActivityIndicatorView?
37+
3438
// MARK: - Init
3539

3640
public init() {
@@ -56,6 +60,39 @@ open class SPButton: UIButton {
5660
*/
5761
open func commonInit() {}
5862

63+
// MARK: - Public
64+
65+
open func setLoading(_ state: Bool) {
66+
67+
// Validate
68+
69+
if state {
70+
if activityIndicatorView?.isAnimating ?? false { return }
71+
} else {
72+
if activityIndicatorView == nil { return }
73+
}
74+
75+
// Process
76+
77+
let contentViews = subviews
78+
79+
if state {
80+
if activityIndicatorView == nil {
81+
let activityIndicatorView = UIActivityIndicatorView()
82+
addSubviews(activityIndicatorView)
83+
self.activityIndicatorView = activityIndicatorView
84+
}
85+
contentViews.forEach({ $0.isHidden = true })
86+
activityIndicatorView?.startAnimating()
87+
layoutSubviews()
88+
} else {
89+
contentViews.forEach({ $0.isHidden = false })
90+
activityIndicatorView?.stopAnimating()
91+
activityIndicatorView?.removeFromSuperview()
92+
activityIndicatorView = nil
93+
}
94+
}
95+
5996
// MARK: - Layout
6097

6198
/**
@@ -68,6 +105,9 @@ open class SPButton: UIButton {
68105

69106
open override func layoutSubviews() {
70107
super.layoutSubviews()
108+
109+
activityIndicatorView?.setToCenter()
110+
71111
if let inset = titleImageInset {
72112
if imageView?.image == nil {
73113
imageEdgeInsets.right = 0

0 commit comments

Comments
 (0)