MenuCell.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 menuItemSize = 85.75
  12. private let iconSize = 36.0
  13. func updateViews(menu: MenuItem) {
  14. // card
  15. let card = MDCCard()
  16. card.isInteractable = false
  17. card.backgroundColor = .white
  18. card.setShadowElevation(.init(1), for: .normal)
  19. card.setShadowColor(.gray, for: .normal)
  20. card.cornerRadius = 5
  21. self.addSubview(card)
  22. card.anchor(
  23. top: topAnchor,
  24. bottom: bottomAnchor,
  25. leading: leadingAnchor,
  26. trailing: trailingAnchor,
  27. padding: .init(top: 4, left: 2, bottom: 4, right: 2)
  28. )
  29. // icon
  30. let paddingSize = CGFloat((menuItemSize-iconSize-4)/2)
  31. let icon = UIImageView()
  32. icon.image = UIImage(named: menu.icon)
  33. icon.contentMode = .scaleAspectFit
  34. card.addSubview(icon)
  35. icon.anchor(
  36. top: card.topAnchor,
  37. bottom: nil,
  38. leading: card.leadingAnchor,
  39. trailing: card.trailingAnchor,
  40. padding: .init(top: 8, left: paddingSize, bottom: 0, right: paddingSize),
  41. size: .init(width: iconSize, height: iconSize)
  42. )
  43. // name
  44. let nameLabel = UILabel()
  45. nameLabel.text = menu.name
  46. nameLabel.textAlignment = NSTextAlignment.center
  47. nameLabel.numberOfLines = 2
  48. nameLabel.lineBreakMode = .byWordWrapping
  49. nameLabel.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
  50. nameLabel.font = UIFont.systemFont(ofSize: 10, weight: .semibold)
  51. card.addSubview(nameLabel)
  52. nameLabel.anchor(
  53. top: icon.bottomAnchor,
  54. bottom: card.bottomAnchor,
  55. leading: card.leadingAnchor,
  56. trailing: card.trailingAnchor,
  57. padding: .init(top: 1, left: 2, bottom: 4, right: 2)
  58. )
  59. // notif
  60. let totalNotif = menu.totalNotif
  61. if totalNotif > 0 {
  62. let bgNotif = UIView()
  63. bgNotif.backgroundColor = #colorLiteral(red: 0.8180381656, green: 0, blue: 0, alpha: 1)
  64. bgNotif.layer.cornerRadius = 5
  65. card.addSubview(bgNotif)
  66. bgNotif.anchor(
  67. top: card.topAnchor,
  68. bottom: nil,
  69. leading: nil,
  70. trailing: card.trailingAnchor,
  71. padding: .init(top: 4, left: 0, bottom: 0, right: 4)
  72. )
  73. bgNotif.bringSubviewToFront(icon)
  74. let notifLabel = UILabel()
  75. notifLabel.text = "\(totalNotif)"
  76. notifLabel.textAlignment = NSTextAlignment.center
  77. notifLabel.numberOfLines = 1
  78. notifLabel.textColor = .white
  79. notifLabel.font = UIFont.systemFont(ofSize: 9, weight: .bold)
  80. bgNotif.addSubview(notifLabel)
  81. notifLabel.anchor(
  82. top: bgNotif.topAnchor,
  83. bottom: bgNotif.bottomAnchor,
  84. leading: bgNotif.leadingAnchor,
  85. trailing: bgNotif.trailingAnchor,
  86. padding: .init(top: 2, left: 4, bottom: 2, right: 4)
  87. )
  88. }
  89. }
  90. }