CommunityRecommendController.swift 35 KB

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