Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import UIKit
  2. import UIView_Shimmer
  3.  
  4. public protocol CardComponentDelegate: class {
  5.  
  6. func cardComponentDidTapFavoriteButton(_ component: CardComponent)
  7. }
  8.  
  9. /// ![Snapshot](../img/CardComponentTest/testBuildRender.1.png)
  10.  
  11. public class CardComponent: UIView, ComponentType {
  12.  
  13. /// :nodoc:
  14. public typealias Delegate = CardComponentDelegate
  15. public weak var delegate: CardComponentDelegate?
  16.  
  17. @IBOutlet private weak var titleLabel: UILabel?
  18. @IBOutlet private weak var contentLabel: UILabel?
  19. @IBOutlet private weak var favoriteButton: UIButton?
  20.  
  21. /// :nodoc:
  22. override public func awakeFromNib() {
  23. super.awakeFromNib()
  24. }
  25.  
  26. @IBAction private func didTapFavoriteButton() {
  27. self.delegate?.cardComponentDidTapFavoriteButton(self)
  28. }
  29. }
  30.  
  31. extension CardComponent {
  32.  
  33. public enum RenderType {
  34.  
  35. case build(DTO)
  36. case loading(Bool)
  37. case favorite(Bool)
  38. }
  39.  
  40. public func render(_ type: CardComponent.RenderType) {
  41. switch type {
  42. case .build(let dto):
  43. titleLabel?.text = dto.title
  44. contentLabel?.text = dto.content
  45. setFavoriteImage(dto.isFavorite)
  46.  
  47. case .loading(let isLoading):
  48. titleLabel?.backgroundColor = isLoading ? UIColor(named: .background) : UIColor.white
  49. titleLabel?.textColor = isLoading ? UIColor.clear : UIColor(named: .title)
  50. isLoading ? titleLabel?.startShimmering() : titleLabel?.stopShimmering()
  51.  
  52. contentLabel?.backgroundColor = isLoading ? UIColor(named: .background) : UIColor.white
  53. contentLabel?.textColor = isLoading ? UIColor.clear : UIColor(named: .content)
  54. isLoading ? contentLabel?.startShimmering() : contentLabel?.stopShimmering()
  55.  
  56. favoriteButton?.isHidden = isLoading
  57.  
  58. case .favorite(let isFavorite):
  59. setFavoriteImage(isFavorite)
  60. }
  61. }
  62.  
  63. private func setFavoriteImage(_ isFavorite: Bool) {
  64. let imageName = isFavorite ? "ic_favorite_on" : "ic_favorite_off"
  65. let image = UIImage(named: imageName, in: Bundle(for: CardComponent.self), compatibleWith: nil)
  66. favoriteButton?.setImage(image, for: .normal)
  67. }
  68. }
  69.  
  70. extension CardComponent {
  71.  
  72. public struct DTO {
  73.  
  74. var title: String
  75. var content: String
  76. var isFavorite: Bool
  77.  
  78. public init(title: String, content: String, isFavorite: Bool) {
  79. self.title = title
  80. self.content = content
  81. self.isFavorite = isFavorite
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement