Skip to content

Commit

Permalink
Added loading state to button.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Mar 12, 2022
1 parent 92414eb commit df00213
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,33 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/(null)">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F43F8319265791A0001D9B3D"
BuildableName = "watchOS Example.app"
BlueprintName = "watchOS Example"
ReferencedContainer = "container:SparrowKit.xcodeproj">
</BuildableReference>
</RemoteRunnable>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/(null)">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F43F8319265791A0001D9B3D"
BuildableName = "watchOS Example.app"
BlueprintName = "watchOS Example"
ReferencedContainer = "container:SparrowKit.xcodeproj">
</BuildableReference>
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F43F8319265791A0001D9B3D"
BuildableName = "watchOS Example.app"
BlueprintName = "watchOS Example"
ReferencedContainer = "container:SparrowKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
40 changes: 40 additions & 0 deletions Sources/SparrowKit/UIKit/Classes/Buttons/SPButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import UIKit
*/
open class SPButton: UIButton {

// MARK: - Views

internal var activityIndicatorView: UIActivityIndicatorView?

// MARK: - Init

public init() {
Expand All @@ -56,6 +60,39 @@ open class SPButton: UIButton {
*/
open func commonInit() {}

// MARK: - Public

open func setLoading(_ state: Bool) {

// Validate

if state {
if activityIndicatorView?.isAnimating ?? false { return }
} else {
if activityIndicatorView == nil { return }
}

// Process

let contentViews = subviews

if state {
if activityIndicatorView == nil {
let activityIndicatorView = UIActivityIndicatorView()
addSubviews(activityIndicatorView)
self.activityIndicatorView = activityIndicatorView
}
contentViews.forEach({ $0.isHidden = true })
activityIndicatorView?.startAnimating()
layoutSubviews()
} else {
contentViews.forEach({ $0.isHidden = false })
activityIndicatorView?.stopAnimating()
activityIndicatorView?.removeFromSuperview()
activityIndicatorView = nil
}
}

// MARK: - Layout

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

open override func layoutSubviews() {
super.layoutSubviews()

activityIndicatorView?.setToCenter()

if let inset = titleImageInset {
if imageView?.image == nil {
imageEdgeInsets.right = 0
Expand Down

0 comments on commit df00213

Please sign in to comment.