MessageView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // MessageView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/4/24.
  6. // Copyright © 2019 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. class MessageView: BaseView {
  10. let oneImages = ["my_message_system","my_message_active","my_message_indent"]
  11. let oneTitles = ["星球通知","活动公告","订单消息"]
  12. let twoImages = ["my_message_comment","my_message_like","my_message_attention","my_message_letter"]
  13. let twoTitles = ["评论和@","喜欢和赞","关注我的","私信"]
  14. let twoDetailTitles = ["暂未收到评论和@,多去互动吧","暂未收到喜欢和赞,多去互动吧","暂无关注,多去互动吧","暂无私信"]
  15. typealias DidSelectClosure = (_ indexPath: IndexPath) -> Void
  16. var didSelectClosure : DidSelectClosure?
  17. enum NotificationAuthorizationStatus {
  18. case authorized
  19. case notDetermined
  20. case denied
  21. }
  22. var authorizationStatus :NotificationAuthorizationStatus?
  23. override func setupViews() {
  24. addSubview(collectionView)
  25. if #available(iOS 10.0, *) {
  26. let unUserNotificationCenter = UNUserNotificationCenter.current()
  27. unUserNotificationCenter.getNotificationSettings {
  28. settings in
  29. switch settings.authorizationStatus {
  30. case .authorized:
  31. self.authorizationStatus = .authorized
  32. return
  33. case .notDetermined:
  34. self.authorizationStatus = .notDetermined
  35. // //请求授权
  36. // UNUserNotificationCenter.current()
  37. // .requestAuthorization(options: [.alert, .sound, .badge]) {
  38. // (accepted, error) in
  39. // if !accepted {
  40. // print("用户不允许消息通知。")
  41. // }
  42. // }
  43. break
  44. case .denied:
  45. self.authorizationStatus = .denied
  46. break
  47. default:
  48. break
  49. }
  50. }
  51. self.collectionView.reloadData()
  52. } else {
  53. // Fallback on earlier versions
  54. }
  55. }
  56. override func setupLayouts() {
  57. collectionView.snp.makeConstraints { (make) in
  58. make.edges.equalToSuperview()
  59. }
  60. }
  61. private lazy var collectionView: UICollectionView = {
  62. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  63. collectionView.backgroundColor = kf7f8faColor
  64. collectionView.delegate = self;
  65. collectionView.dataSource = self;
  66. collectionView.showsVerticalScrollIndicator = false
  67. collectionView.showsHorizontalScrollIndicator = false
  68. collectionView.cornerRadius = 2.5
  69. collectionView.masksToBounds = true
  70. return collectionView
  71. }()
  72. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  73. let collectionViewLayout = UICollectionViewFlowLayout.init()
  74. // collectionViewLayout.estimatedItemSize = CGSize(width: kScaleWidth, height: 10)
  75. return collectionViewLayout
  76. }()
  77. }
  78. extension MessageView: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  79. func numberOfSections(in collectionView: UICollectionView) -> Int {
  80. return 2
  81. }
  82. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  83. switch section {
  84. case 0:
  85. return 3
  86. case 1:
  87. return 4
  88. default:
  89. return 0
  90. }
  91. }
  92. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  93. switch indexPath.section {
  94. case 0:
  95. let cell = MessageOneCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  96. cell.setCell(titles: oneTitles, images: oneImages)
  97. return cell
  98. case 1:
  99. let cell = MessageTwoCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  100. cell.setCell(titles: twoTitles, detailTitles: twoDetailTitles, images: twoImages)
  101. return cell
  102. default:
  103. return UICollectionViewCell()
  104. }
  105. }
  106. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  107. switch indexPath.section {
  108. case 0:
  109. return CGSize(width:kScreenWidth/3, height: kScaleValue(value: 40)+70)
  110. case 1:
  111. return CGSize(width:kScreenWidth, height: 91)
  112. default:
  113. return CGSize(width:0.0, height: 0.0)
  114. }
  115. }
  116. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  117. switch section {
  118. case 0:
  119. if self.authorizationStatus == .notDetermined || self.authorizationStatus == .denied {
  120. return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  121. }else if self.authorizationStatus == .authorized {
  122. return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
  123. }
  124. return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
  125. default:
  126. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  127. }
  128. }
  129. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  130. switch section {
  131. case 0:
  132. return 10
  133. default:
  134. return 0
  135. }
  136. }
  137. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  138. switch section {
  139. case 0:
  140. return 0
  141. default:
  142. return 0
  143. }
  144. }
  145. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  146. if kind == UICollectionView.elementKindSectionHeader {
  147. switch indexPath.section {
  148. case 0:
  149. let headerView = MessageNoticeHeaderCollectionReusableView.headerWith(collectionView: collectionView, kind: UICollectionView.elementKindSectionHeader, indexPath: indexPath)
  150. return headerView
  151. default:
  152. return UICollectionReusableView()
  153. }
  154. }
  155. return UICollectionReusableView()
  156. }
  157. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  158. switch section {
  159. case 0:
  160. if self.authorizationStatus == .notDetermined || self.authorizationStatus == .denied {
  161. return CGSize(width: kScreenWidth, height: kScaleValue(value: 36))
  162. }else if self.authorizationStatus == .authorized {
  163. return CGSize(width: kScreenWidth, height: 0)
  164. }
  165. return CGSize(width: kScreenWidth, height: 0)
  166. default:
  167. return CGSize(width: kScreenWidth, height: 0)
  168. }
  169. }
  170. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  171. if let didSelectClosure = self.didSelectClosure {
  172. didSelectClosure(indexPath)
  173. }
  174. }
  175. }