ShareCommunityView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // ShareCommunityView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/7/10.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import FWPopupView
  10. import RxSwift
  11. import SwiftyMediator
  12. enum ShareCommunityViewType {
  13. case postDetail
  14. case activity
  15. }
  16. class ShareCommunityView: FWPopupView {
  17. let disposeBag = DisposeBag()
  18. let platformTitles = ["微信","朋友圈","QQ","微博","复制链接","保存图片","删除"]
  19. let platformImages = ["edit_pic_wechat","edit_pic_moments","edit_pic_qq","edit_pic_sina","edit_pic_link","edit_pic_download","edit_pic_delete"]
  20. let platformTitles1 = ["微信","朋友圈","QQ","微博","复制链接","保存图片","举报"]
  21. let platformImages1 = ["edit_pic_wechat","edit_pic_moments","edit_pic_qq","edit_pic_sina","edit_pic_link","edit_pic_download","edit_pic_report"]
  22. private var title : String?
  23. private var detailTitle : String?
  24. private var sharedTitle : String?
  25. private var sharedDetailTitle : String?
  26. private var h5Str : String?
  27. private var thumbnailImg : UIImage?
  28. private var shareCommunityViewType : ShareCommunityViewType?
  29. var uid : Int?
  30. var postId : Int?
  31. override init(frame: CGRect) {
  32. super.init(frame: frame)
  33. }
  34. typealias Completion = (() -> Void)
  35. var completion : Completion?
  36. typealias SaveCompletion = (() -> Void)
  37. var saveCompletion : SaveCompletion?
  38. typealias DeleteCompletion = (() -> Void)
  39. var deleteCompletion : DeleteCompletion?
  40. init(postId : Int = 0,uid:Int,title:String,detailTitle:String,h5Str:String,thumbnailImg:UIImage?,sharedTitle:String,sharedDetailTitle:String,shareCommunityViewType:ShareCommunityViewType) {
  41. self.init()
  42. self.postId = postId
  43. self.uid = uid
  44. self.title = title
  45. self.detailTitle = detailTitle
  46. self.h5Str = h5Str
  47. self.thumbnailImg = thumbnailImg
  48. self.sharedTitle = sharedTitle
  49. self.sharedDetailTitle = sharedDetailTitle
  50. self.shareCommunityViewType = shareCommunityViewType
  51. frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 209 + kSafeTabBarHeight)
  52. configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
  53. setupViews()
  54. setupLayouts()
  55. setupData()
  56. }
  57. required init?(coder aDecoder: NSCoder) {
  58. fatalError("init(coder:) has not been implemented")
  59. }
  60. func setupViews() {
  61. addSubview(titleLabel)
  62. addSubview(detailTitleLabel)
  63. addSubview(collectionView)
  64. addSubview(lineLabel)
  65. addSubview(cancelButton)
  66. }
  67. func setupLayouts() {
  68. titleLabel.snp.makeConstraints { (make) in
  69. make.top.equalTo(20)
  70. make.right.equalTo(-20)
  71. make.left.equalTo(20)
  72. make.height.equalTo(18)
  73. }
  74. detailTitleLabel.snp.makeConstraints { (make) in
  75. make.top.equalTo(titleLabel.snp.bottom).offset(10)
  76. make.right.equalTo(-30)
  77. make.left.equalTo(30)
  78. make.height.equalTo(18)
  79. }
  80. collectionView.snp.makeConstraints { (make) in
  81. make.top.equalTo(detailTitleLabel.snp.bottom).offset(20)
  82. make.height.equalTo(69)
  83. make.left.right.equalToSuperview()
  84. }
  85. lineLabel.snp.makeConstraints { (make) in
  86. make.top.equalTo(collectionView.snp.bottom).offset(10)
  87. make.left.right.equalToSuperview()
  88. make.height.equalTo(0.5)
  89. }
  90. cancelButton.snp.makeConstraints { (make) in
  91. make.top.equalTo(lineLabel.snp.bottom)
  92. make.height.equalTo(44)
  93. make.left.right.equalToSuperview()
  94. }
  95. }
  96. lazy var titleLabel: UILabel = {
  97. let titleLabel = UILabel()
  98. titleLabel.text = title
  99. titleLabel.textColor = k333333Color
  100. titleLabel.font = kRegularFont15
  101. titleLabel.textAlignment = .center
  102. return titleLabel
  103. }()
  104. lazy var detailTitleLabel: UILabel = {
  105. let detailTitleLabel = UILabel()
  106. detailTitleLabel.text = detailTitle
  107. detailTitleLabel.textColor = k333333Color
  108. detailTitleLabel.font = kRegularFont15
  109. detailTitleLabel.textAlignment = .center
  110. return detailTitleLabel
  111. }()
  112. private lazy var collectionView: UICollectionView = {
  113. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  114. collectionView.backgroundColor = UIColor.white
  115. collectionView.delegate = self;
  116. collectionView.dataSource = self;
  117. collectionView.showsVerticalScrollIndicator = false
  118. collectionView.showsHorizontalScrollIndicator = false
  119. return collectionView
  120. }()
  121. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  122. let collectionViewLayout = UICollectionViewFlowLayout.init()
  123. collectionViewLayout.minimumLineSpacing = 20
  124. collectionViewLayout.minimumInteritemSpacing = 0
  125. collectionViewLayout.scrollDirection = .horizontal
  126. return collectionViewLayout
  127. }()
  128. lazy var lineLabel: UILabel = {
  129. let lineLabel = UILabel()
  130. lineLabel.backgroundColor = kDDDDDDColor
  131. return lineLabel
  132. }()
  133. lazy var cancelButton: UIButton = {
  134. let cancelButton = UIButton(type: UIButton.ButtonType.custom)
  135. cancelButton.setTitle("取消", for: UIControl.State.normal)
  136. cancelButton.setTitleColor(k333333Color, for: UIControl.State.normal)
  137. cancelButton.titleLabel?.font = kRegularFont16
  138. return cancelButton
  139. }()
  140. func setupData() {
  141. cancelButton.rx.tap.subscribe(onNext: {
  142. [weak self] _ in
  143. self?.hide()
  144. }).disposed(by: disposeBag)
  145. }
  146. /// 分享内容
  147. class func shareCommunityView(postId:Int,uid:Int,title:String = kCommunityPostTitle,detailTitle:String = kCommunityPostDetailTitle, h5Str:String,thumbnailImg:UIImage?,sharedTitle:String,sharedDetailTitle:String,shareCommunityViewType:ShareCommunityViewType,completion: @escaping Completion) -> ShareCommunityView {
  148. let view = ShareCommunityView(
  149. postId:postId,
  150. uid:uid,
  151. title:title,
  152. detailTitle: detailTitle,
  153. h5Str: h5Str,
  154. thumbnailImg : thumbnailImg,
  155. sharedTitle: sharedTitle,
  156. sharedDetailTitle:sharedDetailTitle,
  157. shareCommunityViewType:shareCommunityViewType)
  158. view.completion = {
  159. completion()
  160. }
  161. let vProperty = FWPopupViewProperty()
  162. vProperty.popupCustomAlignment = .bottomCenter
  163. vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  164. vProperty.popupAnimationType = .frame
  165. vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
  166. vProperty.touchWildToHide = "1"
  167. view.vProperty = vProperty
  168. view.show()
  169. return view
  170. }
  171. /// 邀请好友
  172. class func inviteGoodFriends() {
  173. let shareCommunityView = ShareCommunityView.shareCommunityView(
  174. postId: 0, uid:0,
  175. title: kInviteGoodFriendsTitle,
  176. detailTitle: kInviteGoodFriendsDetailTitle,
  177. h5Str: kH5InviteUrl + "\(UserModel.shared().getModel()?.inviteCode ?? "")",
  178. thumbnailImg: UIImage.imageUrl(UserModel.shared().getModel()?.avatarurl),
  179. sharedTitle: kInviteGoodFriendsShareTitle,
  180. sharedDetailTitle: kInviteGoodFriendsShareDetailTitle,
  181. shareCommunityViewType: ShareCommunityViewType.activity,
  182. completion: {})
  183. shareCommunityView.saveCompletion = {
  184. Mediator.push(ShareRouterModuleType.pushShareActivity)
  185. }
  186. }
  187. }
  188. extension ShareCommunityView: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  189. func numberOfSections(in collectionView: UICollectionView) -> Int {
  190. return 1
  191. }
  192. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  193. if UserModel.shared().isEqualUid(uid: self.uid ?? 0) {
  194. return platformTitles.count
  195. }else {
  196. return platformTitles1.count
  197. }
  198. }
  199. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  200. let cell = ShareCommunityViewCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  201. if UserModel.shared().isEqualUid(uid: self.uid ?? 0) {
  202. cell.titleIcon(title: platformTitles[indexPath.row], iconStr: platformImages[indexPath.row], textColor: k333333Color)
  203. }else {
  204. cell.titleIcon(title: platformTitles1[indexPath.row], iconStr: platformImages[indexPath.row], textColor: k333333Color)
  205. }
  206. return cell
  207. }
  208. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  209. switch indexPath.row {
  210. case 0:
  211. UMManager.shared.shareWebPage(to: UMSocialPlatformType.wechatSession, viewController: findViewController(), title: sharedTitle ?? "", descr: sharedDetailTitle ?? "", thumbImage: thumbnailImg as Any, webpageUrl: h5Str ?? "", completion: {
  212. self.completion!()
  213. })
  214. break
  215. case 1:
  216. UMManager.shared.shareWebPage(to: UMSocialPlatformType.wechatTimeLine, viewController: findViewController(), title: sharedTitle ?? "", descr: sharedDetailTitle ?? "", thumbImage: thumbnailImg as Any, webpageUrl: h5Str ?? "",completion: {
  217. self.completion!()
  218. })
  219. break
  220. case 2:
  221. UMManager.shared.shareWebPage(to: UMSocialPlatformType.QQ, viewController: findViewController(), title: sharedTitle ?? "", descr: sharedDetailTitle ?? "", thumbImage: thumbnailImg as Any, webpageUrl: h5Str ?? "",completion: {
  222. self.completion!()
  223. })
  224. break
  225. case 3:
  226. UMManager.shared.shareWebPage(to: UMSocialPlatformType.sina, viewController: findViewController(), title: sharedTitle ?? "", descr: sharedDetailTitle ?? "", thumbImage: thumbnailImg as Any, webpageUrl: h5Str ?? "",completion: {
  227. self.completion!()
  228. })
  229. break
  230. case 4:
  231. commonCopy(string: h5Str ?? "")
  232. break
  233. case 5:
  234. switch shareCommunityViewType {
  235. case .postDetail?:
  236. if let saveCompletion = self.saveCompletion {
  237. saveCompletion()
  238. }
  239. break
  240. case .activity?:
  241. if let saveCompletion = self.saveCompletion {
  242. saveCompletion()
  243. }
  244. break
  245. default:
  246. break
  247. }
  248. break
  249. case 6:
  250. if UserModel.shared().isEqualUid(uid: self.uid ?? 0) {
  251. if let deleteCompletion = self.deleteCompletion {
  252. deleteCompletion()
  253. }
  254. }else {
  255. Mediator.push(CommunityRouterModuleType.pushReport(postId: self.postId ?? 0))
  256. }
  257. break
  258. default:
  259. break
  260. }
  261. self.hide()
  262. }
  263. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  264. return CGSize(width:50, height: 69)
  265. }
  266. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  267. return UIEdgeInsets(top: 0, left: 23, bottom: 0, right: 23)
  268. }
  269. }