Browse Source

Merge branch 'feature/liuxuqing' into develop

Chris 6 years ago
parent
commit
35d207b0ed

+ 19 - 8
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartAccountView.swift

@@ -12,9 +12,20 @@ import RxCocoa
 
 class ShoppingCartAccountView: BaseView {
     
+    typealias AllSelectBlock = (_ isAllSelected: Int) -> Void
+    var allSelectBlock : AllSelectBlock?
+    
     typealias OrderPayBlock = () -> Void
     var orderPayBlock : OrderPayBlock?
     
+    var isAllSelected: Int? {
+        didSet {
+            // 选中状态
+            let selStatus = isAllSelected == 1 ? true : false
+            allSelectBtn.isSelected = selStatus
+        }
+    }
+    
     override func setupViews() {
         self.backgroundColor = kffffffColor
         addSubview(allSelectBtn)
@@ -30,6 +41,7 @@ class ShoppingCartAccountView: BaseView {
             make.height.equalTo(30)
             make.width.equalTo(54)
         }
+        allSelectBtn.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.left, imageTitleSpace: 4)
 
         accountBtn.snp.makeConstraints { (make) in
             make.right.equalToSuperview().offset(-14)
@@ -56,16 +68,15 @@ class ShoppingCartAccountView: BaseView {
         allSelectBtn.setTitle("全选", for: UIControl.State.normal)
         allSelectBtn.setTitleColor(k333333Color, for: UIControl.State.normal)
         allSelectBtn.setTitleColor(k666666Color, for: UIControl.State.selected)
-        allSelectBtn.setImage(UIImage.imageWithColor(color: kDisabledButtonColor), for: UIControl.State.selected)
-        allSelectBtn.setImage(UIImage.imageWithColor(color: kEnabledButtonColor), for: UIControl.State.normal)
+        allSelectBtn.setImage(kImage(name: "common_uncheck_icon"), for: UIControl.State.normal)
+        allSelectBtn.setImage(kImage(name: "common_check_icon"), for: UIControl.State.selected)
         allSelectBtn.titleLabel?.font = kScaleRegularFont16
         allSelectBtn.rx.tap.subscribe(onNext: { [weak self] (data) in
-            
-//            if let loginBlock = self?.loginBlock {
-//                loginBlock((self?.phoneNumber)!,(self?.password)!)
-//            }
-            print("点击了全选")
-            
+            allSelectBtn.isSelected = !allSelectBtn.isSelected
+            if let allSelectBlock = self?.allSelectBlock {
+                let isAllSel: Int = allSelectBtn.isSelected == true ? 1 : 0
+                allSelectBlock(isAllSel)
+            }
         }).disposed(by: disposeBag)
         return allSelectBtn
     }()

+ 19 - 7
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartList/ShoppingCartListTableViewCell.swift

@@ -14,11 +14,19 @@ class ShoppingCartListTableViewCell: UITableViewCell {
     
     let disposeBag = DisposeBag()
     
+    // 选中商品
+    typealias ProductSelBlock = (_ isProductSelected: Int) -> Void
+    var productSelBlock : ProductSelBlock?
+    
+    // 加/减购物车数量
     typealias ChangeProductBlock = (_ id: Int,_ type : Int) -> Void
     var changeProductBlock : ChangeProductBlock?
     
     var productMdl : ProductModel? {
         didSet {
+            // 选中状态
+            let selStatus = productMdl?.isSelect == 1 ? true : false
+            selectedButton.isSelected = selStatus
             // 商品图片
             iconImageView.kf.setImage(with: kURLImage(name: productMdl?.productImg ?? ""), placeholder: kImage(name: "pic_preload"))
             // 商品名称
@@ -75,8 +83,6 @@ class ShoppingCartListTableViewCell: UITableViewCell {
     //MRAK: - 设置View
     private func setupViews() {
         self.selectionStyle = .none
-//        cornerRadius = 4
-//        masksToBounds = true
         
         addSubview(selectedButton)
         addSubview(iconImageView)
@@ -88,15 +94,13 @@ class ShoppingCartListTableViewCell: UITableViewCell {
         addSubview(plusButton)
         addSubview(numberLabel)
         addSubview(reduceButton)
-        
     }
     
     private func setupLayouts() {
         selectedButton.snp.makeConstraints { (make) in
-            make.left.equalTo(10)
-            make.top.equalTo(50)
-            make.bottom.equalTo(-80)
-            make.size.equalTo(18)
+            make.left.top.bottom.equalToSuperview()
+            make.width.equalTo(40)
+            make.height.equalTo(148)
         }
         iconImageView.snp.makeConstraints { (make) in
             make.left.equalToSuperview().offset(40)
@@ -153,6 +157,14 @@ class ShoppingCartListTableViewCell: UITableViewCell {
         let selectedButton = UIButton(type: UIButton.ButtonType.custom)
         selectedButton.setImage(kImage(name: "common_uncheck_icon"), for: UIControl.State.normal)
         selectedButton.setImage(kImage(name: "common_check_icon"), for: UIControl.State.selected)
+        selectedButton.rx.tap.subscribe(onNext: { [weak self] (data) in
+            selectedButton.isSelected = !selectedButton.isSelected
+            if let productSelBlock = self?.productSelBlock {
+                let isSel: Int = selectedButton.isSelected == true ? 1 : 0
+                productSelBlock(isSel)
+            }
+        }).disposed(by: disposeBag)
+        
         return selectedButton
     }()
 

+ 16 - 7
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartList/ShoppingCartListTableViewHeader.swift

@@ -12,6 +12,9 @@ import RxCocoa
 
 class ShoppingCartListTableViewHeader: BaseView {
     
+    typealias SecSelectBlock = (_ isSecSelected: Int) -> Void
+    var secSelectBlock : SecSelectBlock?
+    
     var shopName : String? {
         didSet {
             titleButton.setTitle(shopName, for: UIControl.State.normal)
@@ -19,6 +22,14 @@ class ShoppingCartListTableViewHeader: BaseView {
         }
     }
     
+    var isSectionSelected: Int? {
+        didSet {
+            // 选中状态
+            let selStatus = isSectionSelected == 1 ? true : false
+            selectedButton.isSelected = selStatus
+        }
+    }
+    
     override var frame: CGRect {
         get {
             return super.frame
@@ -55,7 +66,6 @@ class ShoppingCartListTableViewHeader: BaseView {
         
         titleButton.snp.makeConstraints { (make) in
             make.left.equalToSuperview().offset(36)
-//            make.top.equalTo(separateView.snp_bottom).offset(14)
             make.centerY.equalToSuperview()
             make.height.equalTo(20)
         }
@@ -98,12 +108,11 @@ class ShoppingCartListTableViewHeader: BaseView {
         selectedButton.setImage(kImage(name: "common_uncheck_icon"), for: UIControl.State.normal)
         selectedButton.setImage(kImage(name: "common_check_icon"), for: UIControl.State.selected)
         selectedButton.rx.tap.subscribe(onNext: { [weak self] (data) in
-            
-            //            if let loginBlock = self?.loginBlock {
-            //                loginBlock((self?.phoneNumber)!,(self?.password)!)
-            //            }
-            print("点击了全选当前section")
-            
+            selectedButton.isSelected = !selectedButton.isSelected
+            if let secSelectBlock = self?.secSelectBlock {
+                let isSectionSel: Int = selectedButton.isSelected == true ? 1 : 0
+                secSelectBlock(isSectionSel)
+            }
         }).disposed(by: disposeBag)
         return selectedButton
     }()

+ 74 - 1
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartView.swift

@@ -14,6 +14,7 @@ class ShoppingCartView: BaseView {
     var cartListModelArr : Array<CartProductListModel>? {
         didSet {
             tableView.reloadData()
+            self.judgeAllSelectedStatus()
         }
     }
     
@@ -52,6 +53,11 @@ class ShoppingCartView: BaseView {
     lazy var accountView: ShoppingCartAccountView = {
         let accountView = ShoppingCartAccountView()
         
+        accountView.allSelectBlock = { [weak self]
+            (isAllSelected) in
+            self?.allSelectedAction(isAllSelected)
+        }
+        
         accountView.orderPayBlock = {
             [weak self] in
             if let orderPayTransBlock = self?.orderPayTransBlock {
@@ -76,6 +82,7 @@ class ShoppingCartView: BaseView {
 
 }
 
+// MARK: - tableView dataSource && delegate
 extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
     func numberOfSections(in tableView: UITableView) -> Int {
         // 购物车列表 + 超值热卖
@@ -119,7 +126,17 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
                 // 购物车列表Item
                 let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
                 cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
-                cell.changeProductBlock = {
+                
+                cell.productSelBlock = { [weak self]
+                    (isProductSel) in
+                self?.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isProductSel
+                    
+                    self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
+                    self?.judgeAllSelectedStatus()
+                    
+                }
+                
+                cell.changeProductBlock = { [weak self]
                     (productId, type) in
                     SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
                         // 1.更新数据源
@@ -161,7 +178,14 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
         if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
             let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
             headerView.shopName = cartListModelArr![section].shopName
+            headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
+            headerView.secSelectBlock = { [weak self]
+                (isSectionSel) in
+                self?.shopSelectedAction(isSectionSel, section: section)
+                self?.judgeAllSelectedStatus()
+            }
             return headerView
+            
         } else {
             return nil
         }
@@ -190,3 +214,52 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
         return "删除"
     }
 }
+
+// MARK: - 购物车逻辑处理
+extension ShoppingCartView {
+    
+    // 全选点击事件
+    func allSelectedAction(_ isSelected: Int) {
+        for cartProListMdl in cartListModelArr ?? [] {
+            for productMdl in cartProListMdl.productList! {
+                productMdl.isSelect = isSelected
+            }
+        }
+        tableView.reloadData()
+    }
+    
+    // 店铺(全选)点击事件
+    func shopSelectedAction(_ isSelected: Int, section: Int) {
+        let cartProListMdl: CartProductListModel = cartListModelArr![section]
+        for productMdl in cartProListMdl.productList! {
+            productMdl.isSelect = isSelected
+        }
+        self.tableView.reloadSections([section], with: UITableView.RowAnimation.none)
+        self.judgeAllSelectedStatus()
+    }
+    
+    // 校验Section内商品是否全选
+    func judgeSectionSelectedStatus(_ section: Int) -> Int {
+        let cartProListMdl: CartProductListModel = cartListModelArr![section]
+        for productMdl in cartProListMdl.productList! {
+            if productMdl.isSelect == 0 {
+                return 0
+            }
+        }
+        return 1
+    }
+    
+    // 校验购物车内商品是否全选
+    func judgeAllSelectedStatus() -> Void {
+        let secCount: Int = cartListModelArr?.isEmpty ?? true ? 0 : cartListModelArr!.count
+        for sec in 0..<secCount {
+            let secStatus: Int = self.judgeSectionSelectedStatus(sec)
+            if secStatus == 0 {
+                self.accountView.isAllSelected = 0
+                return
+            }
+        }
+        self.accountView.isAllSelected = 1
+    }
+    
+}

+ 1 - 1
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/ViewController/ShoppingCartViewController.swift

@@ -49,7 +49,7 @@ class ShoppingCartViewController: BaseViewController {
             self?.productHotSaleApi(page: page)
         }
         
-        shoppingCartView.tableView.addFooterWithWithHeader(withAutomaticallyRefresh: true) { [weak self] (page) in
+        shoppingCartView.tableView.addFooterWithWithHeader(withAutomaticallyRefresh: false) { [weak self] (page) in
             self?.productHotSaleApi(page: page)
         }
     }

+ 1 - 1
RainbowPlanet/RainbowPlanet/Service/Model/ProductModel/ProductCartListModel.swift

@@ -39,7 +39,7 @@ class CartProductListModel : NSObject, Mappable{
         return CartProductListModel()
     }
     required init?(map: Map){}
-    private override init(){}
+    override init(){}
     
     func mapping(map: Map)
     {