|
| 1 | +// |
| 2 | +// ProductDetailsAddToCartView.swift |
| 3 | +// SuperCart |
| 4 | +// |
| 5 | +// Created by Nirupama Abraham on 01/04/19. |
| 6 | +// Copyright © 2019 Team A. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +protocol ProductDetailsAddToCartProtocol: class { |
| 12 | + func addToCart(_ quantity: Int) |
| 13 | +} |
| 14 | + |
| 15 | +class ProductDetailsAddToCartView: UIView { |
| 16 | + |
| 17 | + // add to cart button |
| 18 | + private let addToCartButton: UIButton = { |
| 19 | + let button = UIButton() |
| 20 | + button.translatesAutoresizingMaskIntoConstraints = false |
| 21 | + button.setTitle("Add to Cart", for: .normal) |
| 22 | + button.tintColor = .white |
| 23 | + button.isUserInteractionEnabled = true |
| 24 | + button.backgroundColor = .blue |
| 25 | + return button |
| 26 | + }() |
| 27 | + |
| 28 | + // edit quantity view |
| 29 | + private let editQuantityView: EditQuantityView = { |
| 30 | + let editQuantityView = EditQuantityView() |
| 31 | + editQuantityView.translatesAutoresizingMaskIntoConstraints = false |
| 32 | + return editQuantityView |
| 33 | + }() |
| 34 | + |
| 35 | + private var quantity = 1 |
| 36 | + |
| 37 | + public weak var delegate: ProductDetailsAddToCartProtocol? |
| 38 | + |
| 39 | + // MARK:- init |
| 40 | + override init(frame: CGRect) { |
| 41 | + super.init(frame: frame) |
| 42 | + |
| 43 | + layoutSetUp() |
| 44 | + } |
| 45 | + |
| 46 | + required init?(coder aDecoder: NSCoder) { |
| 47 | + super.init(coder: aDecoder) |
| 48 | + } |
| 49 | + |
| 50 | + private func layoutSetUp() { |
| 51 | + |
| 52 | + editQuantityView.delegate = self |
| 53 | + editQuantityView.quantity = quantity |
| 54 | + |
| 55 | + [editQuantityView, addToCartButton].forEach { addSubview($0) } |
| 56 | + |
| 57 | + editQuantityView.anchors(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: addToCartButton.leadingAnchor, padding: .init(top: 16, left: 16, bottom: 16, right: 10), size: .init(width: 100, height: 0)) |
| 58 | + |
| 59 | + |
| 60 | + addToCartButton.anchors(top: topAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 16, left: 0, bottom: 16, right: 16)) |
| 61 | + |
| 62 | + addToCartButton.addTarget(self, action: #selector(addToCartTapped), for: .touchUpInside) |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @objc private func addToCartTapped() { |
| 67 | + delegate?.addToCart(quantity) |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +extension ProductDetailsAddToCartView: EditQuantityViewProtocol { |
| 73 | + func quantityUpdated(_ quantity: Int) { |
| 74 | + self.quantity = quantity |
| 75 | + } |
| 76 | +} |
0 commit comments