Advertisement
Wolverine_X-Man

ArtistTVC3.swift

Feb 6th, 2024
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 13.32 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.         priceLocationTextStack.snp.makeConstraints { make in
  105.             make.width.equalTo(60)
  106.         }
  107.         stack.isUserInteractionEnabled = true
  108.         stack.alignment = .leading
  109.         stack.distribution = .fill
  110.         return stack
  111.     }()
  112.     private lazy var priceLocationTextStack: UIStackView = {
  113.         let stack = UIStackView(arrangedSubviews: [priceText, locationText])
  114.         stack.axis = .vertical
  115.         stack.isUserInteractionEnabled = true
  116.         stack.spacing = 6
  117.         stack.alignment = .fill
  118.         stack.distribution = .fillEqually
  119.         return stack
  120.     }()
  121.     private lazy var priceLocationValueStack: UIStackView = {
  122.         let stack = UIStackView(arrangedSubviews: [priceValue, locationValue])
  123.         stack.axis = .vertical
  124.         stack.isUserInteractionEnabled = true
  125.         stack.spacing = 6
  126.         stack.alignment = .fill
  127.         stack.distribution = .fillEqually
  128.         return stack
  129.     }()
  130.     private lazy var priceText: UILabel = {
  131.         let label = UILabel()
  132.         label.isUserInteractionEnabled = true
  133.         label.text = "artist_home_price".myLocalizedString
  134.         label.font = .fontRegular14
  135.         return label
  136.     }()
  137.     private lazy var priceValue: UILabel = {
  138.         let label = UILabel()
  139.         label.isUserInteractionEnabled = true
  140.         label.font = .fontBold14
  141.         return label
  142.     }()
  143.     private lazy var locationText: UILabel = {
  144.         let label = UILabel()
  145.         label.isUserInteractionEnabled = true
  146.         label.text = "artist_home_location".myLocalizedString
  147.         label.font = .fontRegular14
  148.         return label
  149.     }()
  150.     private lazy var locationValue: UILabel = {
  151.         let label = UILabel()
  152.         label.isUserInteractionEnabled = true
  153.         label.font = .fontBold14
  154.         return label
  155.     }()
  156.     private lazy var reatingStarImageView: UIImageView = {
  157.         let imageView = UIImageView()
  158.         imageView.image = .saveStarFill
  159.         imageView.isUserInteractionEnabled = true
  160.         imageView.tintColor = .yellow
  161.         return imageView
  162.     }()
  163.     lazy var cellNextButton: UIButton = {
  164.         let button = UIButton()
  165.         button.isUserInteractionEnabled = true
  166.         button.setImage(.arrowRight, for: .normal)
  167.         button.tintColor = .white
  168.         button.backgroundColor = .colorWhiteAlpha3
  169.         button.corner(cornerRadius: 6.0)
  170.         button.addTarget(self, action: #selector(nextButtonTapped), for: .touchUpInside)
  171.         return button
  172.     }()
  173.     lazy var artistProfileImage: UIImageView = {
  174.         let imageView = UIImageView(isImageViewerEnable: true)
  175.         imageView.image = UIImage(named: "ratingDemoProfile")
  176.         imageView.corner(cornerRadius: 30)
  177.         imageView.accessibilityElement(id: AccessIdentifier.artistPersonalInfoProfileImage, label: AccessLabel.artistPersonalInfoProfileImage)
  178.         imageView.contentMode = .scaleAspectFill
  179.         return imageView
  180.     }()
  181.  
  182.     var starButtonHandler: ((_ index: Int) -> Void)?
  183.     var cellNextButtonHandler: (() -> Void)?
  184.  
  185.     override func awakeFromNib() {
  186.         super.awakeFromNib()
  187.         // Initialization code
  188.     }
  189.  
  190.     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  191.         super.init(style: style, reuseIdentifier: reuseIdentifier)
  192.         setupUi()
  193.     }
  194.  
  195.     required init?(coder: NSCoder) {
  196.         fatalError("init(coder:) has not been implemented")
  197.     }
  198.  
  199.     override func setSelected(_ selected: Bool, animated: Bool) {
  200.         super.setSelected(selected, animated: animated)
  201.         // Configure the view for the selected state
  202.     }
  203.  
  204.     func setupCell(data: HomeArtistModal, index: Int) {
  205.         self.cellTypeNameLabel.text = data.userType
  206.         self.bgImageView.image = .artistBgImage // download image with SD Web Image
  207.         let fullName = data.showLastName ?? true ? "\(data.firstName  ?? K.emptyString) \(data.lastName ?? K.emptyString)" : data.firstName ?? K.emptyString
  208.         self.artistName.text = fullName
  209.         self.cellNextButton.accessibilityElement(id: AccessIdentifier.artistHomeTVCNextButton + "\(index)", label: AccessLabel.artistHomeTVCNextButton.replaceString(with: fullName))
  210.         self.bookmarkedImageView.image = data.isSaved ?? false ? UIImage.saveStarFill : UIImage.unSaveStar
  211.         self.setAccessibilityOfSaveButton(data: data, index: index)
  212.         self.bookmarkedImageView.tag = index
  213.         self.artistName.accessibilityElement(id: AccessIdentifier.artistHomeTVCName + "\(index)", label: AccessLabel.artistHomeTVCName.replaceString(with: fullName ))
  214.         self.artistUserName.text = data.userName
  215.         self.artistUserName.accessibilityElement(id: AccessIdentifier.artistHomeTVCUserName + "\(index)", label: AccessLabel.artistHomeTVCUserName.replaceString(with: data.userName ?? K.emptyString))
  216.         self.priceValue.text = "$\(data.price ?? .zero)"
  217.         self.priceValue.accessibilityElement(id: AccessIdentifier.artistHomeTVCPrice + "\(index)", label: AccessLabel.artistHomeTVCPrice.replaceString(with: "$\(data.price ?? .zero)"))
  218.         self.locationValue.text = data.primaryLocation?.address
  219.         self.artistUserName.accessibilityElement(id: AccessIdentifier.artistHomeTVCLocation + "\(index)", label: AccessLabel.artistHomeTVCLocation.replaceString(with: data.primaryLocation?.address ?? K.emptyString))
  220.         let ratingStar = data.rating ?? 0.0
  221.         if ratingStar < 0.5 {
  222.             self.reatingsStack.isHidden = true
  223.         } else {
  224.             self.ratingsTextlabel.text = String(data.rating ?? .zero)
  225.             self.reatingsStack.isHidden = false
  226.         }
  227.         self.ratingsTextlabel.accessibilityElement(id: AccessIdentifier.artistHomeTVCReating + "\(index)", label: AccessLabel.artistHomeTVCReating.replaceString(with: data.rating ?? K.emptyString))
  228.     }
  229.  
  230.     @objc func nextButtonTapped(_ sender: UIButton) {
  231.         if let block = cellNextButtonHandler {
  232.             block()
  233.         }
  234.     }
  235.  
  236.     func setAccessibilityOfSaveButton(data: HomeArtistModal, index: Int) {
  237.         if !(data.isSaved ?? false) {
  238.             bookmarkedImageView.accessibilityElement(id: AccessIdentifier.artistHomeTVCSaveButton + "\(index)", label: AccessLabel.artistHomeTVCSaveButton.replaceString(with: data.userName ?? K.emptyString))
  239.         } else {
  240.             bookmarkedImageView.accessibilityElement(id: AccessIdentifier.artistHomeTVCUnSaveButton + "\(index)", label: AccessLabel.artistHomeTVCUnSaveButton.replaceString(with: data.userName ?? K.emptyString))
  241.         }
  242.     }
  243.  
  244.     @objc func saveStarButtonTapped(_ tapGestureRecognizer: UITapGestureRecognizer) {
  245.         guard let tappedImage = tapGestureRecognizer.view as? UIImageView else { return }
  246.         if let block = starButtonHandler {
  247.             block(tappedImage.tag)
  248.         }
  249.     }
  250.  
  251.     private func setupUi() {
  252.         self.contentView.isUserInteractionEnabled = true
  253.         self.contentView.backgroundColor = .primaryColor
  254.  
  255.         self.contentView.addSubview(mainBgView)
  256.         mainBgView.addSubview(bgImageView)
  257.         bgImageView.addSubview(cellTypeNameLabel)
  258.         bgImageView.addSubview(bookmarkedImageView)
  259.         bgImageView.addSubview(detailBgView)
  260.         detailBgView.addSubview(reatingsStack)
  261.         detailBgView.addSubview(nameStack)
  262.         detailBgView.addSubview(cellNextButton)
  263.         detailBgView.addSubview(priceLocationValueTextStack)
  264.         bgImageView.addSubview(artistProfileImage)
  265.  
  266.         mainBgView.snp.makeConstraints { make in
  267.             make.top.equalTo(self.contentView.snp.top)
  268.             make.bottom.equalTo(self.contentView.snp.bottom)
  269.             make.left.equalTo(self.contentView.snp.left)
  270.             make.right.equalTo(self.contentView.snp.right)
  271.         }
  272.         artistProfileImage.snp.makeConstraints { make in
  273.             make.left.equalTo(bgImageView.left).offset(leftOffset)
  274.             make.top.equalTo(bgImageView.top).offset(10)
  275.             make.height.width.equalTo(60)
  276.         }
  277.         bgImageView.snp.makeConstraints { make in
  278.             make.top.bottom.left.right.equalTo(mainBgView)
  279.         }
  280.         bookmarkedImageView.snp.makeConstraints { make in
  281.             make.top.equalTo(bgImageView.snp.top).offset(14)
  282.             make.right.equalTo(bgImageView.snp.right).inset(7)
  283.             make.height.width.equalTo(24)
  284.         }
  285.         cellTypeNameLabel.snp.makeConstraints { make in
  286.             make.right.equalTo(bookmarkedImageView.left).inset(-12)
  287.             make.top.equalTo(bgImageView.snp.top).offset(10)
  288.             make.width.equalTo(78)
  289.             make.height.equalTo(28)
  290.         }
  291.         detailBgView.snp.makeConstraints { make in
  292.             make.left.equalTo(bgImageView.snp.left).offset(16)
  293.             make.right.equalTo(bgImageView.snp.right).inset(16)
  294.             make.bottom.equalTo(bgImageView.snp.bottom).inset(10)
  295.             make.height.equalTo(82)
  296.         }
  297.         reatingsStack.snp.makeConstraints { make in
  298.             make.top.equalTo(detailBgView.snp.top).offset(4)
  299.             make.left.equalTo(detailBgView.snp.left).offset(8)
  300.             make.width.equalTo(60)
  301.             make.height.equalTo(16)
  302.         }
  303.         nameStack.snp.makeConstraints { make in
  304.             make.bottom.equalTo(detailBgView.snp.bottom).inset(12)
  305.             make.left.equalTo(detailBgView.snp.left).offset(8)
  306.             make.width.equalTo(96)
  307.         }
  308.         cellNextButton.snp.makeConstraints { make in
  309.             make.height.width.equalTo(32)
  310.             make.right.equalTo(detailBgView.snp.right).inset(10)
  311.             make.top.equalTo(detailBgView.top).offset(12)
  312.         }
  313.         priceLocationValueTextStack.snp.makeConstraints { make in
  314.             make.left.equalTo(nameStack.snp.right).offset(12)
  315.             make.bottom.equalTo(detailBgView.snp.bottom).inset(12)
  316.             make.right.equalTo(detailBgView.snp.right).inset(8)
  317.         }
  318.     }
  319. }
  320.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement