CommunityRecommendController.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. self?.communityPostsApi(page: page)
  166. }
  167. tableView.addFooterWithWithHeader(withAutomaticallyRefresh: true) {
  168. [weak self] (page) in
  169. self?.communityPostsApi(page: page)
  170. }
  171. commentView.bottomClickClosure = {
  172. [weak self] (clickType) in
  173. switch clickType {
  174. case BottomClickType.typeComment:
  175. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  176. self?.hiddenKeyBoardCommentView()
  177. case BottomClickType.typeLike:
  178. VirusViewModel.shared.virueRecordAddApiPraise(communityPostDetailModel: self?.communityPostDetailModel,commentView: (self?.commentView)!)
  179. case BottomClickType.typeCollect:
  180. VirusViewModel.shared.virueRecordAddApiCollection(communityPostDetailModel: self?.communityPostDetailModel,commentView: (self?.commentView)!)
  181. }
  182. }
  183. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  184. let followStatusModel = notification.object as? FollowStatusModel
  185. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  186. self?.communityPostDetailModel?.isFollow = followStatusModel?.isFollowStatus
  187. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  188. CommunityFollowUserViewModel.shared.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  189. self?.tableView.reloadData()
  190. }
  191. }
  192. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  193. let followStatusModel = notification.object as? FollowStatusModel
  194. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  195. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  196. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  197. self?.tableView.reloadData()
  198. }
  199. }
  200. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  201. let followStatusModel = notification.object as? FollowStatusModel
  202. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  203. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  204. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  205. }
  206. if !(self?.communityPostCommentModels.isEmpty ?? true) {
  207. for communityPostDataModel in (self?.communityPostDataModels)! {
  208. if communityPostDataModel.uid == followStatusModel?.uid {
  209. communityPostDataModel.isLike = followStatusModel?.isFollowStatus
  210. }
  211. }
  212. }
  213. self?.tableView.reloadData()
  214. }
  215. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  216. let followStatusModel = notification.object as? FollowStatusModel
  217. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  218. self?.communityPostDetailModel?.isCollect = followStatusModel?.isFollowStatus
  219. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  220. self?.tableView.reloadData()
  221. }
  222. self?.tableView.reloadData()
  223. }
  224. }
  225. /// 显示键盘
  226. func showKeyBoardCommentView(placeholder:String) {
  227. keyBoardCommentView = KeyBoardCommentView.keyBoardCommentView(placeholder:placeholder,sendClosure: {
  228. [weak self] text in
  229. self?.communityPostCommentApi(text: text, complete: {
  230. self?.keyBoardCommentView?.dismisskeyBoardCommentView()
  231. })
  232. })
  233. }
  234. /// 隐藏键盘
  235. func hiddenKeyBoardCommentView() {
  236. keyBoardCommentView?.hiddenViewClosure = {
  237. [weak self] in
  238. self?.communityPostCommentModel = nil
  239. }
  240. }
  241. /// 分享
  242. func share() {
  243. var title = self.communityPostDetailModel?.title?.prefix(12)
  244. if title == nil || title == "" {
  245. title = self.communityPostDetailModel?.content?.prefix(12) ?? ""
  246. }
  247. let shareCommunityView = ShareCommunityView.shareCommunityView(
  248. title: "向好友传递美好生活,送TA 500彩虹豆",
  249. detailTitle: "越多好友看到,收获彩虹豆越多",
  250. h5Str: (self.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
  251. thumbnailImg: UIImage.imageUrl(self.communityPostDetailModel?.img),
  252. sharedTitle: String(title ?? ""),
  253. sharedDetailTitle: kCommunityPostDetailTitleShare,
  254. shareCommunityViewType: ShareCommunityViewType.postDetail,
  255. completion: {
  256. 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)
  257. })
  258. shareCommunityView.saveCompletion = {
  259. [weak self] in
  260. let vc = CommunityShareContentViewController()
  261. vc.imgUrl = self?.communityPostDetailModel?.img
  262. let communityPostDetailTopicModel = self?.communityPostDetailModel?.topic?[0]
  263. vc.topicStr = communityPostDetailTopicModel?.name ?? ""
  264. if self?.communityPostDetailModel?.title == "" || self?.communityPostDetailModel?.title == nil {
  265. vc.titleStr =
  266. String(self?.communityPostDetailModel?.content?.prefix(20) ?? "")
  267. }else {
  268. vc.titleStr = String(self?.communityPostDetailModel?.title?.prefix(20) ?? "")
  269. }
  270. vc.avatarStr = self?.communityPostDetailModel?.avatar
  271. vc.nameStr = self?.communityPostDetailModel?.username
  272. vc.H5UrlStr = (self?.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
  273. vc.bean = self?.communityPostDetailModel?.willCollectBean
  274. vc.postId = self?.communityPostDetailModel?.id
  275. vc.uid = self?.communityPostDetailModel?.uid
  276. vc.type = self?.communityPostDetailModel?.type
  277. vc.contentStr = self?.communityPostDetailModel?.content
  278. self?.navigationController?.pushViewController(vc, animated: true)
  279. }
  280. }
  281. }
  282. extension CommunityRecommendController {
  283. /// 帖子内容
  284. func communityPostDetailApi() {
  285. SwiftMoyaNetWorkServiceCommunity.shared().communityPostDetailApi(id: id) { [weak self] (communityPostDetailModel) -> (Void) in
  286. self?.communityPostDetailModel = communityPostDetailModel as? CommunityPostDetailModel
  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. VirusViewModel.shared.read(communityPostDetailModel: (self?.communityPostDetailModel)!)
  325. }
  326. }
  327. /// 评论列表
  328. ///
  329. /// - Parameters:
  330. /// - postId: 内容id
  331. /// - page: 分页
  332. func communityPostCommentApi(page:Int) {
  333. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page) {
  334. [weak self] (communityPostCommentsModel) -> (Void) in
  335. self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  336. self?.count = self?.communityPostCommentsModel?.pagination?.total ?? 0
  337. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  338. self?.communityPostCommentModels.removeAll()
  339. }
  340. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  341. self?.tableView.reloadData()
  342. }
  343. }
  344. /// 相关推荐
  345. ///
  346. /// - Parameter page: 分页
  347. func communityPostsApi(page:Int) {
  348. SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(page: page) {
  349. [weak self] (communityPostsModel) -> (Void) in
  350. let communityPostsModel = communityPostsModel as? CommunityPostsModel
  351. if communityPostsModel?.pagination?.currentPage ?? 1 <= communityPostsModel?.pagination?.totalPages ?? 1 {
  352. if communityPostsModel?.pagination?.currentPage == 1{
  353. self?.communityPostDataModels.removeAll()
  354. }
  355. self?.communityPostDataModels = (self?.communityPostDataModels)! + (communityPostsModel?.data!)!
  356. self?.heightList()
  357. self?.tableView.reloadData()
  358. if self?.communityPostDataModels.count ?? 0 >= communityPostsModel?.pagination?.total ?? 0 {
  359. self?.tableView.endFooterNoMoreData()
  360. }
  361. }else {
  362. self?.tableView.endFooterNoMoreData()
  363. }
  364. }
  365. }
  366. /// 获取高度
  367. func heightList() {
  368. heights.removeAll()
  369. if !(communityPostDataModels.isEmpty) {
  370. for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
  371. //图片高度
  372. var imageHeight : CGFloat!
  373. if index == 0 {
  374. imageHeight = (kScreenWidth - 15)/2
  375. }else {
  376. imageHeight = 240 * kScaleWidth
  377. }
  378. //label高度
  379. var labelHeight : CGFloat = 0
  380. //总间距
  381. let spacingHeght : CGFloat!
  382. if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
  383. labelHeight = 0
  384. spacingHeght = 25
  385. }else {
  386. labelHeight = (communityPostDataModel.title?.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))!
  387. spacingHeght = 35
  388. }
  389. //button高度
  390. let buttonHeight = 18
  391. let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
  392. heights.append(totalHeight)
  393. }
  394. }
  395. }
  396. /// 评论
  397. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  398. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  399. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  400. communityCustomCommnetModel.content = text
  401. if communityPostCommentModel != nil {
  402. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  403. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  404. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  405. }
  406. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  407. [weak self] (communityPostCommentIdModel) -> (Void) in
  408. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  409. if self?.communityPostCommentModel == nil { //评
  410. let communityPostCommentModel = CommunityPostCommentModel()
  411. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  412. communityPostCommentModel.content = text
  413. communityPostCommentModel.createdAt = "刚刚"
  414. communityPostCommentModel.id = communityPostCommentIdModel?.id
  415. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  416. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  417. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  418. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0)
  419. self?.count += 1
  420. self?.communityPostDetailModel?.commentCount = self?.count
  421. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  422. self?.tableView.reloadData()
  423. }else { //回评论
  424. let communityPostReplyModel = CommunityPostReplyModel()
  425. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  426. communityPostReplyModel.content = text
  427. communityPostReplyModel.createdAt = "刚刚"
  428. communityPostReplyModel.id = communityPostCommentIdModel?.id
  429. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  430. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  431. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  432. self?.communityPostCommentModel?.replyCount = count
  433. if self?.communityPostCommentModel?.reply == nil {
  434. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  435. }
  436. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  437. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0, communityPostCommentModel: self?.communityPostCommentModel)
  438. self?.tableView.reloadData()
  439. }
  440. complete()
  441. }
  442. }
  443. }
  444. // MARK: - tableView dataSource && delegate
  445. extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
  446. func numberOfSections(in tableView: UITableView) -> Int {
  447. return 3
  448. }
  449. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  450. switch section {
  451. case 0:
  452. if communityPostDetailModel != nil {
  453. return 1
  454. }else {
  455. return 0
  456. }
  457. case 1:
  458. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  459. case 2:
  460. return 1
  461. default:
  462. return 0
  463. }
  464. }
  465. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  466. switch indexPath.section {
  467. case 0:
  468. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  469. let cell = CommunityRecommendDetailHTMLTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  470. cell.communityPostDetailModel = communityPostDetailModel
  471. cell.frame = tableView.bounds
  472. cell.layoutIfNeeded()
  473. cell.reloadData()
  474. cell.heightModel = heightModel1
  475. cell.tableView = tableView
  476. return cell
  477. }else {
  478. let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
  479. cell.communityPostDetailModel = communityPostDetailModel
  480. cell.frame = tableView.bounds
  481. cell.layoutIfNeeded()
  482. cell.reloadData()
  483. cell.heightModel = heightModel1
  484. return cell
  485. }
  486. case 1:
  487. if communityPostCommentModels.isEmpty {
  488. let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  489. cell.communityPostDetailModel = communityPostDetailModel
  490. cell.commentClosure = {
  491. [weak self] in
  492. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  493. self?.hiddenKeyBoardCommentView()
  494. }
  495. cell.userClosure = {
  496. [weak self] in
  497. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  498. let vc = OtherPersonalCenterViewController()
  499. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  500. self?.navigationController?.pushViewController(vc, animated: true)
  501. }
  502. }
  503. return cell
  504. }else {
  505. let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  506. cell.communityPostDetailModel = communityPostDetailModel
  507. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  508. cell.frame = tableView.bounds
  509. cell.layoutIfNeeded()
  510. cell.reloadData()
  511. return cell
  512. }
  513. case 2:
  514. let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
  515. cell.heights = heights
  516. cell.communityPostDataModels = communityPostDataModels
  517. cell.frame = tableView.bounds
  518. cell.layoutIfNeeded()
  519. cell.reloadData()
  520. cell.heightModel = heightModel
  521. return cell
  522. default:
  523. return UITableViewCell()
  524. }
  525. }
  526. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  527. switch indexPath.section {
  528. case 0:
  529. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  530. }else {
  531. }
  532. case 1:
  533. if communityPostCommentModels.isEmpty {
  534. break
  535. }else {
  536. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  537. self.indexPath = indexPath
  538. AlertSheetView.sheetCommentReplyView(userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "") {
  539. [weak self] in
  540. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  541. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
  542. self?.hiddenKeyBoardCommentView()
  543. }
  544. break
  545. }
  546. case 2:
  547. break
  548. default:
  549. break
  550. }
  551. }
  552. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  553. switch indexPath.section {
  554. case 0:
  555. return heightModel1.height ?? 0
  556. case 1:
  557. if communityPostCommentModels.isEmpty {
  558. return 132
  559. }else {
  560. return communityPostCommentModels[indexPath.row].height ?? 0
  561. }
  562. case 2:
  563. return heightModel.height ?? 0
  564. default:
  565. return 0
  566. }
  567. }
  568. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  569. switch section {
  570. case 0:
  571. return 0.000001
  572. case 1:
  573. if communityPostCommentModels.isEmpty {
  574. return 0.000001
  575. }else {
  576. return 64
  577. }
  578. case 2:
  579. return 52
  580. default:
  581. return 0.000001
  582. }
  583. }
  584. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  585. switch section {
  586. case 0:
  587. return UIView()
  588. case 1:
  589. if communityPostCommentModels.isEmpty {
  590. return UIView()
  591. }else {
  592. let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
  593. headerView.count = count
  594. return headerView
  595. }
  596. case 2:
  597. return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
  598. default:
  599. return UIView()
  600. }
  601. }
  602. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  603. switch section {
  604. case 0:
  605. return 10
  606. case 1:
  607. if communityPostCommentModels.isEmpty {
  608. return 0.000001
  609. }else {
  610. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  611. return 0.000001
  612. }else {
  613. return 51
  614. }
  615. }
  616. case 2:
  617. return 0.000001
  618. default:
  619. return 0.000001
  620. }
  621. }
  622. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  623. switch section {
  624. case 0:
  625. return UIView()
  626. case 1:
  627. if communityPostCommentModels.isEmpty {
  628. return UIView()
  629. }else {
  630. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  631. return UIView()
  632. }else {
  633. let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
  634. footerView.unfoldClosure = {
  635. [weak self] in
  636. self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
  637. }
  638. return footerView
  639. }
  640. }
  641. case 2:
  642. return UIView()
  643. default:
  644. return UIView()
  645. }
  646. }
  647. }
  648. // MARK: - scrollView
  649. extension CommunityRecommendController {
  650. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  651. lastContentOffset = scrollView.contentOffset.y
  652. }
  653. func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
  654. if lastContentOffset < scrollView.contentOffset.y {
  655. sharedButton.isHidden = true
  656. }else{
  657. sharedButton.isHidden = false
  658. }
  659. }
  660. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  661. sharedButton.isHidden = false
  662. }
  663. }