CommunityRecommnendViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. //
  2. // CommunityRecommnendViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/6/28.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import Kingfisher
  11. import SwiftyMediator
  12. class CommunityRecommnendViewController: UIViewController {
  13. override func didReceiveMemoryWarning() {
  14. super.didReceiveMemoryWarning()
  15. //KingfisherManager.shared.cache.clearDiskCache()
  16. KingfisherManager.shared.cache.clearMemoryCache()
  17. }
  18. deinit {
  19. NXLLog("deinit")
  20. if observe != nil {
  21. NotificationCenter.default.removeObserver(observe!)
  22. }
  23. }
  24. weak var observe : NSObjectProtocol?
  25. var communityRecommendDataModels = Array<CommunityRecommendDataModel>()
  26. /// 评论需要的模型
  27. var communityRecommendDataModel : CommunityRecommendDataModel?
  28. var communityRecommendCategoryModel : CommunityRecommendCategoryModel?
  29. var navigationBarIsHidden: Bool = false
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. setupViews()
  33. setupLayouts()
  34. setupData()
  35. }
  36. func setupViews() {
  37. view.backgroundColor = kf7f8faColor
  38. view.addSubview(tableView)
  39. }
  40. func setupLayouts() {
  41. tableView.snp.makeConstraints { (make) in
  42. make.top.left.right.equalToSuperview()
  43. make.height.equalTo(kScreenHeight-44-kNavBarTotalHeight-kTabBarTotalHeight-40)
  44. }
  45. tableView.reloadData()
  46. }
  47. func setupData() {
  48. //下拉刷新
  49. tableView.addHeader(withBeginRefresh: true, animation: true) {
  50. [weak self] (page) in
  51. self?.communityPostSuggestApi(page:page)
  52. }
  53. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  54. [weak self] (page) in
  55. self?.communityPostSuggestApi(page:page)
  56. })
  57. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("login"), object: nil, queue: OperationQueue.main, using: {
  58. [weak self] (notification) in
  59. self?.tableView.mj_header.beginRefreshing()
  60. })
  61. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("loginOut"), object: nil, queue: OperationQueue.main, using: {
  62. [weak self] (notification) in
  63. self?.tableView.mj_header.beginRefreshing()
  64. })
  65. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  66. let followStatusModel = notification.object as? FollowStatusModel
  67. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  68. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  69. if communityRecommendDataModel.uid == followStatusModel?.uid {
  70. communityRecommendDataModel.isFollow = followStatusModel?.isFollowStatus
  71. }
  72. }
  73. }
  74. self?.tableView.reloadData()
  75. }
  76. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  77. let followStatusModel = notification.object as? FollowStatusModel
  78. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  79. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  80. if communityRecommendDataModel.id == followStatusModel?.postId {
  81. communityRecommendDataModel.isDislike = followStatusModel?.isFollowStatus
  82. }
  83. }
  84. }
  85. self?.tableView.reloadData()
  86. }
  87. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  88. let followStatusModel = notification.object as? FollowStatusModel
  89. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  90. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  91. if communityRecommendDataModel.id == followStatusModel?.postId {
  92. communityRecommendDataModel.isLike = followStatusModel?.isFollowStatus
  93. }
  94. }
  95. }
  96. self?.tableView.reloadData()
  97. }
  98. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  99. let followStatusModel = notification.object as? FollowStatusModel
  100. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  101. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  102. if communityRecommendDataModel.id == followStatusModel?.postId {
  103. communityRecommendDataModel.isCollect = followStatusModel?.isFollowStatus
  104. }
  105. }
  106. }
  107. self?.tableView.reloadData()
  108. }
  109. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCommnetLikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  110. let followStatusModel = notification.object as? FollowStatusModel
  111. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  112. for communityRecommendDataModel in (self?.communityRecommendDataModels)! {
  113. if communityRecommendDataModel.id == followStatusModel?.postId {
  114. if !(communityRecommendDataModel.comment?.isEmpty ?? true ){
  115. for communityRecommendCommentModel in communityRecommendDataModel.comment! {
  116. if communityRecommendCommentModel.id == followStatusModel?.commnetId {
  117. communityRecommendCommentModel.isLike = followStatusModel?.isFollowStatus
  118. communityRecommendCommentModel.likeCount = followStatusModel?.commentLikeCount
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. self?.tableView.reloadData()
  126. }
  127. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("communityDeletePostApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  128. let postId = notification.object as? Int
  129. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  130. for (index,communityRecommendDataModel) in (self?.communityRecommendDataModels)!.enumerated() {
  131. if communityRecommendDataModel.id == postId {
  132. self?.communityRecommendDataModels.remove(at: index)
  133. }
  134. }
  135. self?.tableView.reloadData()
  136. }
  137. }
  138. // observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityModuleRecommendTop"), object: nil, queue: OperationQueue.main, using: {
  139. // [weak self] (notification) in
  140. // if self?.isViewLoaded ?? true && ((self?.view.window) != nil) {
  141. // if self?.tableView.contentOffset == CGPoint(x: 0, y: 0) {
  142. // self?.tableView.mj_header.beginRefreshing()
  143. // }else {
  144. // self?.tableView.scrollToTop()
  145. // }
  146. // }
  147. // })
  148. }
  149. private lazy var tableView: UITableView = {
  150. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  151. tableView.separatorStyle = .none
  152. tableView.backgroundColor = kf7f8faColor
  153. tableView.dataSource = self
  154. tableView.delegate = self
  155. tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
  156. return tableView
  157. }()
  158. private lazy var followTableHeaderView: CommunityFollowTableHeaderView = {
  159. let followTableHeaderView = CommunityFollowTableHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 286))
  160. return followTableHeaderView
  161. }()
  162. /// 显示键盘
  163. func showKeyBoardCommentView() {
  164. KeyBoardInputView.show(inputViewResultClosure: {
  165. [weak self] (text) in
  166. self?.communityPostCommentApi(text: text, complete: {
  167. [weak self] in
  168. self?.communityRecommendDataModel = nil
  169. })
  170. }) {
  171. [weak self] in
  172. self?.communityRecommendDataModel = nil
  173. }
  174. }
  175. }
  176. extension CommunityRecommnendViewController {
  177. /// 推荐feed流
  178. ///
  179. /// - Parameter page: 分页
  180. func communityPostSuggestApi(page:Int) {
  181. SwiftMoyaNetWorkServiceCommunity.shared().communityPostSuggestApi(page:page,categoryId:communityRecommendCategoryModel?.id ?? 0,completion: {
  182. [weak self] (communityRecommendFeedModel) -> (Void) in
  183. DIYEmptyView.emptyNoDataTableView(tableView: self?.tableView)
  184. let communityRecommendFeedModel = communityRecommendFeedModel as? CommunityRecommendFeedModel
  185. if communityRecommendFeedModel?.pagination?.currentPage == 1{
  186. self?.communityRecommendDataModels.removeAll()
  187. self?.tableView.resetNoMoreData()
  188. }
  189. self?.communityRecommendDataModels = (self?.communityRecommendDataModels)! + (communityRecommendFeedModel?.data!)!
  190. self?.tableView.reloadData()
  191. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityRecommendFeedModel?.pagination)
  192. }) {
  193. [weak self] (loadingStatus) in
  194. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  195. }
  196. }
  197. /// 评论
  198. func communityPostCommentApi(text:String,complete:@escaping () -> ()) {
  199. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  200. communityCustomCommnetModel.postId = communityRecommendDataModel?.id ?? 0
  201. communityCustomCommnetModel.content = text
  202. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  203. [weak self] (communityPostCommentIdModel) -> (Void) in
  204. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  205. let communityRecommendCommentModel = CommunityRecommendCommentModel()
  206. communityRecommendCommentModel.content = text
  207. communityRecommendCommentModel.id = communityPostCommentIdModel?.id
  208. communityRecommendCommentModel.username = UserModel.shared().getModel()?.username
  209. communityRecommendCommentModel.uid = UserModel.shared().getModel()?.uid
  210. if self?.communityRecommendDataModel?.comment?.isEmpty ?? true {
  211. self?.communityRecommendDataModel?.comment = Array<CommunityRecommendCommentModel>()
  212. }
  213. self?.communityRecommendDataModel?.comment?.insert(communityRecommendCommentModel, at: 0)
  214. VirusViewModel.shared.comment(communityRecommendDataModel: self?.communityRecommendDataModel, id: communityPostCommentIdModel?.id ?? 0,content: text)
  215. let count = (1 + (self?.communityRecommendDataModel?.commentCount ?? 0))
  216. self?.communityRecommendDataModel?.commentCount = count
  217. self?.tableView.reloadData()
  218. complete()
  219. }
  220. }
  221. /// 删除帖子
  222. func communityDeleteApi(postId:Int,section:Int) {
  223. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  224. if !(self?.communityRecommendDataModels.isEmpty ?? true) {
  225. self?.communityRecommendDataModels.remove(at: section)
  226. }
  227. self?.tableView.reloadData()
  228. }
  229. }
  230. }
  231. extension CommunityRecommnendViewController : UITableViewDelegate,UITableViewDataSource {
  232. func numberOfSections(in tableView: UITableView) -> Int {
  233. return communityRecommendDataModels.isEmpty ? 0 : communityRecommendDataModels.count
  234. }
  235. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  236. let communityRecommendDataModel = communityRecommendDataModels[section]
  237. if CommunityPostSuggestType(rawValue: communityRecommendDataModel.showType ?? "") == .post {
  238. return 6
  239. }else {
  240. return 1
  241. }
  242. }
  243. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  244. let communityRecommendDataModel = communityRecommendDataModels[indexPath.section]
  245. let communityPostSuggestCType = CommunityPostSuggestType(rawValue: communityRecommendDataModel.showType ?? "")
  246. switch communityPostSuggestCType {
  247. case .banner?: //banner
  248. let cell = CommunityBannerTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  249. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  250. cell.communityRecommendTypeDataModels = communityRecommendDataModel.data
  251. }
  252. return cell
  253. case .user?: //推荐用户
  254. let cell = CommunityRecommendFollowTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  255. cell.type = .recommend
  256. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  257. cell.communityRecommendTypeDataModels = communityRecommendDataModel.data
  258. cell.followClosure1 = {
  259. [weak self] (isFollow,communityRecommendTypeDataModel) in
  260. for (_,model) in (self?.communityRecommendDataModels.enumerated())! {
  261. let communityPostSuggestCType = CommunityPostSuggestType(rawValue: model.showType ?? "")
  262. if communityPostSuggestCType == .post {
  263. if model.uid == communityRecommendTypeDataModel.uid {
  264. model.isFollow = isFollow
  265. tableView.reloadData()
  266. }
  267. }
  268. }
  269. }
  270. }
  271. return cell
  272. case .video?: //推荐视频
  273. let cell = PopularVideoTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  274. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  275. cell.communityRecommendTypeDataModels = communityRecommendDataModel.data
  276. }
  277. return cell
  278. case .topic?: //推荐话题
  279. let cell = FeaturedTopicsTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  280. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  281. cell.communityRecommendTypeDataModels = communityRecommendDataModel.data
  282. }
  283. return cell
  284. case .post?: //贴子
  285. switch indexPath.row {
  286. //贴子用户
  287. case 0:
  288. let cell = CardContentUserTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  289. cell.communityRecommendDataModel = communityRecommendDataModel
  290. cell.followClosure = {
  291. [weak self] in
  292. CommunityFollowUserViewModel.shared.follow(communityRecommendDataModel: communityRecommendDataModel, communityRecommendDataModels: (self?.communityRecommendDataModels)!, tableView: tableView)
  293. }
  294. cell.likeClosure = { y in
  295. if UserModel.shared().isEqualUid(uid: communityRecommendDataModel.uid ?? 0) {
  296. CardContentUserDeleteView.sheetDeleteView(y: y, sureClosure: { [weak self] (cardContentUserDeleteView) in
  297. self?.communityDeleteApi(postId: communityRecommendDataModel.id ?? 0, section: indexPath.section)
  298. })
  299. }else {
  300. VirusViewModel.shared.like(communityRecommendDataModel: communityRecommendDataModel, y: y, tableView: tableView)
  301. }
  302. }
  303. return cell
  304. //图片视频
  305. case 1:
  306. let cell = CardContentPicVideoTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  307. cell.type = .recommend
  308. cell.communityRecommendDataModel = communityRecommendDataModel
  309. return cell
  310. //内容标题
  311. case 2:
  312. let cell = CardContentTitleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  313. cell.communityRecommendDataModel = communityRecommendDataModel
  314. return cell
  315. //点赞,收藏,分享
  316. case 3:
  317. let cell = CardContentActionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  318. cell.communityRecommendDataModel = communityRecommendDataModel
  319. cell.praiseClosureName = { (button:UIButton) in
  320. VirusViewModel.shared.praise(communityRecommendDataModel: communityRecommendDataModel, tableView: tableView)
  321. }
  322. cell.collectClosureName = {
  323. (button:UIButton) in
  324. VirusViewModel.shared.collection(communityRecommendDataModel: communityRecommendDataModel, tableView: tableView)
  325. }
  326. return cell
  327. //评论列表
  328. case 4:
  329. let cell = CardContentCommentListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  330. cell.communityRecommendDataModel = communityRecommendDataModel
  331. return cell
  332. //评论
  333. case 5:
  334. let cell = CardContentCommnetTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  335. cell.addCommnetClosureName = {
  336. [weak self] in
  337. self?.communityRecommendDataModel = self?.communityRecommendDataModels[indexPath.section]
  338. self?.showKeyBoardCommentView()
  339. }
  340. cell.isAvatar = true
  341. return cell
  342. default:
  343. return UITableViewCell()
  344. }
  345. default:
  346. return UITableViewCell()
  347. }
  348. }
  349. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  350. let communityRecommendDataModel = communityRecommendDataModels[indexPath.section]
  351. let communityPostSuggestCType = CommunityPostSuggestType(rawValue: communityRecommendDataModel.showType ?? "")
  352. switch communityPostSuggestCType {
  353. //banner
  354. case .banner?:
  355. break
  356. //推荐用户
  357. case .user?:
  358. break
  359. //推荐视频
  360. case .video?:
  361. break
  362. //推荐话题
  363. case .topic?:
  364. break
  365. //贴子
  366. case .post?:
  367. switch indexPath.row {
  368. //贴子用户
  369. case 0:
  370. break
  371. //图片视频/内容标题/评论列表
  372. case 1,2:
  373. if PostType(rawValue: communityRecommendDataModel.type ?? "video") == .video {
  374. Mediator.push(CommunityRouterModuleType.pushPostDetailVoide(postId: "\(communityRecommendDataModel.id ?? 0)", departType: .others, topicId: 0))
  375. }else {
  376. Mediator.push(CommunityRouterModuleType.pushPostDetailContent(postId: "\(communityRecommendDataModel.id ?? 0)"))
  377. }
  378. break
  379. //点赞,收藏,分享
  380. case 3:
  381. break
  382. //评论
  383. case 5:
  384. break
  385. default:
  386. break
  387. }
  388. break
  389. default:
  390. break
  391. }
  392. }
  393. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  394. let communityRecommendDataModel = communityRecommendDataModels[indexPath.section]
  395. let communityPostSuggestCType = CommunityPostSuggestType(rawValue: communityRecommendDataModel.showType ?? "")
  396. switch communityPostSuggestCType {
  397. //banner
  398. case .banner?:
  399. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  400. return 160 * kScaleWidth
  401. }
  402. return 0
  403. //推荐用户
  404. case .user?:
  405. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  406. return 191
  407. }
  408. return 0
  409. //推荐视频
  410. case .video?:
  411. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  412. return 241
  413. }
  414. return 0
  415. //推荐话题
  416. case .topic?:
  417. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  418. return 173
  419. }
  420. return 0
  421. //贴子
  422. case .post?:
  423. switch indexPath.row {
  424. //贴子用户
  425. case 0:
  426. if communityRecommendDataModel.uid != nil {
  427. return 80
  428. }
  429. return 0
  430. //图片视频
  431. case 1:
  432. let cardContentPicVideoModel = CardContentPicVideoModel()
  433. cardContentPicVideoModel.postType = PostType(rawValue: communityRecommendDataModel.type ?? "html")
  434. let imgStr = communityRecommendDataModel.img
  435. if communityRecommendDataModel.imgs?.count ?? 0 == 0 {
  436. if communityRecommendDataModel.img != "" && communityRecommendDataModel.img != nil {
  437. cardContentPicVideoModel.number = 1
  438. cardContentPicVideoModel.width = getImageWidth(imgStr: (imgStr)!)
  439. cardContentPicVideoModel.height = getImageHeight(imgStr: (imgStr)!)
  440. return cardContentPicVideoModel.collectionViewHeight() + 15
  441. }else {
  442. return 0
  443. }
  444. }else {
  445. if communityRecommendDataModel.imgs?.count ?? 0 == 1 {
  446. if communityRecommendDataModel.img != "" || communityRecommendDataModel.img != nil {
  447. cardContentPicVideoModel.number = 1
  448. cardContentPicVideoModel.width = getImageWidth(imgStr: (imgStr)!)
  449. cardContentPicVideoModel.height = getImageHeight(imgStr: (imgStr)!)
  450. return cardContentPicVideoModel.collectionViewHeight() + 15
  451. }else {
  452. return 0
  453. }
  454. }else {
  455. cardContentPicVideoModel.number = communityRecommendDataModel.imgs?.count ?? 0
  456. return cardContentPicVideoModel.collectionViewHeight() + 15
  457. }
  458. }
  459. //内容标题
  460. case 2:
  461. var str = communityRecommendDataModel.content ?? ""
  462. str = str.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "")
  463. if (communityRecommendDataModel.title == "" || communityRecommendDataModel.title == nil) {
  464. if str.count >= 45 {
  465. str = str.prefix(45) + "...更多"
  466. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  467. return contentHeight
  468. }else {
  469. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  470. return contentHeight
  471. }
  472. }else {
  473. if PostType(rawValue: communityRecommendDataModel.type ?? "html") == .html {
  474. return 22
  475. }else {
  476. if communityRecommendDataModel.content?.count ?? 0 >= 45 {
  477. str = str.prefix(45) + "...更多"
  478. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  479. return (22 + 6 + contentHeight)
  480. }else {
  481. let contentHeight = "\(str)".heightForComment(font: kRegularFont14!, width: kScreenWidth-28)
  482. return 22 + 6 + contentHeight
  483. }
  484. }
  485. }
  486. //点赞,收藏,分享
  487. case 3:
  488. return 54
  489. //评论列表
  490. case 4:
  491. if !(communityRecommendDataModel.comment?.isEmpty ?? true) {
  492. var height : CGFloat?
  493. for communityRecommendCommentModel in communityRecommendDataModel.comment!.prefix(2) {
  494. let nameStr = "\(communityRecommendCommentModel.username ?? ""):"
  495. let contentStr = "\(communityRecommendCommentModel.content ?? "")"
  496. let likeCountStr = "\(communityRecommendCommentModel.likeCount ?? 0)"
  497. let likeWidth = likeCountStr.widthForComment(font: kRegularFont14!, height: 25.5)
  498. let str = nameStr + contentStr
  499. let strHeight = (str.heightForComment(font: kRegularFont14!, width: kScreenWidth-28-10-20-5-likeWidth) + 5)
  500. height = (height ?? 0) + strHeight
  501. }
  502. if (communityRecommendDataModel.commentCount ?? 0) <= 2 {
  503. return (height ?? 0)
  504. }else {
  505. return 23 + (height ?? 0)
  506. }
  507. }
  508. return 0
  509. //评论
  510. case 5:
  511. return 50
  512. default:
  513. return 0
  514. }
  515. default:
  516. return 0
  517. }
  518. }
  519. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  520. let communityRecommendDataModel = communityRecommendDataModels[section]
  521. let communityPostSuggestCType = CommunityPostSuggestType(rawValue: communityRecommendDataModel.showType ?? "")
  522. switch communityPostSuggestCType {
  523. //banner
  524. case .banner?:
  525. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  526. return 0.000001
  527. }
  528. return 0.000001
  529. //推荐用户
  530. case .user?:
  531. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  532. return 62
  533. }
  534. return 0.000001
  535. //推荐视频
  536. case .video?:
  537. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  538. return 62
  539. }
  540. return 0.000001
  541. //推荐话题
  542. case .topic?:
  543. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  544. return 62
  545. }
  546. return 0.000001
  547. //贴子
  548. case .post?:
  549. return 0.000001
  550. default:
  551. return 0.000001
  552. }
  553. }
  554. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  555. return 10
  556. }
  557. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  558. let communityRecommendDataModel = communityRecommendDataModels[section]
  559. let communityPostSuggestCType = CommunityPostSuggestType(rawValue: communityRecommendDataModel.showType ?? "")
  560. let headerView = CommunityCommonSectionHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 62))
  561. switch communityPostSuggestCType {
  562. //banner
  563. case .banner?:
  564. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  565. return nil
  566. }
  567. return nil
  568. //推荐用户
  569. case .user?:
  570. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  571. headerView.communityCommonSectionHeaderViewType = .recommendFollow
  572. return headerView
  573. }
  574. return nil
  575. //推荐视频
  576. case .video?:
  577. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  578. headerView.communityCommonSectionHeaderViewType = .popularVideo
  579. headerView.communityRecommendTypeDataModels = communityRecommendDataModel.data
  580. return headerView
  581. }
  582. return nil
  583. //推荐话题
  584. case .topic?:
  585. if !(communityRecommendDataModel.data?.isEmpty ?? true) {
  586. headerView.communityCommonSectionHeaderViewType = .featuredTopics
  587. return headerView
  588. }
  589. return nil
  590. //贴子
  591. case .post?:
  592. return nil
  593. default:
  594. return nil
  595. }
  596. }
  597. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  598. return nil
  599. }
  600. }
  601. extension CommunityRecommnendViewController {
  602. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  603. let point = scrollView.panGestureRecognizer.translation(in: self.view)
  604. if point.y < 0 {
  605. //向上
  606. if communityNavigationBarIsHidden == false {
  607. NotificationCenter.default.post(name: NSNotification.Name("CommunityViewControllerScrollView"), object: true)
  608. tableView.snp.updateConstraints { (make) in
  609. make.height.equalTo(kScreenHeight-44-kSafeStatusBarHeight-kTabBarTotalHeight)
  610. }
  611. communityNavigationBarIsHidden = true
  612. }
  613. } else {
  614. //向下
  615. if communityNavigationBarIsHidden {
  616. NotificationCenter.default.post(name: NSNotification.Name("CommunityViewControllerScrollView"), object: false)
  617. tableView.snp.updateConstraints { (make) in
  618. make.height.equalTo(kScreenHeight-44-kNavBarTotalHeight-kTabBarTotalHeight)
  619. }
  620. communityNavigationBarIsHidden = false
  621. }
  622. }
  623. self.view.layoutIfNeeded()
  624. }
  625. }
  626. extension CommunityRecommnendViewController : JXSegmentedListContainerViewListDelegate {
  627. func listView() -> UIView {
  628. return view
  629. }
  630. func listDidAppear() {
  631. if communityNavigationBarIsHidden {
  632. tableView.snp.updateConstraints { (make) in
  633. make.height.equalTo(kScreenHeight-44-kSafeStatusBarHeight-kTabBarTotalHeight)
  634. }
  635. }else {
  636. tableView.snp.updateConstraints { (make) in
  637. make.height.equalTo(kScreenHeight-44-kNavBarTotalHeight-kTabBarTotalHeight)
  638. }
  639. }
  640. }
  641. }