CommunityRecommendController.swift 35 KB

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