Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Last active March 3, 2020 14:55
Show Gist options
  • Save anirudhamahale/5f3bab0d89df21728f4959643c761f47 to your computer and use it in GitHub Desktop.
Save anirudhamahale/5f3bab0d89df21728f4959643c761f47 to your computer and use it in GitHub Desktop.
EmptyDataSet
import UIKit
class EmptyDataSet: UIView {
private(set) var parentView: UIView!
private(set) var titleLabel: UILabel!
private(set) var descriptionLabel: UILabel!
private(set) var imageView: UIImageView!
private(set) var button: UIButton!
private var imageAnimation: CABasicAnimation {
let animation = CABasicAnimation(keyPath: "transform")
animation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(CGFloat(Double.pi/2), 0.0, 0.0, 1.0))
animation.duration = 0.25
animation.isCumulative = true
animation.repeatCount = MAXFLOAT
animation.isRemovedOnCompletion = false
return animation
}
var margin: CGFloat = 16.0
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white
parentView = UIView()
parentView.translatesAutoresizingMaskIntoConstraints = false
addSubview(parentView)
parentView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
parentView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func setProgress(title: String?, description: String?) {
var topConstraint: NSLayoutYAxisAnchor?
parentView.subviews.forEach { $0.removeFromSuperview() }
// Add Image
imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "Oval")
parentView.addSubview(imageView)
imageView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
imageView.topAnchor.constraint(equalTo: parentView.topAnchor, constant: 0).isActive = true
imageView.layer.add(imageAnimation, forKey: nil)
topConstraint = imageView.bottomAnchor
if let title = title {
// Add Title
titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textColor = .black
titleLabel.font = UIFont.boldSystemFont(ofSize: 24)
titleLabel.text = title
parentView.addSubview(titleLabel)
titleLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
titleLabel.topAnchor.constraint(equalTo: topConstraint!, constant: margin).isActive = true
topConstraint = titleLabel.bottomAnchor
}
if let description = description {
// Add Description
descriptionLabel = UILabel()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.textColor = .black
descriptionLabel.text = description
parentView.addSubview(descriptionLabel)
descriptionLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
descriptionLabel.topAnchor.constraint(equalTo: topConstraint!, constant: margin).isActive = true
topConstraint = descriptionLabel.bottomAnchor
}
parentView.bottomAnchor.constraint(equalTo: topConstraint!).isActive = true
}
func setNoData(image: UIImage?, title: String?, description: String?) {
var topConstraint = parentView.topAnchor
parentView.subviews.forEach { $0.removeFromSuperview() }
if let image = image {
// Add Image
imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = image
parentView.addSubview(imageView)
imageView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
imageView.topAnchor.constraint(equalTo: topConstraint, constant: 0).isActive = true
topConstraint = imageView.bottomAnchor
}
if let text = title {
// Add Title
titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textColor = .black
titleLabel.font = UIFont.boldSystemFont(ofSize: 24)
titleLabel.text = text
parentView.addSubview(titleLabel)
titleLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
titleLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true
topConstraint = titleLabel.bottomAnchor
}
if let text = description {
// Add Description
descriptionLabel = UILabel()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.textColor = .black
descriptionLabel.text = text
parentView.addSubview(descriptionLabel)
descriptionLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
descriptionLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true
topConstraint = descriptionLabel.bottomAnchor
}
parentView.bottomAnchor.constraint(equalTo: topConstraint).isActive = true
}
func setFailed(title: String?, description: String?, buttonTitle: String?, action: ()->()) {
var topConstraint = parentView.topAnchor
parentView.subviews.forEach { $0.removeFromSuperview() }
if let text = title {
// Add Title
titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textColor = .black
titleLabel.font = UIFont.boldSystemFont(ofSize: 24)
titleLabel.text = text
parentView.addSubview(titleLabel)
titleLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
titleLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true
topConstraint = titleLabel.bottomAnchor
}
if let text = description {
// Add Description
descriptionLabel = UILabel()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.textColor = .black
descriptionLabel.text = text
parentView.addSubview(descriptionLabel)
descriptionLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
descriptionLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true
topConstraint = descriptionLabel.bottomAnchor
}
if let text = buttonTitle {
button = UIButton()
button.backgroundColor = .black
button.layer.masksToBounds = true
button.setTitle(text, for: .normal)
button.setTitleColor(.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
parentView.addSubview(button)
button.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true
button.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true
button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 36, bottom: 10, right: 36)
topConstraint = button.bottomAnchor
button.layoutIfNeeded()
button.layer.cornerRadius = button.frame.size.height/2
}
parentView.bottomAnchor.constraint(equalTo: topConstraint).isActive = true
}
}
@anirudhamahale
Copy link
Author

anirudhamahale commented Mar 3, 2020

let emptyDataSet = EmptyDataSet()
emptyDataSet.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(emptyDataSet)
        
emptyDataSet.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
emptyDataSet.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
emptyDataSet.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
emptyDataSet.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
        
// emptyDataSet.setProgress(title: "Loading Projects", description: "Please wait while we load the projects....")
// emptyDataSet.setNoData(image: UIImage(named: "Oval")!, title: "No Projects", description: "Click the plus button to add projects.")
emptyDataSet.setFailed(title: "Failed", description: "Failed to get the list data.", buttonTitle: "Retry") { }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment