ShoppingCartView.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // ShoppingCartView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/5/8.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 购物车--首页View
  8. import UIKit
  9. class ShoppingCartView: BaseView {
  10. // 购物车列表ModelArr
  11. var cartListModelArr : Array<CartProductListModel>? {
  12. didSet {
  13. tableView.reloadData()
  14. self.judgeAllSelectedStatus()
  15. }
  16. }
  17. // 热销ModelArr
  18. var hotSaleModelArr : Array<ProductSearchModel>? {
  19. didSet {
  20. let sectionIdx = cartListModelArr?.count ?? 1
  21. self.tableView.reloadSections([sectionIdx], with: UITableView.RowAnimation.none)
  22. }
  23. }
  24. typealias OrderPayTransBlock = () -> Void
  25. var orderPayTransBlock : OrderPayTransBlock?
  26. override func setupViews() {
  27. self.backgroundColor = kf7f8faColor
  28. addSubview(accountView)
  29. addSubview(tableView)
  30. let emptyView = EmptyView.shared.diyCustomEmptyViewStyle2(iconStr: "page04", titleStr: "当前暂无数据")
  31. emptyView.contentViewY = kScaleValue(value: 182)
  32. tableView.ly_emptyView = emptyView
  33. tableView.ly_startLoading()
  34. }
  35. override func setupLayouts() {
  36. accountView.snp.makeConstraints { (make) in
  37. make.left.right.bottom.equalToSuperview()
  38. make.height.equalTo(48)
  39. }
  40. tableView.snp.makeConstraints { (make) in
  41. make.top.left.right.equalToSuperview()
  42. make.bottom.equalTo(accountView.snp_top).offset(0)
  43. }
  44. }
  45. lazy var accountView: ShoppingCartAccountView = {
  46. let accountView = ShoppingCartAccountView()
  47. accountView.allSelectBlock = { [weak self]
  48. (isAllSelected) in
  49. self?.allSelectedAction(isAllSelected)
  50. }
  51. accountView.orderPayBlock = {
  52. [weak self] in
  53. if let orderPayTransBlock = self?.orderPayTransBlock {
  54. orderPayTransBlock()
  55. }
  56. }
  57. return accountView
  58. }()
  59. lazy var tableView: UITableView = {
  60. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  61. tableView.separatorStyle = .none
  62. tableView.backgroundColor = kf7f8faColor
  63. tableView.dataSource = self
  64. tableView.delegate = self
  65. tableView.estimatedRowHeight = 0.000001
  66. tableView.estimatedSectionFooterHeight = 0.000001
  67. tableView.estimatedSectionHeaderHeight = 0.000001
  68. return tableView
  69. }()
  70. }
  71. // MARK: - tableView dataSource && delegate
  72. extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
  73. func numberOfSections(in tableView: UITableView) -> Int {
  74. // 购物车列表 + 超值热卖
  75. return cartListModelArr?.isEmpty ?? true ? 2 : cartListModelArr!.count + 1
  76. }
  77. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  78. if cartListModelArr?.isEmpty ?? true {
  79. return 1
  80. } else {
  81. if section < cartListModelArr!.count {
  82. return cartListModelArr![section].productList?.isEmpty ?? true ? 0 : cartListModelArr![section].productList!.count
  83. } else {
  84. return 1
  85. }
  86. }
  87. }
  88. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  89. if cartListModelArr?.count == 0 {
  90. switch indexPath.section {
  91. case 0:
  92. // 购物车列表为空
  93. let cell = ShoppingCartListNoneItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  94. return cell
  95. case 1:
  96. // 超值热卖
  97. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  98. hotSaleCell.frame = tableView.bounds
  99. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  100. hotSaleCell.layoutIfNeeded()
  101. hotSaleCell.reloadData()
  102. return hotSaleCell
  103. default:
  104. return UITableViewCell()
  105. }
  106. } else {
  107. if indexPath.section < cartListModelArr!.count {
  108. // 购物车列表Item
  109. let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  110. cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
  111. cell.productSelBlock = { [weak self]
  112. (isProductSel) in
  113. self?.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isProductSel
  114. self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
  115. self?.judgeAllSelectedStatus()
  116. }
  117. cell.changeProductBlock = { [weak self]
  118. (productId, type) in
  119. SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
  120. // 1.更新数据源
  121. let cartAmountMdl = amountData as? CartAmountModel
  122. let amount = cartAmountMdl?.amount
  123. self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount = amount
  124. // 2.刷新cell
  125. let indexPath = IndexPath(item: indexPath.row, section: indexPath.section)
  126. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  127. })
  128. }
  129. return cell
  130. } else {
  131. // 超值热卖
  132. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  133. hotSaleCell.frame = tableView.bounds
  134. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  135. hotSaleCell.layoutIfNeeded()
  136. hotSaleCell.reloadData()
  137. return hotSaleCell
  138. }
  139. }
  140. }
  141. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  142. return UITableView.automaticDimension
  143. }
  144. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  145. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  146. return 58
  147. } else {
  148. return 10
  149. }
  150. }
  151. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  152. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  153. let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
  154. headerView.shopName = cartListModelArr![section].shopName
  155. headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
  156. headerView.secSelectBlock = { [weak self]
  157. (isSectionSel) in
  158. self?.shopSelectedAction(isSectionSel, section: section)
  159. self?.judgeAllSelectedStatus()
  160. }
  161. return headerView
  162. } else {
  163. return nil
  164. }
  165. }
  166. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  167. return 0.000001
  168. }
  169. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  170. return nil
  171. }
  172. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  173. if editingStyle == .delete {
  174. // 删除商品
  175. let productId = cartListModelArr![indexPath.section].productList?[indexPath.row].id
  176. SwiftMoyaNetWorkServiceProduct.shared().productCartDeleteApi(id: productId ?? 0) { [weak self] (data) -> (Void) in
  177. self?.cartListModelArr![indexPath.section].productList?.remove(at: indexPath.row)
  178. tableView.deleteRows(at: [indexPath], with: .none)
  179. }
  180. }
  181. }
  182. func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  183. return "删除"
  184. }
  185. }
  186. // MARK: - 购物车逻辑处理
  187. extension ShoppingCartView {
  188. // 全选点击事件
  189. func allSelectedAction(_ isSelected: Int) {
  190. for cartProListMdl in cartListModelArr ?? [] {
  191. for productMdl in cartProListMdl.productList! {
  192. productMdl.isSelect = isSelected
  193. }
  194. }
  195. tableView.reloadData()
  196. }
  197. // 店铺(全选)点击事件
  198. func shopSelectedAction(_ isSelected: Int, section: Int) {
  199. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  200. for productMdl in cartProListMdl.productList! {
  201. productMdl.isSelect = isSelected
  202. }
  203. self.tableView.reloadSections([section], with: UITableView.RowAnimation.none)
  204. self.judgeAllSelectedStatus()
  205. }
  206. // 校验Section内商品是否全选
  207. func judgeSectionSelectedStatus(_ section: Int) -> Int {
  208. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  209. for productMdl in cartProListMdl.productList! {
  210. if productMdl.isSelect == 0 {
  211. return 0
  212. }
  213. }
  214. return 1
  215. }
  216. // 校验购物车内商品是否全选
  217. func judgeAllSelectedStatus() -> Void {
  218. let secCount: Int = cartListModelArr?.isEmpty ?? true ? 0 : cartListModelArr!.count
  219. for sec in 0..<secCount {
  220. let secStatus: Int = self.judgeSectionSelectedStatus(sec)
  221. if secStatus == 0 {
  222. self.accountView.isAllSelected = 0
  223. return
  224. }
  225. }
  226. self.accountView.isAllSelected = 1
  227. }
  228. }