Skip to content

Instantly share code, notes, and snippets.

@mhijack
Last active April 23, 2021 17:09
Show Gist options
  • Save mhijack/571d5744e3b18f96f5ff56f39612b430 to your computer and use it in GitHub Desktop.
Save mhijack/571d5744e3b18f96f5ff56f39612b430 to your computer and use it in GitHub Desktop.
class EmptyTableViewCell: UITableViewCell {
var emptyModel: EmptyTableViewCellModel?
public func setContent(emptyModel: EmptyTableViewCellModel) {
self.emptyModel = emptyModel
}
}
final class PlainEmptyTableViewCell: EmptyTableViewCell {
public var icon: UIImage?
public var descriptionText: String?
public var actionText: String?
public var action: (() -> ())?
private lazy var rootWrapper: UIStackView = UIStackView()
public lazy var iconImageView: UIImageView = UIImageView()
public lazy var descriptionLabel: UILabel = UILabel()
public lazy var actionButton: UIButton = UIButton(type: .system)
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backgroundColor = .clear
contentView.backgroundColor = .clear
contentView.sv(rootWrapper)
rootWrapper.fillContainer()
let topSpacer = UIView()
topSpacer.height(30)
topSpacer.backgroundColor = .clear
let bottomSpacer = UIView()
bottomSpacer.height(20)
bottomSpacer.backgroundColor = .clear
let bottomSpacer2 = UIView()
bottomSpacer2.height(20)
bottomSpacer2.backgroundColor = .clear
let labelSpacer = UIView()
labelSpacer.backgroundColor = .clear
labelSpacer.height(5)
rootWrapper.addArrangedSubview(topSpacer)
rootWrapper.addArrangedSubview(iconImageView)
rootWrapper.addArrangedSubview(labelSpacer)
rootWrapper.addArrangedSubview(descriptionLabel)
rootWrapper.addArrangedSubview(bottomSpacer)
rootWrapper.addArrangedSubview(actionButton)
rootWrapper.addArrangedSubview(bottomSpacer2)
rootWrapper.spacing = 8
rootWrapper.alignment = .center
rootWrapper.distribution = .fill
rootWrapper.axis = .vertical
iconImageView.contentMode = .scaleAspectFill
iconImageView
.width(70)
.height(70)
descriptionLabel.textColor = StyleHelper.style.textTertiaryColor
descriptionLabel.textAlignment = .center
descriptionLabel.font = StyleHelper.style.system12FontRegular
descriptionLabel.numberOfLines = 4
descriptionLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
actionButton.titleLabel?.font = StyleHelper.style.system12FontRegular
actionButton.tintColor = StyleHelper.style.lightBlueLinkColor
actionButton.setImage(UIImage(named: "chevronRight")!.withRenderingMode(.alwaysTemplate), for: .normal)
actionButton.addTarget(self, action: #selector(didTapActionButton), for: .touchUpInside)
actionButton.imageEdgeInsets = UIEdgeInsets(top: 3, left: 0, bottom: 3, right: -2)
actionButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: -5)
actionButton.semanticContentAttribute = .forceRightToLeft
actionButton.contentVerticalAlignment = .center
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemleted")
}
override func layoutSubviews() {
super.layoutSubviews()
separatorInset = UIEdgeInsets(top: 0, left: bounds.size.width, bottom: 0, right: 0)
let halfWidth = self.frame.width / 4.0
rootWrapper.leadingConstraint?.constant = halfWidth / 2.0
rootWrapper.trailingConstraint?.constant = -(halfWidth / 2.0)
}
override func setContent(emptyModel: EmptyTableViewCellModel) {
super.setContent(emptyModel: emptyModel)
setContent(icon: emptyModel.icon, description: emptyModel.descriptionText, action: emptyModel.actionText)
}
}
extension PlainEmptyTableViewCell {
@objc private func didTapActionButton() {
parentDelegate?.didTapEmptyTableViewCellPrimaryAction()
}
}
extension PlainEmptyTableViewCell {
public func setContent(icon: UIImage = UIImage(named: "chat-no-ustars")!,
description: String = StringsHelper.standard.localized.itsKindOfLonelyOutThere,
action: String = "") {
self.icon = icon
self.descriptionText = description
if action.isEmpty {
self.actionButton.isHidden = true
} else {
self.actionText = action
}
iconImageView.image = icon
descriptionLabel.text = description
actionButton.setTitle(action, for: .normal)
}
}
extension PlainEmptyTableViewCell {
@objc private func didTapActionButton() {
action?()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment