Skip to content

Commit d583b89

Browse files
author
Nirupama Miriam Abraham
committedApr 1, 2019
Implemented 'add to cart' functionality from PDP.
1 parent 1e36747 commit d583b89

File tree

8 files changed

+119
-12
lines changed

8 files changed

+119
-12
lines changed
 

‎SuperCart.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
D44D99C9224FBA0600CDF544 /* ProductDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D44D99C8224FBA0600CDF544 /* ProductDetailsView.swift */; };
3131
D44D99CB22506F4400CDF544 /* ProductDetailsView+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = D44D99CA22506F4400CDF544 /* ProductDetailsView+Ext.swift */; };
3232
D44D99CD2250C0D500CDF544 /* ProductDetailsSectionHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D44D99CC2250C0D500CDF544 /* ProductDetailsSectionHeaderView.swift */; };
33+
D44D99CF22521F7200CDF544 /* ProductDetailsAddToCartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D44D99CE22521F7200CDF544 /* ProductDetailsAddToCartView.swift */; };
3334
D4A3BF9C2248EE4F008FC597 /* NetworkLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4A3BF9A2248EE4F008FC597 /* NetworkLayer.swift */; };
3435
D4A3BF9D2248EE4F008FC597 /* DataServiceProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4A3BF9B2248EE4F008FC597 /* DataServiceProvider.swift */; };
3536
D4A3BFA12248EE80008FC597 /* Product.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4A3BF9F2248EE7F008FC597 /* Product.swift */; };
@@ -86,6 +87,7 @@
8687
D44D99C8224FBA0600CDF544 /* ProductDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductDetailsView.swift; sourceTree = "<group>"; };
8788
D44D99CA22506F4400CDF544 /* ProductDetailsView+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProductDetailsView+Ext.swift"; sourceTree = "<group>"; };
8889
D44D99CC2250C0D500CDF544 /* ProductDetailsSectionHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductDetailsSectionHeaderView.swift; sourceTree = "<group>"; };
90+
D44D99CE22521F7200CDF544 /* ProductDetailsAddToCartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductDetailsAddToCartView.swift; sourceTree = "<group>"; };
8991
D4A3BF9A2248EE4F008FC597 /* NetworkLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkLayer.swift; sourceTree = "<group>"; };
9092
D4A3BF9B2248EE4F008FC597 /* DataServiceProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataServiceProvider.swift; sourceTree = "<group>"; };
9193
D4A3BF9F2248EE7F008FC597 /* Product.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Product.swift; sourceTree = "<group>"; };
@@ -238,6 +240,7 @@
238240
D44D99C8224FBA0600CDF544 /* ProductDetailsView.swift */,
239241
D44D99CA22506F4400CDF544 /* ProductDetailsView+Ext.swift */,
240242
D44D99CC2250C0D500CDF544 /* ProductDetailsSectionHeaderView.swift */,
243+
D44D99CE22521F7200CDF544 /* ProductDetailsAddToCartView.swift */,
241244
);
242245
path = VIew;
243246
sourceTree = "<group>";
@@ -464,6 +467,7 @@
464467
D4A3BFA72248F0D9008FC597 /* UIView+Ext.swift in Sources */,
465468
AD73B72E2249314B00A98CC2 /* InputViewController.swift in Sources */,
466469
ADB157D3224688BB000A2135 /* ChatViewController.swift in Sources */,
470+
D44D99CF22521F7200CDF544 /* ProductDetailsAddToCartView.swift in Sources */,
467471
D4A3BF9C2248EE4F008FC597 /* NetworkLayer.swift in Sources */,
468472
D4A3BF9D2248EE4F008FC597 /* DataServiceProvider.swift in Sources */,
469473
D44D99C6224FAD1300CDF544 /* ProductDetailsTableViewCell.swift in Sources */,

‎SuperCart/AppManager.swift

+9
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,14 @@ class AppManager {
9797
.noodles: []
9898
]
9999
}
100+
101+
public func addProductToCart(_ productId: Int,_ quantity: Int) {
102+
if let product = (self.products.filter { (product) -> Bool in
103+
return product.id == productId
104+
}).first {
105+
product.quantity = quantity
106+
product.isPreselected = true
107+
}
108+
}
100109
}
101110

‎SuperCart/Entities/Product.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Product {
3535
func getOrderParams() -> [String: Any] {
3636
let params: [String: Any] = [
3737
"id": id,
38-
"qty": quantity,
38+
"qty": "\(quantity)",
3939
"cat": category,
4040
"subCat": subCategory
4141
]

‎SuperCart/ProductDetails/Controller/ProductDetailsViewController.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class ProductDetailsViewController: UIViewController {
2222
}()
2323

2424
// add to cart view
25-
private let addToCartView: AddToCartView = {
26-
let addToCartView = AddToCartView()
25+
private let addToCartView: ProductDetailsAddToCartView = {
26+
let addToCartView = ProductDetailsAddToCartView()
2727
addToCartView.translatesAutoresizingMaskIntoConstraints = false
2828
return addToCartView
2929
}()
@@ -76,7 +76,9 @@ class ProductDetailsViewController: UIViewController {
7676
}
7777

7878
//MARK:- AddToCartProtocol
79-
extension ProductDetailsViewController: AddToCartProtocol {
80-
func addToCart() {
79+
extension ProductDetailsViewController: ProductDetailsAddToCartProtocol {
80+
func addToCart(_ quantity: Int) {
81+
guard let product = model else { return }
82+
AppManager.shared.addProductToCart(product.id, quantity)
8183
}
8284
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

‎SuperCart/ProductList/Cells/ProductListTableViewCell.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ extension ProductListTableViewCell: UICollectionViewDelegate {
103103
} else {
104104
delegate?.itemAdded(product)
105105
}
106-
// collectionView.reloadItems(at: [indexPath])
106+
collectionView.reloadItems(at: [indexPath])
107107
}
108108
}
109109

‎SuperCart/ProductList/Controller/ProductListViewController.swift

+18-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ class ProductListViewController: UIViewController {
4242
var productsList: ProductsList? {
4343
didSet {
4444
self.productListView.productsList = productsList
45-
let selectedProducts: [Product] = productsList?.categories.flatMap { (category) -> [Product] in
46-
return category.products.filter({ (product) -> Bool in
47-
return product.isPreselected
48-
})
49-
} ?? []
50-
self.selectedProducts = selectedProducts
45+
self.selectedProducts = getSelectedProducts()
5146
}
5247
}
5348

@@ -65,6 +60,14 @@ class ProductListViewController: UIViewController {
6560
layoutSetUp()
6661
}
6762

63+
override func viewDidAppear(_ animated: Bool) {
64+
super.viewDidAppear(animated)
65+
if productsList != nil {
66+
productListView.reload()
67+
selectedProducts = getSelectedProducts()
68+
}
69+
}
70+
6871
private func layoutSetUp() {
6972

7073
[productListView, addToCartView, activityIndicator].forEach { view.addSubview($0) }
@@ -101,6 +104,15 @@ class ProductListViewController: UIViewController {
101104
self?.activityIndicator.stopAnimating()
102105
}
103106
}
107+
108+
private func getSelectedProducts() -> [Product] {
109+
let selectedProducts: [Product] = productsList?.categories.flatMap { (category) -> [Product] in
110+
return category.products.filter({ (product) -> Bool in
111+
return product.isPreselected
112+
})
113+
} ?? []
114+
return selectedProducts
115+
}
104116
}
105117

106118
//MARK:- ProductListProtocol

‎SuperCart/ProductList/View/ProductListView.swift

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class ProductListView: UIView {
5757
tableView.estimatedRowHeight = 150
5858
backgroundColor = .white
5959
}
60+
61+
public func reload() {
62+
self.tableView.reloadData()
63+
}
6064
}
6165

6266
//MARK:- UITableViewDataSource

0 commit comments

Comments
 (0)