CommunityRecommendController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // CommunityRecommendController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/6/13.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class CommunityRecommendController: BaseViewController {
  10. deinit {
  11. if observe != nil {
  12. NotificationCenter.default.removeObserver(observe!)
  13. }
  14. }
  15. weak var observe : NSObjectProtocol?
  16. // 帖子Id
  17. var id : Int = 0
  18. var communityPostDetailModel : CommunityPostDetailModel?
  19. // 评论
  20. var communityPostCommentsModel : CommunityPostCommentsModel?
  21. var communityPostCommentModels = Array<CommunityPostCommentModel>()
  22. var communityPostCommentModel : CommunityPostCommentModel?
  23. var keyBoardCommentView : KeyBoardCommentView?
  24. // 相关推荐
  25. var communityPostDataModels = Array<CommunityPostDataModel>()
  26. // 全部评论
  27. var count : Int = 0
  28. var indexPath : IndexPath?
  29. var heights = Array<CGFloat>()
  30. /// 相关推荐高度
  31. var heightModel = HeightModel()
  32. /// 内容高度
  33. var heightModel1 = HeightModel()
  34. /// 最后滚动的位置
  35. var lastContentOffset : CGFloat = 0
  36. var emptyView : DIYEmptyView?
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. setupViews()
  40. setupData()
  41. }
  42. override func setupViews() {
  43. view.backgroundColor = kf7f8faColor
  44. navigationBar.addSubview(avatarButton)
  45. navigationBar.addSubview(nameButton)
  46. navigationBar.addSubview(followButton)
  47. view.addSubview(commentView)
  48. view.addSubview(tableView)
  49. view.addSubview(sharedButton)
  50. emptyView = DIYEmptyView.emptyActionView(withImageStr: "default_page_content", titleStr: nil, detailStr: "内容飞走了, 去看看别的吧", btnTitleStr: "去首页") {
  51. [weak self] in
  52. self?.tabBarController?.selectedIndex = 0
  53. self?.navigationController?.popToRootViewController(animated: true)
  54. }
  55. emptyView?.actionBtnHorizontalMargin = 58
  56. emptyView?.contentViewY = kScaleValue(value: 164 + kNavBarTotalHeight)
  57. emptyView?.subViewMargin = 20
  58. emptyView?.actionBtnFont = kBoldFont16
  59. emptyView?.actionBtnHeight = 32
  60. emptyView?.actionBtnCornerRadius = 32/2
  61. emptyView?.actionBtnTitleColor = kffffffColor
  62. emptyView?.actionBtnBackGroundColor = kThemeColor
  63. self.view.ly_emptyView = emptyView
  64. self.view.ly_showEmpty()
  65. }
  66. override func setupLayouts() {
  67. navigationBar.wr_setRightButton(image: kImage(name: "nav_share_black")!)
  68. avatarButton.snp.makeConstraints { (make) in
  69. make.left.equalTo(navigationBar.leftButton.snp_right)
  70. make.size.equalTo(30)
  71. make.centerY.equalTo(navigationBar.leftButton)
  72. }
  73. nameButton.snp.makeConstraints { (make) in
  74. make.centerY.equalTo(avatarButton)
  75. make.left.equalTo(avatarButton.snp_right).offset(4)
  76. }
  77. followButton.snp_makeConstraints { (make) in
  78. make.right.equalTo(navigationBar.rightButton.snp_left)
  79. make.height.equalTo(24)
  80. make.width.equalTo(60)
  81. make.centerY.equalTo(navigationBar.leftButton)
  82. }
  83. commentView.snp.makeConstraints { (make) in
  84. make.left.right.equalToSuperview()
  85. make.height.equalTo(48+kSafeTabBarHeight)
  86. make.bottom.equalToSuperview()
  87. }
  88. tableView.snp.makeConstraints { (make) in
  89. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  90. make.left.right.equalToSuperview()
  91. make.bottom.equalTo(commentView.snp_top).offset(0)
  92. }
  93. sharedButton.snp.makeConstraints { (make) in
  94. make.bottom.equalTo(commentView.snp_top).offset(-20)
  95. make.height.equalTo(71)
  96. make.width.equalTo(78)
  97. make.right.equalTo(-14)
  98. }
  99. }
  100. lazy var avatarButton: UIButton = {
  101. let avatarButton = UIButton(type: UIButton.ButtonType.custom)
  102. avatarButton.setImage(kImage(name: "default_avatar"), for: UIControl.State.normal)
  103. avatarButton.cornerRadius = 15
  104. avatarButton.masksToBounds = true
  105. return avatarButton
  106. }()
  107. lazy var nameButton: UIButton = {
  108. let nameButton = UIButton(type: UIButton.ButtonType.custom)
  109. nameButton.setTitle("昵称", for: UIControl.State.normal)
  110. nameButton.setTitleColor(k262626Color, for: UIControl.State.normal)
  111. nameButton.titleLabel?.font = kRegularFont14
  112. return nameButton
  113. }()
  114. lazy var followButton: UIButton = {
  115. let followButton = UIButton(type: UIButton.ButtonType.custom)
  116. followButton.titleLabel?.font = kMediumFont13
  117. followButton.cornerRadius = 12
  118. followButton.masksToBounds = true
  119. followButton.layer.borderWidth = 0.5
  120. followButton.isHidden = true
  121. return followButton
  122. }()
  123. lazy var tableView: UITableView = {
  124. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  125. tableView.separatorStyle = .none
  126. tableView.backgroundColor = kf7f8faColor
  127. tableView.dataSource = self
  128. tableView.delegate = self
  129. tableView.estimatedSectionFooterHeight = 0
  130. tableView.estimatedSectionHeaderHeight = 0
  131. tableView.estimatedRowHeight = 0
  132. return tableView
  133. }()
  134. lazy var communityPostDetailTableViewHeaderView: CommunityPostDetailTableViewHeaderView = {
  135. let communityPostDetailTableViewHeaderView = CommunityPostDetailTableViewHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0))
  136. return communityPostDetailTableViewHeaderView
  137. }()
  138. lazy var commentView: RecommendBottomCommentView = {
  139. let commentView = RecommendBottomCommentView()
  140. return commentView
  141. }()
  142. lazy var sharedButton: UIButton = {
  143. let sharedButton = UIButton(type: UIButton.ButtonType.custom)
  144. sharedButton.setImage(kImage(name: "share_pic_poster_floating"), for: UIControl.State.normal)
  145. return sharedButton
  146. }()
  147. override func setupData() {
  148. //头像
  149. avatarButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  150. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  151. let vc = OtherPersonalCenterViewController()
  152. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  153. self?.navigationController?.pushViewController(vc, animated: true)
  154. }
  155. }).disposed(by: disposeBag)
  156. //用户昵称
  157. nameButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  158. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  159. let vc = OtherPersonalCenterViewController()
  160. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  161. self?.navigationController?.pushViewController(vc, animated: true)
  162. }
  163. }).disposed(by: disposeBag)
  164. // 关注
  165. followButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  166. CommunityFollowUserViewModel.shared.follow(communityPostDetailModel: (self?.communityPostDetailModel)!, button: (self?.followButton)!)
  167. }).disposed(by: disposeBag)
  168. //分享
  169. navigationBar.onClickRightButton = {
  170. [weak self] in
  171. self?.share()
  172. }
  173. sharedButton.rx.tap.subscribe(onNext: { [weak self] _ in
  174. self?.share()
  175. }).disposed(by: disposeBag)
  176. tableView.addHeaderWithHeader(withBeginRefresh: true, animation: false) {
  177. [weak self] (page) in
  178. self?.communityPostDetailApi()
  179. self?.communityPostCommentApi(page: page)
  180. }
  181. tableView.addFooterWithWithHeader(withAutomaticallyRefresh: true) {
  182. [weak self] (page) in
  183. self?.communityPostsApi(page: page)
  184. }
  185. commentView.bottomClickClosure = {
  186. [weak self] (clickType) in
  187. switch clickType {
  188. case BottomClickType.typeComment:
  189. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  190. self?.hiddenKeyBoardCommentView()
  191. case BottomClickType.typeLike:
  192. VirusViewModel.shared.virueRecordAddApiPraise(communityPostDetailModel: self?.communityPostDetailModel,commentView: (self?.commentView)!)
  193. case BottomClickType.typeCollect:
  194. VirusViewModel.shared.virueRecordAddApiCollection(communityPostDetailModel: self?.communityPostDetailModel,commentView: (self?.commentView)!)
  195. }
  196. }
  197. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  198. let followStatusModel = notification.object as? FollowStatusModel
  199. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  200. self?.communityPostDetailModel?.isFollow = followStatusModel?.isFollowStatus
  201. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  202. CommunityFollowUserViewModel.shared.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  203. self?.tableView.reloadData()
  204. }
  205. }
  206. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  207. let followStatusModel = notification.object as? FollowStatusModel
  208. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  209. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  210. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  211. self?.tableView.reloadData()
  212. }
  213. }
  214. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  215. let followStatusModel = notification.object as? FollowStatusModel
  216. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  217. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  218. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  219. }
  220. if !(self?.communityPostCommentModels.isEmpty ?? true) {
  221. for communityPostDataModel in (self?.communityPostDataModels)! {
  222. if communityPostDataModel.uid == followStatusModel?.uid {
  223. communityPostDataModel.isLike = followStatusModel?.isFollowStatus
  224. }
  225. }
  226. }
  227. self?.tableView.reloadData()
  228. }
  229. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  230. let followStatusModel = notification.object as? FollowStatusModel
  231. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  232. self?.communityPostDetailModel?.isCollect = followStatusModel?.isFollowStatus
  233. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  234. self?.tableView.reloadData()
  235. }
  236. self?.tableView.reloadData()
  237. }
  238. }
  239. /// 显示键盘
  240. func showKeyBoardCommentView(placeholder:String) {
  241. keyBoardCommentView = KeyBoardCommentView.keyBoardCommentView(placeholder:placeholder,sendClosure: {
  242. [weak self] text in
  243. self?.communityPostCommentApi(text: text, complete: {
  244. self?.keyBoardCommentView?.dismisskeyBoardCommentView()
  245. })
  246. })
  247. }
  248. /// 隐藏键盘
  249. func hiddenKeyBoardCommentView() {
  250. keyBoardCommentView?.hiddenViewClosure = {
  251. [weak self] in
  252. self?.communityPostCommentModel = nil
  253. }
  254. }
  255. /// 分享
  256. func share() {
  257. var title = self.communityPostDetailModel?.title?.prefix(12)
  258. if title == nil || title == "" {
  259. title = self.communityPostDetailModel?.content?.prefix(12) ?? ""
  260. }
  261. let shareCommunityView = ShareCommunityView.shareCommunityView(
  262. uid: self.communityPostDetailModel?.uid ?? 0, title: "向好友传递美好生活,送TA 500彩虹豆",
  263. detailTitle: "越多好友看到,收获彩虹豆越多",
  264. h5Str: (self.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
  265. thumbnailImg: UIImage.imageUrl(self.communityPostDetailModel?.img),
  266. sharedTitle: String(title ?? ""),
  267. sharedDetailTitle: kCommunityPostDetailTitleShare,
  268. shareCommunityViewType: ShareCommunityViewType.postDetail,
  269. completion: {
  270. VirusViewModel.shared.forwarVirueRecordAddApi(postId: self.communityPostDetailModel?.id, postAuthorUid: "\(self.communityPostDetailModel?.uid ?? 0)", title: self.communityPostDetailModel?.title, content: self.communityPostDetailModel?.content, postType: self.communityPostDetailModel?.type, postCover: self.communityPostDetailModel?.img)
  271. })
  272. shareCommunityView.saveCompletion = {
  273. [weak self] in
  274. let vc = CommunityShareContentViewController()
  275. vc.imgUrl = self?.communityPostDetailModel?.img
  276. let communityPostDetailTopicModel = self?.communityPostDetailModel?.topic?[0]
  277. vc.topicStr = communityPostDetailTopicModel?.name ?? ""
  278. if self?.communityPostDetailModel?.title == "" || self?.communityPostDetailModel?.title == nil {
  279. vc.titleStr = "\(self?.communityPostDetailModel?.content?.prefix(20) ?? "")"
  280. }else {
  281. vc.titleStr = "\(self?.communityPostDetailModel?.title?.prefix(20) ?? "")"
  282. }
  283. vc.avatarStr = self?.communityPostDetailModel?.avatar
  284. vc.nameStr = self?.communityPostDetailModel?.username
  285. vc.H5UrlStr = (self?.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
  286. vc.bean = self?.communityPostDetailModel?.willCollectBean
  287. vc.postId = self?.communityPostDetailModel?.id
  288. vc.uid = self?.communityPostDetailModel?.uid
  289. vc.type = self?.communityPostDetailModel?.type
  290. vc.contentStr = self?.communityPostDetailModel?.content
  291. self?.navigationController?.pushViewController(vc, animated: true)
  292. }
  293. shareCommunityView.deleteCompletion = {
  294. [weak self] in
  295. self?.communityDeleteApi(postId: self?.communityPostDetailModel?.id ?? 0)
  296. }
  297. }
  298. }
  299. extension CommunityRecommendController {
  300. /// 帖子内容
  301. func communityPostDetailApi() {
  302. SwiftMoyaNetWorkServiceCommunity.shared().communityPostDetailApi(id: id) { [weak self] (communityPostDetailModel) -> (Void) in
  303. self?.view.ly_hideEmpty()
  304. self?.communityPostDetailModel = communityPostDetailModel as? CommunityPostDetailModel
  305. self?.communityPostsApi(page:1)
  306. self?.avatarButton.kf.setImage(with: kURLImage(name: self?.communityPostDetailModel?.avatar ?? ""), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
  307. self?.nameButton.setTitle(self?.communityPostDetailModel?.username, for: UIControl.State.normal)
  308. if self?.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
  309. self?.followButton.isHidden = true
  310. }else {
  311. self?.followButton.isHidden = false
  312. }
  313. CommunityFollowUserViewModel.shared.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  314. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  315. if PostType(rawValue: self?.communityPostDetailModel?.type ?? "html") == .html {
  316. self?.avatarButton.isHidden = true
  317. self?.nameButton.isHidden = true
  318. self?.followButton.isHidden = true
  319. }else {
  320. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  321. self?.followButton.isHidden = false
  322. }else {
  323. self?.followButton.isHidden = true
  324. }
  325. if !(self?.communityPostDetailModel?.imgs?.isEmpty ?? true) {
  326. let height = getImageHeight(imgStr: (self?.communityPostDetailModel?.imgs?[0])!)
  327. let width = getImageWidth(imgStr: (self?.communityPostDetailModel?.imgs?[0])!)
  328. let scaleHeight = kScreenWidth*height/width
  329. if scaleHeight >= (500 * kScaleWidth) {
  330. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 500 * kScaleWidth)
  331. }else {
  332. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth*height/width)
  333. }
  334. self?.communityPostDetailTableViewHeaderView.tableView = self?.tableView
  335. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  336. }else {
  337. if self?.communityPostDetailModel?.img != nil || self?.communityPostDetailModel?.img != "" {
  338. let height = getImageHeight(imgStr: (self?.communityPostDetailModel?.img)!)
  339. let width = getImageWidth(imgStr: (self?.communityPostDetailModel?.img)!)
  340. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth*height/width)
  341. self?.communityPostDetailTableViewHeaderView.tableView = self?.tableView
  342. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  343. }
  344. }
  345. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  346. }
  347. self?.setupLayouts()
  348. self?.tableView.reloadData()
  349. self?.readVirueRecordAddApi()
  350. }
  351. }
  352. /// 阅读virue
  353. func readVirueRecordAddApi() {
  354. var postDesc : String?
  355. if self.communityPostDetailModel?.title == nil || self.communityPostDetailModel?.title == "" {
  356. postDesc = "\(self.communityPostDetailModel?.content?.prefix(20) ?? "")"
  357. }else {
  358. postDesc = self.communityPostDetailModel?.title
  359. }
  360. VirusViewModel.shared.readVirueRecordAddApi(postId: self.communityPostDetailModel?.id, postAuthorUid: "\(self.communityPostDetailModel?.uid ?? 0)", postDesc: postDesc, postType: self.communityPostDetailModel?.type, postCover: self.communityPostDetailModel?.img, actionId: "\(self.communityPostDetailModel?.id ?? 0)")
  361. }
  362. /// 评论列表
  363. ///
  364. /// - Parameters:
  365. /// - postId: 内容id
  366. /// - page: 分页
  367. func communityPostCommentApi(page:Int) {
  368. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page) {
  369. [weak self] (communityPostCommentsModel) -> (Void) in
  370. self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  371. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  372. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  373. self?.communityPostCommentModels.removeAll()
  374. }
  375. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  376. self?.tableView.reloadData()
  377. }
  378. }
  379. /// 相关推荐
  380. ///
  381. /// - Parameter page: 分页
  382. func communityPostsApi(page:Int) {
  383. SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(topicIds:"[\(self.communityPostDetailModel?.topicIds ?? "")]", page: page) {
  384. [weak self] (communityPostsModel) -> (Void) in
  385. let communityPostsModel = communityPostsModel as? CommunityPostsModel
  386. if communityPostsModel?.pagination?.currentPage ?? 1 <= communityPostsModel?.pagination?.totalPages ?? 1 {
  387. if communityPostsModel?.pagination?.currentPage == 1{
  388. self?.communityPostDataModels.removeAll()
  389. }
  390. self?.communityPostDataModels = (self?.communityPostDataModels)! + (communityPostsModel?.data!)!
  391. self?.heightList()
  392. self?.tableView.reloadData()
  393. if self?.communityPostDataModels.count ?? 0 >= communityPostsModel?.pagination?.total ?? 0 {
  394. self?.tableView.endFooterNoMoreData()
  395. }
  396. }else {
  397. self?.tableView.endFooterNoMoreData()
  398. }
  399. }
  400. }
  401. /// 获取高度
  402. func heightList() {
  403. heights.removeAll()
  404. if !(communityPostDataModels.isEmpty) {
  405. for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
  406. //图片高度
  407. var imageHeight : CGFloat!
  408. if index == 0 {
  409. imageHeight = (kScreenWidth - 15)/2
  410. }else {
  411. imageHeight = 240 * kScaleWidth
  412. }
  413. //label高度
  414. var labelHeight : CGFloat = 0
  415. //总间距
  416. let spacingHeght : CGFloat!
  417. if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
  418. labelHeight = 0
  419. spacingHeght = 25
  420. }else {
  421. labelHeight = (communityPostDataModel.title?.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))!
  422. spacingHeght = 35
  423. }
  424. //button高度
  425. let buttonHeight = 18
  426. let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
  427. heights.append(totalHeight)
  428. }
  429. }
  430. }
  431. /// 评论
  432. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  433. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  434. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  435. communityCustomCommnetModel.content = text
  436. if communityPostCommentModel != nil {
  437. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  438. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  439. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  440. }
  441. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  442. [weak self] (communityPostCommentIdModel) -> (Void) in
  443. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  444. if self?.communityPostCommentModel == nil { //评
  445. let communityPostCommentModel = CommunityPostCommentModel()
  446. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  447. communityPostCommentModel.content = text
  448. communityPostCommentModel.createdAt = "刚刚"
  449. communityPostCommentModel.id = communityPostCommentIdModel?.id
  450. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  451. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  452. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  453. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
  454. }else { //回评论
  455. let communityPostReplyModel = CommunityPostReplyModel()
  456. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  457. communityPostReplyModel.content = text
  458. communityPostReplyModel.createdAt = "刚刚"
  459. communityPostReplyModel.id = communityPostCommentIdModel?.id
  460. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  461. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  462. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  463. self?.communityPostCommentModel?.replyCount = count
  464. if self?.communityPostCommentModel?.reply == nil {
  465. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  466. }
  467. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  468. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
  469. }
  470. self?.count += 1
  471. self?.communityPostDetailModel?.commentCount = self?.count
  472. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  473. self?.tableView.reloadData()
  474. complete()
  475. }
  476. }
  477. /// 删除帖子
  478. func communityDeleteApi(postId:Int) {
  479. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  480. self?.navigationController?.popViewController(animated: true)
  481. }
  482. }
  483. }
  484. // MARK: - tableView dataSource && delegate
  485. extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
  486. func numberOfSections(in tableView: UITableView) -> Int {
  487. return 3
  488. }
  489. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  490. switch section {
  491. case 0:
  492. if communityPostDetailModel != nil {
  493. return 1
  494. }else {
  495. return 0
  496. }
  497. case 1:
  498. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  499. case 2:
  500. return 1
  501. default:
  502. return 0
  503. }
  504. }
  505. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  506. switch indexPath.section {
  507. case 0:
  508. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  509. let cell = CommunityRecommendDetailHTMLTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  510. cell.communityPostDetailModel = communityPostDetailModel
  511. cell.frame = tableView.bounds
  512. cell.layoutIfNeeded()
  513. cell.reloadData()
  514. cell.heightModel = heightModel1
  515. cell.tableView = tableView
  516. return cell
  517. }else {
  518. let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
  519. cell.communityPostDetailModel = communityPostDetailModel
  520. cell.frame = tableView.bounds
  521. cell.layoutIfNeeded()
  522. cell.reloadData()
  523. cell.heightModel = heightModel1
  524. return cell
  525. }
  526. case 1:
  527. if communityPostCommentModels.isEmpty {
  528. let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  529. cell.communityPostDetailModel = communityPostDetailModel
  530. cell.commentClosure = {
  531. [weak self] in
  532. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  533. self?.hiddenKeyBoardCommentView()
  534. }
  535. cell.userClosure = {
  536. [weak self] in
  537. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  538. let vc = OtherPersonalCenterViewController()
  539. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  540. self?.navigationController?.pushViewController(vc, animated: true)
  541. }
  542. }
  543. return cell
  544. }else {
  545. let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  546. cell.communityPostDetailModel = communityPostDetailModel
  547. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  548. cell.frame = tableView.bounds
  549. cell.layoutIfNeeded()
  550. cell.reloadData()
  551. return cell
  552. }
  553. case 2:
  554. let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
  555. cell.heights = heights
  556. cell.communityPostDataModels = communityPostDataModels
  557. cell.frame = tableView.bounds
  558. cell.layoutIfNeeded()
  559. cell.reloadData()
  560. cell.heightModel = heightModel
  561. return cell
  562. default:
  563. return UITableViewCell()
  564. }
  565. }
  566. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  567. switch indexPath.section {
  568. case 0:
  569. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  570. }else {
  571. }
  572. case 1:
  573. if communityPostCommentModels.isEmpty {
  574. break
  575. }else {
  576. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  577. self.indexPath = indexPath
  578. AlertSheetView.sheetCommentReplyView(userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "") {
  579. [weak self] in
  580. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  581. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
  582. self?.hiddenKeyBoardCommentView()
  583. }
  584. break
  585. }
  586. case 2:
  587. break
  588. default:
  589. break
  590. }
  591. }
  592. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  593. switch indexPath.section {
  594. case 0:
  595. return heightModel1.height ?? 0
  596. case 1:
  597. if communityPostCommentModels.isEmpty {
  598. return 132
  599. }else {
  600. return communityPostCommentModels[indexPath.row].height ?? 0
  601. }
  602. case 2:
  603. return heightModel.height ?? 0
  604. default:
  605. return 0
  606. }
  607. }
  608. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  609. switch section {
  610. case 0:
  611. return 0.000001
  612. case 1:
  613. if communityPostCommentModels.isEmpty {
  614. return 0.000001
  615. }else {
  616. return 64
  617. }
  618. case 2:
  619. return 52
  620. default:
  621. return 0.000001
  622. }
  623. }
  624. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  625. switch section {
  626. case 0:
  627. return UIView()
  628. case 1:
  629. if communityPostCommentModels.isEmpty {
  630. return UIView()
  631. }else {
  632. let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
  633. headerView.count = count
  634. return headerView
  635. }
  636. case 2:
  637. return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
  638. default:
  639. return UIView()
  640. }
  641. }
  642. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  643. switch section {
  644. case 0:
  645. return 10
  646. case 1:
  647. if communityPostCommentModels.isEmpty {
  648. return 0.000001
  649. }else {
  650. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  651. return 0.000001
  652. }else {
  653. return 51
  654. }
  655. }
  656. case 2:
  657. return 0.000001
  658. default:
  659. return 0.000001
  660. }
  661. }
  662. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  663. switch section {
  664. case 0:
  665. return UIView()
  666. case 1:
  667. if communityPostCommentModels.isEmpty {
  668. return UIView()
  669. }else {
  670. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  671. return UIView()
  672. }else {
  673. let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
  674. footerView.unfoldClosure = {
  675. [weak self] in
  676. self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
  677. }
  678. return footerView
  679. }
  680. }
  681. case 2:
  682. return UIView()
  683. default:
  684. return UIView()
  685. }
  686. }
  687. }
  688. // MARK: - scrollView
  689. extension CommunityRecommendController {
  690. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  691. lastContentOffset = scrollView.contentOffset.y
  692. }
  693. func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
  694. if lastContentOffset < scrollView.contentOffset.y {
  695. sharedButton.isHidden = true
  696. }else{
  697. sharedButton.isHidden = false
  698. }
  699. }
  700. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  701. sharedButton.isHidden = false
  702. }
  703. }