PersonViewUserAndOtherListView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // PersonViewUserAndOtherListView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/6/17.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyMediator
  10. class PersonViewUserAndOtherListView: BaseView {
  11. deinit {
  12. listViewDidScrollCallback = nil
  13. if observe != nil {
  14. NotificationCenter.default.removeObserver(observe!)
  15. }
  16. }
  17. weak var observe : NSObjectProtocol?
  18. var listViewDidScrollCallback: ((UIScrollView) -> ())?
  19. var jumpModeType : JumpModeType? {
  20. didSet {
  21. collectionView.snp_makeConstraints { (make) in
  22. make.top.right.left.equalToSuperview()
  23. if jumpModeType == JumpModeType.none {
  24. make.height.equalTo(kScreenHeight-kNavBarTotalHeight-kTabBarTotalHeight-44)
  25. }else {
  26. make.height.equalTo(kScreenHeight-kNavBarTotalHeight-kSafeTabBarHeight-44)
  27. }
  28. }
  29. }
  30. }
  31. /// 用户Id
  32. var uid:Int?
  33. /// 发布,收藏,分享类型
  34. var personalCenterVCType : Int?
  35. /// 模型数组
  36. var postMyModels = Array<PostMyModel>()
  37. override func setupViews() {
  38. addSubview(collectionView)
  39. }
  40. override func setupData() {
  41. collectionView.addHeader(withBeginRefresh: true, animation: false, refreshBlock: { [weak self] (page) in
  42. self?.communityPostMyApi(page: page)
  43. })
  44. collectionView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  45. [weak self] (page) in
  46. self?.communityPostMyApi(page: page)
  47. })
  48. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("communityDeletePostApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  49. let postId = notification.object as? Int
  50. if !(self?.postMyModels.isEmpty ?? true) {
  51. for (index,postMyModel) in (self?.postMyModels)!.enumerated() {
  52. if postMyModel.id == postId {
  53. self?.postMyModels.remove(at: index)
  54. }
  55. }
  56. self?.collectionView.reloadData()
  57. }
  58. }
  59. }
  60. lazy var collectionView: UICollectionView = {
  61. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  62. collectionView.backgroundColor = kf7f8faColor
  63. collectionView.delegate = self;
  64. collectionView.dataSource = self;
  65. collectionView.showsVerticalScrollIndicator = false
  66. collectionView.showsHorizontalScrollIndicator = false
  67. return collectionView
  68. }()
  69. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  70. let collectionViewLayout = UICollectionViewFlowLayout.init()
  71. collectionViewLayout.minimumLineSpacing = 1
  72. collectionViewLayout.minimumInteritemSpacing = 1
  73. return collectionViewLayout
  74. }()
  75. }
  76. extension PersonViewUserAndOtherListView {
  77. func communityPostMyApi(page:Int) {
  78. var type : CommunityPostMyType!
  79. if personalCenterVCType == 0 {
  80. type = .create
  81. }
  82. if personalCenterVCType == 1 {
  83. type = .collect
  84. }
  85. if personalCenterVCType == 2 {
  86. type = .share
  87. }
  88. SwiftMoyaNetWorkServiceCommunity.shared().communityPostMyApi(type: type,uid:uid ?? 0,page:page, completion: {
  89. [weak self] (communityPostMyModel) -> (Void) in
  90. DIYEmptyView.emptyNoDataCollectionView(collectionView: self?.collectionView, detailStr: .one)
  91. let communityPostMyModel = communityPostMyModel as? CommunityPostMyModel
  92. if communityPostMyModel?.pagination?.currentPage == 1{
  93. self?.postMyModels.removeAll()
  94. self?.collectionView.resetNoMoreData()
  95. }
  96. self?.postMyModels = (self?.postMyModels)! + (communityPostMyModel?.data!)!
  97. self?.collectionView.reloadData()
  98. MJRefreshManager.hiddenHeaderWithFooter(collectionView: self?.collectionView, pagination: communityPostMyModel?.pagination)
  99. }) {
  100. [weak self] _ in
  101. MJRefreshManager.hiddenHeaderWithFooterNONetWork(collectionView: self?.collectionView)
  102. }
  103. }
  104. }
  105. extension PersonViewUserAndOtherListView: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  106. func numberOfSections(in collectionView: UICollectionView) -> Int {
  107. return 1
  108. }
  109. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  110. return postMyModels.isEmpty ? 0 : postMyModels.count
  111. }
  112. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  113. let cell = PersonViewUserAndOtherListCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  114. cell.uid = self.uid
  115. cell.type = self.personalCenterVCType
  116. cell.postMyModel = postMyModels[indexPath.row]
  117. return cell
  118. }
  119. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  120. let postMyModel = postMyModels[indexPath.row]
  121. if PostType(rawValue: postMyModel.type ?? "video") == .video {
  122. Mediator.push(CommunityRouterModuleType.pushPostDetailVoide(postId: "\(postMyModel.id ?? 0)", departType: .personal))
  123. }else {
  124. let vc = CommunityRecommendController()
  125. vc.id = postMyModel.id ?? 0
  126. findViewController().navigationController?.pushViewController(vc, animated: true)
  127. }
  128. }
  129. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  130. return CGSize(width:(kScreenWidth-4)/3, height:(kScreenWidth-4)/3)
  131. }
  132. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  133. return UIEdgeInsets(top:1, left: 1, bottom: 10, right: 1)
  134. }
  135. public func scrollViewDidScroll(_ scrollView: UIScrollView) {
  136. self.listViewDidScrollCallback?(scrollView)
  137. }
  138. }
  139. extension PersonViewUserAndOtherListView: JXPagingViewListViewDelegate {
  140. public func listView() -> UIView {
  141. return self
  142. }
  143. public func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
  144. self.listViewDidScrollCallback = callback
  145. }
  146. public func listScrollView() -> UIScrollView {
  147. return self.collectionView
  148. }
  149. }