|
| 1 | +// |
| 2 | +// RestaurantInfoView.swift |
| 3 | +// DeliveryApp |
| 4 | +// |
| 5 | +// Created by Manoel Filho on 26/07/22. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | + |
| 10 | +final class RestaurantInfoView: UIView { |
| 11 | + |
| 12 | + var restaurant: Restaurant? { |
| 13 | + didSet{ |
| 14 | + self.nameLabel.text = restaurant?.name |
| 15 | + self.categoryLabel.text = "\(restaurant?.category ?? "") \u{2022} \(restaurant?.deliveryTime.min ?? 0) - \(restaurant?.deliveryTime.max ?? 0) min" |
| 16 | + self.restaurantImage.image = UIImage(named: "restaurant-logo") |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + private lazy var nameLabel: UILabel = { |
| 21 | + let nameLabel = UILabel() |
| 22 | + nameLabel.text = "Title" |
| 23 | + nameLabel.font = .systemFont(ofSize: 22, weight: .bold) |
| 24 | + nameLabel.tintColor = .black |
| 25 | + return nameLabel |
| 26 | + }() |
| 27 | + |
| 28 | + private lazy var categoryLabel: UILabel = { |
| 29 | + let categoryLabel = UILabel() |
| 30 | + categoryLabel.text = "Category" |
| 31 | + categoryLabel.font = .systemFont(ofSize: 15, weight: .regular) |
| 32 | + categoryLabel.tintColor = .black |
| 33 | + return categoryLabel |
| 34 | + }() |
| 35 | + |
| 36 | + private lazy var nameAndCategoryStackView: UIStackView = { |
| 37 | + let nameAndCategoryStackView = UIStackView(arrangedSubviews: [nameLabel, categoryLabel]) |
| 38 | + nameAndCategoryStackView.axis = .vertical |
| 39 | + nameAndCategoryStackView.spacing = 12 |
| 40 | + return nameAndCategoryStackView |
| 41 | + }() |
| 42 | + |
| 43 | + private lazy var restaurantImage: UIImageView = { |
| 44 | + let restaurantImage = UIImageView() |
| 45 | + restaurantImage.clipsToBounds = true |
| 46 | + return restaurantImage |
| 47 | + }() |
| 48 | + |
| 49 | + private lazy var wrapperStackView: UIStackView = { |
| 50 | + let wrapperStackView = UIStackView(arrangedSubviews: [nameAndCategoryStackView, restaurantImage]) |
| 51 | + wrapperStackView.translatesAutoresizingMaskIntoConstraints = false |
| 52 | + wrapperStackView.alignment = .firstBaseline |
| 53 | + return wrapperStackView |
| 54 | + }() |
| 55 | + |
| 56 | + convenience init() { |
| 57 | + self.init(frame: .zero) |
| 58 | + setup() |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +extension RestaurantInfoView: ViewCode { |
| 64 | + |
| 65 | + func setupSubviews() { |
| 66 | + addSubview(wrapperStackView) |
| 67 | + } |
| 68 | + |
| 69 | + func setupConstraints() { |
| 70 | + setupWrapperStackViewConstraints() |
| 71 | + setupRestaurantImageConstraints() |
| 72 | + } |
| 73 | + |
| 74 | + private func setupWrapperStackViewConstraints() { |
| 75 | + NSLayoutConstraint.activate([ |
| 76 | + wrapperStackView.topAnchor.constraint(equalTo: topAnchor), |
| 77 | + wrapperStackView.bottomAnchor.constraint(equalTo: bottomAnchor), |
| 78 | + wrapperStackView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 79 | + wrapperStackView.trailingAnchor.constraint(equalTo: trailingAnchor) |
| 80 | + ]) |
| 81 | + } |
| 82 | + |
| 83 | + private func setupRestaurantImageConstraints() { |
| 84 | + let sizeImage = CGSize(width: 48, height: 48) |
| 85 | + NSLayoutConstraint.activate([ |
| 86 | + restaurantImage.widthAnchor.constraint(equalToConstant: sizeImage.width), |
| 87 | + restaurantImage.heightAnchor.constraint(equalToConstant: sizeImage.height) |
| 88 | + ]) |
| 89 | + restaurantImage.layer.cornerRadius = sizeImage.width / 2 |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments