// // MenuCell.swift // Sirani // // Created by Anhar Solehudin on 28/03/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit import MaterialComponents.MaterialCards class MenuCell: UICollectionViewCell { private let iconCircleSize: CGFloat = 52.0 private let iconSize: CGFloat = 26.0 private let iconBgView: UIView = { let view = UIView() view.backgroundColor = UIColor.siraniSoftBlue view.layer.cornerRadius = 26.0 view.layer.masksToBounds = true return view }() private let iconImageView: UIImageView = { let iv = UIImageView() iv.contentMode = .scaleAspectFit return iv }() private let nameLabel: UILabel = { let label = UILabel() label.textAlignment = .center label.numberOfLines = 2 label.lineBreakMode = .byWordWrapping label.textColor = UIColor.siraniTextPrimary label.font = UIFont.systemFont(ofSize: 11, weight: .medium) return label }() private let badgeContainer: UIView = { let view = UIView() view.backgroundColor = UIColor(red: 220/255, green: 38/255, blue: 38/255, alpha: 1.0) // Red badge view.layer.cornerRadius = 8 view.layer.masksToBounds = true view.isHidden = true return view }() private let badgeLabel: UILabel = { let label = UILabel() label.textAlignment = .center label.numberOfLines = 1 label.textColor = .white label.font = UIFont.systemFont(ofSize: 9, weight: .bold) return label }() override init(frame: CGRect) { super.init(frame: frame) setupViews() } required init?(coder: NSCoder) { super.init(coder: coder) setupViews() } override func prepareForReuse() { super.prepareForReuse() badgeContainer.isHidden = true badgeLabel.text = "" iconImageView.image = nil nameLabel.text = "" } private func setupViews() { contentView.backgroundColor = .clear backgroundColor = .clear contentView.addSubview(iconBgView) iconBgView.addSubview(iconImageView) contentView.addSubview(nameLabel) contentView.addSubview(badgeContainer) badgeContainer.addSubview(badgeLabel) iconBgView.anchor( top: contentView.topAnchor, bottom: nil, leading: nil, trailing: nil, padding: .init(top: 4, left: 0, bottom: 0, right: 0), size: .init(width: iconCircleSize, height: iconCircleSize) ) iconBgView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true iconImageView.anchor( top: nil, bottom: nil, leading: nil, trailing: nil, size: .init(width: iconSize, height: iconSize) ) iconImageView.centerXAnchor.constraint(equalTo: iconBgView.centerXAnchor).isActive = true iconImageView.centerYAnchor.constraint(equalTo: iconBgView.centerYAnchor).isActive = true nameLabel.anchor( top: iconBgView.bottomAnchor, bottom: contentView.bottomAnchor, leading: contentView.leadingAnchor, trailing: contentView.trailingAnchor, padding: .init(top: 6, left: 2, bottom: 2, right: 2) ) badgeContainer.anchor( top: iconBgView.topAnchor, bottom: nil, leading: nil, trailing: iconBgView.trailingAnchor, padding: .init(top: -2, left: 0, bottom: 0, right: -4), size: .init(width: 0, height: 16) ) badgeLabel.anchor( top: badgeContainer.topAnchor, bottom: badgeContainer.bottomAnchor, leading: badgeContainer.leadingAnchor, trailing: badgeContainer.trailingAnchor, padding: .init(top: 0, left: 5, bottom: 0, right: 5) ) } func updateViews(menu: MenuItem) { iconImageView.image = UIImage(named: menu.icon) nameLabel.text = menu.name let totalNotif = menu.totalNotif if totalNotif > 0 { badgeContainer.isHidden = false badgeLabel.text = "\(totalNotif)" } else { badgeContainer.isHidden = true badgeLabel.text = "" } } }