CommunityRecommendController.swift 33 KB

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