CommunityRecommendController.swift 37 KB

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