Advertisement
Wolverine_X-Man

ArtistTVC.swift

Feb 5th, 2024
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 13.21 KB | Source Code | 0 0
  1. //
  2. //  ArtistTVC.swift
  3. //  hubble-ios
  4. //
  5. //  Created by Chandan Chandan on 11/06/23.
  6. //
  7.  
  8. import UIKit
  9.  
  10. class ArtistTVC: UITableViewCell {
  11.     private lazy var mainBgView: UIView = {
  12.         let view = UIView()
  13.         view.isUserInteractionEnabled = true
  14.         view.backgroundColor = .clear
  15.         view.corner(cornerRadius: 12)
  16.         return view
  17.     }()
  18.     private lazy var bgImageView: UIImageView = {
  19.         let imageView = UIImageView()
  20.         imageView.image = UIImage(named: "cellbg")
  21.         imageView.isUserInteractionEnabled = true
  22.         imageView.backgroundColor = .clear
  23.         return imageView
  24.     }()
  25.     private lazy var cellTypeNameLabel: UILabel = {
  26.         let label = UILabel()
  27.         label.text = "artist_home_artst".myLocalizedString
  28.         label.textAlignment = .center
  29.         label.isUserInteractionEnabled = true
  30.         label.textColor = .white
  31.         label.backgroundColor = .color111111Alpha6
  32.         label.corner(cornerRadius: 8.0)
  33.         label.font = .fontRegular16
  34.         return label
  35.     }()
  36.     private lazy var cellTypeNameLabelBgView: UIView = {
  37.         let view = UIView()
  38.         view.isUserInteractionEnabled = true
  39.         view.addSubview(cellTypeNameLabel)
  40.         cellTypeNameLabel.snp.makeConstraints { make in
  41.             make.center.equalTo(view)
  42.         }
  43.         return view
  44.     }()
  45.     lazy var bookmarkedImageView: UIImageView = {
  46.         let imageView = UIImageView()
  47.         imageView.tintColor = .white
  48.         imageView.isUserInteractionEnabled = true
  49.         let tap = UITapGestureRecognizer(target: self, action: #selector(saveStarButtonTapped(_:)))
  50.         imageView.addGestureRecognizer(tap)
  51.         return imageView
  52.     }()
  53.     private lazy var detailBgView: UIView = { // details
  54.         let view = UIView()
  55.         view.isUserInteractionEnabled = true
  56.         view.corner(cornerRadius: 12)
  57.         view.backgroundColor = .color111111Alpha6
  58.         return view
  59.     }()
  60.     private lazy var reatingsStack: UIStackView = {
  61.         let stack = UIStackView(arrangedSubviews: [ratingsTextlabel, reatingStarImageView])
  62.         reatingStarImageView.snp.makeConstraints { make in
  63.             make.width.equalTo(20)
  64.         }
  65.         stack.isUserInteractionEnabled = true
  66.         stack.axis = .horizontal
  67.         stack.spacing = 2
  68.         stack.distribution = .fill
  69.         stack.alignment = .fill
  70.         return stack
  71.     }()
  72.     private lazy var ratingsTextlabel: UILabel = {
  73.         let label = UILabel()
  74.         label.textColor = .white
  75.         label.isUserInteractionEnabled = true
  76.         label.font = .fontLight14
  77.         return label
  78.     }()
  79.  
  80.     lazy var nameStack: UIStackView = {
  81.         let stack = UIStackView(arrangedSubviews: [artistName, artistUserName])
  82.         stack.axis = .vertical
  83.         stack.spacing = 6
  84.         stack.isUserInteractionEnabled = true
  85.         stack.alignment = .fill
  86.         stack.distribution = .fillEqually
  87.         return stack
  88.     }()
  89.     private lazy var artistName: UILabel = {
  90.         let label = UILabel()
  91.         label.isUserInteractionEnabled = true
  92.         label.font = .fontBold14
  93.         return label
  94.     }()
  95.     private lazy var artistUserName: UILabel = {
  96.         let label = UILabel()
  97.         label.isUserInteractionEnabled = true
  98.         label.font = .fontRegular14
  99.         return label
  100.     }()
  101.     private lazy var priceLocationValueTextStack: UIStackView = {
  102.         let stack = UIStackView(arrangedSubviews: [priceLocationTextStack, priceLocationValueStack])
  103.         stack.axis = .horizontal
  104.         stack.isUserInteractionEnabled = true
  105.         stack.alignment = .leading
  106.         stack.distribution = .fillEqually
  107.         return stack
  108.     }()
  109.     private lazy var priceLocationTextStack: UIStackView = {
  110.         let stack = UIStackView(arrangedSubviews: [priceText, locationText])
  111.         stack.axis = .vertical
  112.         stack.isUserInteractionEnabled = true
  113.         stack.spacing = 6
  114.         stack.alignment = .fill
  115.         stack.distribution = .fillEqually
  116.         return stack
  117.     }()
  118.     private lazy var priceLocationValueStack: UIStackView = {
  119.         let stack = UIStackView(arrangedSubviews: [priceValue, locationValue])
  120.         stack.axis = .vertical
  121.         stack.isUserInteractionEnabled = true
  122.         stack.spacing = 6
  123.         stack.alignment = .fill
  124.         stack.distribution = .fillEqually
  125.         return stack
  126.     }()
  127.     private lazy var priceText: UILabel = {
  128.         let label = UILabel()
  129.         label.isUserInteractionEnabled = true
  130.         label.text = "artist_home_price".myLocalizedString
  131.         label.font = .fontRegular14
  132.         return label
  133.     }()
  134.     private lazy var priceValue: UILabel = {
  135.         let label = UILabel()
  136.         label.isUserInteractionEnabled = true
  137.         label.font = .fontBold14
  138.         return label
  139.     }()
  140.     private lazy var locationText: UILabel = {
  141.         let label = UILabel()
  142.         label.isUserInteractionEnabled = true
  143.         label.text = "artist_home_location".myLocalizedString
  144.         label.font = .fontRegular14
  145.         return label
  146.     }()
  147.     private lazy var locationValue: UILabel = {
  148.         let label = UILabel()
  149.         label.isUserInteractionEnabled = true
  150.         label.font = .fontBold14
  151.         return label
  152.     }()
  153.     private lazy var reatingStarImageView: UIImageView = {
  154.         let imageView = UIImageView()
  155.         imageView.image = .saveStarFill
  156.         imageView.isUserInteractionEnabled = true
  157.         imageView.tintColor = .yellow
  158.         return imageView
  159.     }()
  160.     lazy var cellNextButton: UIButton = {
  161.         let button = UIButton()
  162.         button.isUserInteractionEnabled = true
  163.         button.setImage(.arrowRight, for: .normal)
  164.         button.tintColor = .white
  165.         button.backgroundColor = .colorWhiteAlpha3
  166.         button.corner(cornerRadius: 6.0)
  167.         button.addTarget(self, action: #selector(nextButtonTapped), for: .touchUpInside)
  168.         return button
  169.     }()
  170.  
  171.     var starButtonHandler: ((_ index: Int) -> Void)?
  172.     var cellNextButtonHandler: (() -> Void)?
  173.  
  174.     override func awakeFromNib() {
  175.         super.awakeFromNib()
  176.         // Initialization code
  177.     }
  178.  
  179.     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  180.         super.init(style: style, reuseIdentifier: reuseIdentifier)
  181.         setupUi()
  182.     }
  183.  
  184.     required init?(coder: NSCoder) {
  185.         fatalError("init(coder:) has not been implemented")
  186.     }
  187.  
  188.     override func setSelected(_ selected: Bool, animated: Bool) {
  189.         super.setSelected(selected, animated: animated)
  190.         // Configure the view for the selected state
  191.     }
  192.  
  193.     func setupCell(data: HomeArtistModal, index: Int) {
  194.         self.cellTypeNameLabel.text = data.userType
  195.         self.bgImageView.image = .artistBgImage // download image with SD Web Image
  196.         let fullName = data.showLastName ?? true ? "\(data.firstName  ?? K.emptyString) \(data.lastName ?? K.emptyString)" : data.firstName ?? K.emptyString
  197.         self.artistName.text = fullName
  198.         self.cellNextButton.accessibilityElement(id: AccessIdentifier.artistHomeTVCNextButton + "\(index)", label: AccessLabel.artistHomeTVCNextButton.replaceString(with: fullName))
  199.         self.bookmarkedImageView.image = data.isSaved ?? false ? UIImage.saveStarFill : UIImage.unSaveStar
  200.         self.setAccessibilityOfSaveButton(data: data, index: index)
  201.         self.bookmarkedImageView.tag = index
  202.         self.artistName.accessibilityElement(id: AccessIdentifier.artistHomeTVCName + "\(index)", label: AccessLabel.artistHomeTVCName.replaceString(with: fullName ))
  203.         self.artistUserName.text = data.userName
  204.         self.artistUserName.accessibilityElement(id: AccessIdentifier.artistHomeTVCUserName + "\(index)", label: AccessLabel.artistHomeTVCUserName.replaceString(with: data.userName ?? K.emptyString))
  205.         self.priceValue.text = "$\(data.price ?? .zero)"
  206.         self.priceValue.accessibilityElement(id: AccessIdentifier.artistHomeTVCPrice + "\(index)", label: AccessLabel.artistHomeTVCPrice.replaceString(with: "$\(data.price ?? .zero)"))
  207.         self.locationValue.text = data.primaryLocation?.address
  208.         self.artistUserName.accessibilityElement(id: AccessIdentifier.artistHomeTVCLocation + "\(index)", label: AccessLabel.artistHomeTVCLocation.replaceString(with: data.primaryLocation?.address ?? K.emptyString))
  209.         let ratingStar = data.rating ?? 0.0
  210.         if ratingStar < 0.5 {
  211.             self.reatingsStack.isHidden = true
  212.         } else {
  213.             self.ratingsTextlabel.text = String(data.rating ?? .zero)
  214.             self.reatingsStack.isHidden = false
  215.         }
  216.         self.ratingsTextlabel.accessibilityElement(id: AccessIdentifier.artistHomeTVCReating + "\(index)", label: AccessLabel.artistHomeTVCReating.replaceString(with: data.rating ?? K.emptyString))
  217.     }
  218.  
  219.     @objc func nextButtonTapped(_ sender: UIButton) {
  220.         if let block = cellNextButtonHandler {
  221.             block()
  222.         }
  223.     }
  224.  
  225.     func setAccessibilityOfSaveButton(data: HomeArtistModal, index: Int) {
  226.         if !(data.isSaved ?? false) {
  227.             bookmarkedImageView.accessibilityElement(id: AccessIdentifier.artistHomeTVCSaveButton + "\(index)", label: AccessLabel.artistHomeTVCSaveButton.replaceString(with: data.userName ?? K.emptyString))
  228.         } else {
  229.             bookmarkedImageView.accessibilityElement(id: AccessIdentifier.artistHomeTVCUnSaveButton + "\(index)", label: AccessLabel.artistHomeTVCUnSaveButton.replaceString(with: data.userName ?? K.emptyString))
  230.         }
  231.     }
  232.  
  233.     @objc func saveStarButtonTapped(_ tapGestureRecognizer: UITapGestureRecognizer) {
  234.         guard let tappedImage = tapGestureRecognizer.view as? UIImageView else { return }
  235.         if let block = starButtonHandler {
  236.             block(tappedImage.tag)
  237.         }
  238.     }
  239.  
  240.     lazy var artistProfileImage: UIImageView = {
  241.         let imageView = UIImageView(isImageViewerEnable: true)
  242.         imageView.image = UIImage(named: "ratingDemoProfile")
  243.         imageView.corner(cornerRadius: 30)
  244.         imageView.accessibilityElement(id: AccessIdentifier.artistPersonalInfoProfileImage, label: AccessLabel.artistPersonalInfoProfileImage)
  245.         imageView.contentMode = .scaleAspectFill
  246.         return imageView
  247.     }()
  248.     private func setupUi() {
  249.         self.contentView.isUserInteractionEnabled = true
  250.         self.contentView.backgroundColor = .primaryColor
  251.  
  252.         self.contentView.addSubview(mainBgView)
  253.         mainBgView.addSubview(bgImageView)
  254.         bgImageView.addSubview(cellTypeNameLabel)
  255.         bgImageView.addSubview(bookmarkedImageView)
  256.         bgImageView.addSubview(detailBgView)
  257.         detailBgView.addSubview(reatingsStack)
  258.         detailBgView.addSubview(nameStack)
  259.         detailBgView.addSubview(cellNextButton)
  260.         detailBgView.addSubview(priceLocationValueTextStack)
  261.         bgImageView.addSubview(artistProfileImage)
  262.  
  263.         mainBgView.snp.makeConstraints { make in
  264.             make.top.equalTo(self.contentView.snp.top)
  265.             make.bottom.equalTo(self.contentView.snp.bottom)
  266.             make.left.equalTo(self.contentView.snp.left)
  267.             make.right.equalTo(self.contentView.snp.right)
  268.         }
  269.         artistProfileImage.snp.makeConstraints { make in
  270.             make.left.equalTo(bgImageView.left).offset(leftOffset)
  271.             make.top.equalTo(bgImageView.top).offset(10)
  272.             make.height.width.equalTo(60)
  273.         }
  274.         bgImageView.snp.makeConstraints { make in
  275.             make.top.bottom.left.right.equalTo(mainBgView)
  276.         }
  277.         bookmarkedImageView.snp.makeConstraints { make in
  278.             make.top.equalTo(bgImageView.snp.top).offset(14)
  279.             make.right.equalTo(bgImageView.snp.right).inset(7)
  280.             make.height.width.equalTo(24)
  281.         }
  282.         cellTypeNameLabel.snp.makeConstraints { make in
  283.             make.right.equalTo(bookmarkedImageView.left).inset(-12)
  284.             make.top.equalTo(bgImageView.snp.top).offset(10)
  285.             make.width.equalTo(78)
  286.             make.height.equalTo(28)
  287.         }
  288.         detailBgView.snp.makeConstraints { make in
  289.             make.left.equalTo(bgImageView.snp.left).offset(16)
  290.             make.right.equalTo(bgImageView.snp.right).inset(16)
  291.             make.bottom.equalTo(bgImageView.snp.bottom).inset(10)
  292.             make.height.equalTo(82)
  293.         }
  294.         reatingsStack.snp.makeConstraints { make in
  295.             make.top.equalTo(detailBgView.snp.top).offset(4)
  296.             make.left.equalTo(detailBgView.snp.left).offset(8)
  297.             make.width.equalTo(60)
  298.             make.height.equalTo(16)
  299.         }
  300.         nameStack.snp.makeConstraints { make in
  301.             make.bottom.equalTo(detailBgView.snp.bottom).inset(12)
  302.             make.left.equalTo(detailBgView.snp.left).offset(8)
  303.             make.width.equalTo(96)
  304.         }
  305.         cellNextButton.snp.makeConstraints { make in
  306.             make.height.width.equalTo(32)
  307.             make.right.equalTo(detailBgView.snp.right).inset(10)
  308.             make.centerY.equalTo(detailBgView)
  309.         }
  310.         priceLocationValueTextStack.snp.makeConstraints { make in
  311.             make.left.equalTo(nameStack.snp.right).offset(20)
  312.             make.bottom.equalTo(detailBgView.snp.bottom).inset(12)
  313.             make.right.equalTo(cellNextButton.snp.left).inset(8)
  314.         }
  315.     }
  316. }
  317.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement