MenuCell.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // MenuCell.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 28/03/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. import MaterialComponents.MaterialCards
  10. class MenuCell: UICollectionViewCell {
  11. private let iconCircleSize: CGFloat = 52.0
  12. private let iconSize: CGFloat = 26.0
  13. private let iconBgView: UIView = {
  14. let view = UIView()
  15. view.backgroundColor = UIColor.siraniSoftBlue
  16. view.layer.cornerRadius = 26.0
  17. view.layer.masksToBounds = true
  18. return view
  19. }()
  20. private let iconImageView: UIImageView = {
  21. let iv = UIImageView()
  22. iv.contentMode = .scaleAspectFit
  23. return iv
  24. }()
  25. private let nameLabel: UILabel = {
  26. let label = UILabel()
  27. label.textAlignment = .center
  28. label.numberOfLines = 2
  29. label.lineBreakMode = .byWordWrapping
  30. label.textColor = UIColor.siraniTextPrimary
  31. label.font = UIFont.systemFont(ofSize: 11, weight: .medium)
  32. return label
  33. }()
  34. private let badgeContainer: UIView = {
  35. let view = UIView()
  36. view.backgroundColor = UIColor(red: 220/255, green: 38/255, blue: 38/255, alpha: 1.0) // Red badge
  37. view.layer.cornerRadius = 8
  38. view.layer.masksToBounds = true
  39. view.isHidden = true
  40. return view
  41. }()
  42. private let badgeLabel: UILabel = {
  43. let label = UILabel()
  44. label.textAlignment = .center
  45. label.numberOfLines = 1
  46. label.textColor = .white
  47. label.font = UIFont.systemFont(ofSize: 9, weight: .bold)
  48. return label
  49. }()
  50. override init(frame: CGRect) {
  51. super.init(frame: frame)
  52. setupViews()
  53. }
  54. required init?(coder: NSCoder) {
  55. super.init(coder: coder)
  56. setupViews()
  57. }
  58. override func prepareForReuse() {
  59. super.prepareForReuse()
  60. badgeContainer.isHidden = true
  61. badgeLabel.text = ""
  62. iconImageView.image = nil
  63. nameLabel.text = ""
  64. }
  65. private func setupViews() {
  66. contentView.backgroundColor = .clear
  67. backgroundColor = .clear
  68. contentView.addSubview(iconBgView)
  69. iconBgView.addSubview(iconImageView)
  70. contentView.addSubview(nameLabel)
  71. contentView.addSubview(badgeContainer)
  72. badgeContainer.addSubview(badgeLabel)
  73. iconBgView.anchor(
  74. top: contentView.topAnchor,
  75. bottom: nil,
  76. leading: nil,
  77. trailing: nil,
  78. padding: .init(top: 4, left: 0, bottom: 0, right: 0),
  79. size: .init(width: iconCircleSize, height: iconCircleSize)
  80. )
  81. iconBgView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
  82. iconImageView.anchor(
  83. top: nil,
  84. bottom: nil,
  85. leading: nil,
  86. trailing: nil,
  87. size: .init(width: iconSize, height: iconSize)
  88. )
  89. iconImageView.centerXAnchor.constraint(equalTo: iconBgView.centerXAnchor).isActive = true
  90. iconImageView.centerYAnchor.constraint(equalTo: iconBgView.centerYAnchor).isActive = true
  91. nameLabel.anchor(
  92. top: iconBgView.bottomAnchor,
  93. bottom: contentView.bottomAnchor,
  94. leading: contentView.leadingAnchor,
  95. trailing: contentView.trailingAnchor,
  96. padding: .init(top: 6, left: 2, bottom: 2, right: 2)
  97. )
  98. badgeContainer.anchor(
  99. top: iconBgView.topAnchor,
  100. bottom: nil,
  101. leading: nil,
  102. trailing: iconBgView.trailingAnchor,
  103. padding: .init(top: -2, left: 0, bottom: 0, right: -4),
  104. size: .init(width: 0, height: 16)
  105. )
  106. badgeLabel.anchor(
  107. top: badgeContainer.topAnchor,
  108. bottom: badgeContainer.bottomAnchor,
  109. leading: badgeContainer.leadingAnchor,
  110. trailing: badgeContainer.trailingAnchor,
  111. padding: .init(top: 0, left: 5, bottom: 0, right: 5)
  112. )
  113. }
  114. func updateViews(menu: MenuItem) {
  115. iconImageView.image = UIImage(named: menu.icon)
  116. nameLabel.text = menu.name
  117. let totalNotif = menu.totalNotif
  118. if totalNotif > 0 {
  119. badgeContainer.isHidden = false
  120. badgeLabel.text = "\(totalNotif)"
  121. } else {
  122. badgeContainer.isHidden = true
  123. badgeLabel.text = ""
  124. }
  125. }
  126. }