RedemptionAreaViewController.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // RedemptionAreaViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/7/16.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyMediator
  10. import Kingfisher
  11. class RedemptionAreaViewController: BaseViewController {
  12. override func didReceiveMemoryWarning() {
  13. super.didReceiveMemoryWarning()
  14. //KingfisherManager.shared.cache.clearDiskCache()
  15. KingfisherManager.shared.cache.clearMemoryCache()
  16. }
  17. deinit {
  18. NXLLog("deinit")
  19. if observe != nil {
  20. NotificationCenter.default.removeObserver(observe!)
  21. }
  22. }
  23. weak var observe : NSObjectProtocol?
  24. var cmsRedemptionAreaModel : CMSRedemptionAreaModel?
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. setupViews()
  28. setupLayouts()
  29. setupData()
  30. }
  31. override func setupViews() {
  32. navigationBar.title = "积分商城"
  33. view.addSubview(tableView)
  34. view.insertSubview(navigationBar, aboveSubview: tableView)
  35. }
  36. lazy var tableView: UITableView = {
  37. let tableView = UITableView(frame: CGRect(x: 0, y: kNavBarTotalHeight, width: kScreenWidth, height: kScreenHeight - kNavBarTotalHeight), style: UITableView.Style.grouped)
  38. tableView.separatorStyle = .none
  39. tableView.backgroundColor = UIColor.white
  40. tableView.dataSource = self
  41. tableView.delegate = self
  42. return tableView
  43. }()
  44. override func setupData() {
  45. tableView.addHeader(withBeginRefresh: true, animation: true) {
  46. [weak self] (page) in
  47. self?.cmsTemplateExchangeApi()
  48. }
  49. navigationBar.onClickRightButton = {
  50. Mediator.push(H5RouterModuleType.pushCategory)
  51. }
  52. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("RedemptionAreaModuleTop"), object: nil, queue: OperationQueue.main, using: {
  53. [weak self] (notification) in
  54. if self?.tableView.contentOffset == CGPoint(x: 0, y: 0) {
  55. self?.tableView.mj_header.beginRefreshing()
  56. }else {
  57. self?.tableView.scrollToTop()
  58. }
  59. })
  60. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("login"), object: nil, queue: OperationQueue.main, using: {
  61. [weak self] (notification) in
  62. self?.tableView.mj_header.beginRefreshing()
  63. })
  64. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("loginOut"), object: nil, queue: OperationQueue.main, using: {
  65. [weak self] (notification) in
  66. self?.tableView.mj_header.beginRefreshing()
  67. })
  68. }
  69. }
  70. extension RedemptionAreaViewController {
  71. /// 加载cms
  72. func cmsTemplateExchangeApi() {
  73. SwiftMoyaNetWorkServiceCMS.shared().cmsTemplateExchangeApi(completion: {
  74. [weak self] (cmsRedemptionAreaModel) -> (Void) in
  75. DIYEmptyView.emptyNoDataTableView(tableView: self?.tableView)
  76. self?.cmsRedemptionAreaModel = cmsRedemptionAreaModel as? CMSRedemptionAreaModel
  77. self?.navigationBar.wr_setRightButton(image: kImage(name: "navbar_class_black")!)
  78. self?.navigationBar.rightButton.isHidden = false
  79. self?.navigationBar.title = self?.cmsRedemptionAreaModel?.title
  80. self?.tableView.reloadData()
  81. MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
  82. }) {
  83. [weak self] loadingStatus in
  84. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  85. }
  86. }
  87. }
  88. extension RedemptionAreaViewController : UITableViewDelegate, UITableViewDataSource {
  89. func numberOfSections(in tableView: UITableView) -> Int {
  90. return self.cmsRedemptionAreaModel?.content?.isEmpty ?? true ? 0 : (self.cmsRedemptionAreaModel?.content?.count)!
  91. }
  92. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  93. return self.cmsRedemptionAreaModel?.content?[section].rule?.isEmpty ?? true ? 0 : 1
  94. }
  95. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  96. let cmsRedemptionAreaContent = self.cmsRedemptionAreaModel?.content?[indexPath.section]
  97. switch cmsRedemptionAreaContent?.cmsRedemptionAreaContentType {
  98. case .banner?:
  99. let cell = RedemptionAreaBannerTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  100. cell.cmsRedemptionAreaContent = cmsRedemptionAreaContent
  101. return cell
  102. case .specialOne?:
  103. let cell = RedemptionAreaSpecialOneTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  104. cell.cmsRedemptionAreaContent = cmsRedemptionAreaContent
  105. return cell
  106. case .specialTwo?:
  107. let cell = RedemptionAreaSpecialTwoTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  108. cell.cmsRedemptionAreaContent = cmsRedemptionAreaContent
  109. return cell
  110. case .floor?:
  111. let cell = RedemptionAreaFloorTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  112. cell.cmsRedemptionAreaContent = cmsRedemptionAreaContent
  113. return cell
  114. default:
  115. return UITableViewCell()
  116. }
  117. }
  118. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  119. let cmsRedemptionAreaContent = self.cmsRedemptionAreaModel?.content?[indexPath.section]
  120. switch cmsRedemptionAreaContent?.cmsRedemptionAreaContentType {
  121. case .banner?:
  122. if UserModel.isTokenNil() {
  123. return (150 * kScaleWidth)
  124. }else {
  125. if cmsRedemptionAreaContent?.rule?.isEmpty ?? true {
  126. return 105
  127. }else {
  128. return (150 * kScaleWidth) + 105
  129. }
  130. }
  131. case .specialOne?:
  132. if cmsRedemptionAreaContent?.rule?.isEmpty ?? true {
  133. return 0
  134. }else {
  135. return (193 * kScaleWidth) + 30
  136. }
  137. case .specialTwo?:
  138. if cmsRedemptionAreaContent?.rule?.isEmpty ?? true {
  139. return 0
  140. }else {
  141. return (275 * kScaleWidth) + 30
  142. }
  143. case .floor?:
  144. if cmsRedemptionAreaContent?.rule?.isEmpty ?? true {
  145. return 0
  146. }else {
  147. let count = Int((cmsRedemptionAreaContent?.rule?.count ?? 0) / 2)
  148. if ((cmsRedemptionAreaContent?.rule?.count ?? 0) % 2) == 0 {
  149. return CGFloat(CGFloat(count) * ((kScreenWidth-15)/2 + 116.0)) + CGFloat(5 * CGFloat(count))
  150. }else {
  151. return CGFloat(CGFloat(count) * ((kScreenWidth-15)/2 + 116.0)) + CGFloat((kScreenWidth-15)/2 + 116.0) + CGFloat(5 * (CGFloat(count) + 1))
  152. }
  153. }
  154. default:
  155. return 0
  156. }
  157. }
  158. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  159. let cmsRedemptionAreaContent = self.cmsRedemptionAreaModel?.content?[section]
  160. switch cmsRedemptionAreaContent?.cmsRedemptionAreaContentType {
  161. case .banner?:
  162. return 0.000001
  163. default:
  164. if cmsRedemptionAreaContent?.floorImg != nil && cmsRedemptionAreaContent?.floorImg != "" {
  165. return 88*kScaleWidth
  166. }else {
  167. return 0.000001
  168. }
  169. }
  170. }
  171. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  172. let cmsRedemptionAreaContent = self.cmsRedemptionAreaModel?.content?[section]
  173. switch cmsRedemptionAreaContent?.cmsRedemptionAreaContentType {
  174. case .banner?:
  175. return nil
  176. default:
  177. if cmsRedemptionAreaContent?.floorImg != nil && cmsRedemptionAreaContent?.floorImg != "" {
  178. let headerView = FloorHeaderViewFloorHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 88*kScaleWidth))
  179. headerView.cmsRedemptionAreaContent = cmsRedemptionAreaContent
  180. return headerView
  181. }else {
  182. return nil
  183. }
  184. }
  185. }
  186. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  187. if self.cmsRedemptionAreaModel?.content?.isEmpty ?? true {
  188. return 0.000001
  189. }else {
  190. return 10
  191. }
  192. }
  193. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  194. if self.cmsRedemptionAreaModel?.content?.isEmpty ?? true {
  195. return nil
  196. }else {
  197. if section == (self.cmsRedemptionAreaModel?.content?.count ?? 1) - 1 {
  198. return nil
  199. }else {
  200. let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 10))
  201. view.backgroundColor = kf7f8faColor
  202. return view
  203. }
  204. }
  205. }
  206. }