CommunityRecommendController.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. }
  14. deinit {
  15. NXLLog("deinit")
  16. self.view = nil
  17. if observe != nil {
  18. NotificationCenter.default.removeObserver(observe!)
  19. }
  20. }
  21. weak var observe : NSObjectProtocol?
  22. // 帖子Id
  23. var id : Int = 0
  24. var communityPostDetailModel : CommunityPostDetailModel?
  25. // 评论
  26. var communityPostCommentsModel : CommunityPostCommentsModel?
  27. var communityPostCommentModels = Array<CommunityPostCommentModel>()
  28. var communityPostCommentModel : CommunityPostCommentModel?
  29. // 相关推荐
  30. var communityPostDataModels = Array<CommunityPostDataModel>()
  31. // 全部评论
  32. var count : Int = 0
  33. var indexPath : IndexPath?
  34. var heights = Array<CGFloat>()
  35. /// 相关推荐高度
  36. var heightModel = HeightModel()
  37. /// 内容高度
  38. var heightModel1 = HeightModel()
  39. /// 最后滚动的位置
  40. var lastContentOffset : CGFloat = 0
  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. 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?.showKeyBoardCommentView(placeholder: "添加评论...")
  197. case BottomClickType.typeLike:
  198. VirusViewModel.shared.virueRecordAddApiPraise(communityPostDetailModel: self?.communityPostDetailModel,commentView: self?.commentView)
  199. case BottomClickType.typeCollect:
  200. VirusViewModel.shared.virueRecordAddApiCollection(communityPostDetailModel: self?.communityPostDetailModel,commentView: self?.commentView)
  201. }
  202. }
  203. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  204. let followStatusModel = notification.object as? FollowStatusModel
  205. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  206. self?.communityPostDetailModel?.isFollow = followStatusModel?.isFollowStatus
  207. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  208. CommunityFollowUserViewModel.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  209. self?.tableView.reloadData()
  210. }
  211. }
  212. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  213. let followStatusModel = notification.object as? FollowStatusModel
  214. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  215. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  216. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  217. self?.tableView.reloadData()
  218. }
  219. }
  220. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  221. let followStatusModel = notification.object as? FollowStatusModel
  222. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  223. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  224. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  225. }
  226. if !(self?.communityPostCommentModels.isEmpty ?? true) {
  227. for communityPostDataModel in (self?.communityPostDataModels)! {
  228. if communityPostDataModel.uid == followStatusModel?.uid {
  229. communityPostDataModel.isLike = followStatusModel?.isFollowStatus
  230. }
  231. }
  232. }
  233. self?.tableView.reloadData()
  234. }
  235. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  236. let followStatusModel = notification.object as? FollowStatusModel
  237. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  238. self?.communityPostDetailModel?.isCollect = followStatusModel?.isFollowStatus
  239. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  240. self?.tableView.reloadData()
  241. }
  242. self?.tableView.reloadData()
  243. }
  244. }
  245. /// 显示键盘
  246. func showKeyBoardCommentView(placeholder:String) {
  247. KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
  248. [weak self] text in
  249. self?.communityPostCommentApi(text: text, complete: {
  250. [weak self] in
  251. self?.communityPostCommentModel = nil
  252. })
  253. }) {
  254. [weak self] in
  255. self?.communityPostCommentModel = nil
  256. }
  257. }
  258. /// 分享
  259. func share() {
  260. var title = self.communityPostDetailModel?.title?.prefix(12)
  261. if title == nil || title == "" {
  262. title = self.communityPostDetailModel?.content?.prefix(12) ?? ""
  263. }
  264. let shareCommunityView = ShareCommunityView.shareCommunityView(
  265. uid: self.communityPostDetailModel?.uid ?? 0,
  266. h5Str: (self.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
  267. thumbnailImg: UIImage.imageUrl(self.communityPostDetailModel?.img),
  268. sharedTitle: String(title ?? ""),
  269. sharedDetailTitle: kCommunityPostShareDetailTitle,
  270. shareCommunityViewType: ShareCommunityViewType.postDetail,
  271. completion: {
  272. [weak self] in
  273. 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)
  274. })
  275. shareCommunityView.saveCompletion = {
  276. [weak self] in
  277. let vc = CommunityShareContentViewController()
  278. vc.imgUrl = self?.communityPostDetailModel?.img
  279. let communityPostDetailTopicModel = self?.communityPostDetailModel?.topic?[0]
  280. vc.topicStr = communityPostDetailTopicModel?.name ?? ""
  281. if self?.communityPostDetailModel?.title == "" || self?.communityPostDetailModel?.title == nil {
  282. vc.titleStr = "\(self?.communityPostDetailModel?.content?.prefix(20) ?? "")"
  283. }else {
  284. vc.titleStr = "\(self?.communityPostDetailModel?.title?.prefix(20) ?? "")"
  285. }
  286. vc.avatarStr = self?.communityPostDetailModel?.avatar
  287. vc.nameStr = self?.communityPostDetailModel?.username
  288. vc.H5UrlStr = (self?.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
  289. vc.bean = self?.communityPostDetailModel?.willCollectBean
  290. vc.postId = self?.communityPostDetailModel?.id
  291. vc.uid = self?.communityPostDetailModel?.uid
  292. vc.type = self?.communityPostDetailModel?.type
  293. vc.contentStr = self?.communityPostDetailModel?.content
  294. self?.navigationController?.pushViewController(vc, animated: true)
  295. }
  296. shareCommunityView.deleteCompletion = {
  297. [weak self] in
  298. self?.communityDeleteApi(postId: self?.communityPostDetailModel?.id ?? 0)
  299. }
  300. }
  301. }
  302. extension CommunityRecommendController {
  303. /// 帖子内容
  304. func communityPostDetailApi() {
  305. SwiftMoyaNetWorkServiceCommunity.shared().communityPostDetailApi(id: id, completion: {
  306. [weak self] (communityPostDetailModel) -> (Void) in
  307. self?.tableView.snp.remakeConstraints {[weak self] (make) in
  308. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  309. make.left.right.equalToSuperview()
  310. make.bottom.equalTo((self?.commentView.snp_top)!).offset(0)
  311. }
  312. DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
  313. [weak self] in
  314. self?.tabBarController?.selectedIndex = 0
  315. self?.navigationController?.popToRootViewController(animated: true)
  316. })
  317. self?.communityPostDetailModel = communityPostDetailModel as? CommunityPostDetailModel
  318. self?.communityPostsApi(page:1)
  319. self?.avatarButton.kf.setImage(with: kURLThumbnailsImage(name: self?.communityPostDetailModel?.avatar ?? "", size: kSize30x30Image), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
  320. //ImageCache.default.clearMemoryCache()
  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. if loadingStatus == .noData {
  355. DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
  356. [weak self] in
  357. self?.tabBarController?.selectedIndex = 0
  358. self?.navigationController?.popToRootViewController(animated: true)
  359. })
  360. self?.tableView.reloadData()
  361. MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
  362. if self?.tableView.mj_footer != nil {
  363. self?.tableView.isHiddenFooter(true)
  364. }
  365. }else {
  366. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  367. }
  368. }
  369. }
  370. /// 阅读virue
  371. func readVirueRecordAddApi() {
  372. var postDesc : String?
  373. if self.communityPostDetailModel?.title == nil || self.communityPostDetailModel?.title == "" {
  374. postDesc = "\(self.communityPostDetailModel?.content?.prefix(20) ?? "")"
  375. }else {
  376. postDesc = self.communityPostDetailModel?.title
  377. }
  378. 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)")
  379. }
  380. /// 评论列表
  381. ///
  382. /// - Parameters:
  383. /// - postId: 内容id
  384. /// - page: 分页
  385. func communityPostCommentApi(page:Int) {
  386. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page,completion: {
  387. [weak self] (communityPostCommentsModel) -> (Void) in
  388. self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  389. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  390. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  391. self?.communityPostCommentModels.removeAll()
  392. self?.tableView.resetNoMoreData()
  393. }
  394. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  395. self?.tableView.reloadData()
  396. }) { _ in }
  397. }
  398. /// 相关推荐
  399. ///
  400. /// - Parameter page: 分页
  401. func communityPostsApi(page:Int) {
  402. SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(topicIds:"[\(self.communityPostDetailModel?.topicIds ?? "")]", page: page, completion: {
  403. [weak self] (communityPostsModel) -> (Void) in
  404. let communityPostsModel = communityPostsModel as? CommunityPostsModel
  405. if communityPostsModel?.pagination?.currentPage == 1{
  406. self?.communityPostDataModels.removeAll()
  407. self?.tableView.resetNoMoreData()
  408. }
  409. self?.communityPostDataModels = (self?.communityPostDataModels)! + (communityPostsModel?.data!)!
  410. self?.heightList()
  411. self?.tableView.reloadData()
  412. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityPostsModel?.pagination)
  413. }) {
  414. [weak self] (loadingStatus) in
  415. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  416. }
  417. }
  418. /// 获取高度
  419. func heightList() {
  420. heights.removeAll()
  421. if !(communityPostDataModels.isEmpty) {
  422. for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
  423. //图片高度
  424. var imageHeight : CGFloat!
  425. if index == 0 {
  426. imageHeight = (kScreenWidth - 15)/2
  427. }else {
  428. imageHeight = 240 * kScaleWidth
  429. }
  430. //label高度
  431. var labelHeight : CGFloat = 0
  432. //总间距
  433. let spacingHeght : CGFloat = 35
  434. let str = communityPostDataModel.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(13)
  435. if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
  436. let content = "\(str ?? "")"
  437. labelHeight = (content.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))
  438. }else {
  439. let str = communityPostDataModel.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "")
  440. labelHeight = (str?.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))!
  441. }
  442. //button高度
  443. let buttonHeight = 18
  444. let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
  445. heights.append(totalHeight)
  446. }
  447. }
  448. }
  449. /// 评论
  450. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  451. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  452. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  453. communityCustomCommnetModel.content = text
  454. if communityPostCommentModel != nil {
  455. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  456. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  457. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  458. }
  459. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  460. [weak self] (communityPostCommentIdModel) -> (Void) in
  461. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  462. if self?.communityPostCommentModel == nil { //评
  463. let communityPostCommentModel = CommunityPostCommentModel()
  464. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  465. communityPostCommentModel.content = text
  466. communityPostCommentModel.createdAt = "刚刚"
  467. communityPostCommentModel.id = communityPostCommentIdModel?.id
  468. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  469. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  470. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  471. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
  472. }else { //回评论
  473. let communityPostReplyModel = CommunityPostReplyModel()
  474. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  475. communityPostReplyModel.content = text
  476. communityPostReplyModel.createdAt = "刚刚"
  477. communityPostReplyModel.id = communityPostCommentIdModel?.id
  478. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  479. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  480. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  481. self?.communityPostCommentModel?.replyCount = count
  482. if self?.communityPostCommentModel?.reply == nil {
  483. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  484. }
  485. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  486. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
  487. }
  488. self?.count += 1
  489. self?.communityPostDetailModel?.commentCount = self?.count
  490. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  491. self?.tableView.reloadData()
  492. complete()
  493. }
  494. }
  495. /// 删除帖子
  496. func communityDeleteApi(postId:Int) {
  497. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  498. self?.navigationController?.popViewController(animated: true)
  499. }
  500. }
  501. }
  502. // MARK: - tableView dataSource && delegate
  503. extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
  504. func numberOfSections(in tableView: UITableView) -> Int {
  505. if communityPostDetailModel != nil {
  506. return 3
  507. }else {
  508. return 0
  509. }
  510. }
  511. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  512. switch section {
  513. case 0:
  514. return 1
  515. case 1:
  516. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  517. case 2:
  518. return 1
  519. default:
  520. return 0
  521. }
  522. }
  523. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  524. switch indexPath.section {
  525. case 0:
  526. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  527. let cell = CommunityRecommendDetailHTMLTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  528. cell.communityPostDetailModel = communityPostDetailModel
  529. cell.frame = tableView.bounds
  530. cell.layoutIfNeeded()
  531. cell.reloadData()
  532. cell.heightModel = heightModel1
  533. cell.tableView = tableView
  534. return cell
  535. }else {
  536. let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
  537. cell.communityPostDetailModel = communityPostDetailModel
  538. cell.frame = tableView.bounds
  539. cell.layoutIfNeeded()
  540. cell.reloadData()
  541. cell.heightModel = heightModel1
  542. return cell
  543. }
  544. case 1:
  545. if communityPostCommentModels.isEmpty {
  546. let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  547. cell.communityPostDetailModel = communityPostDetailModel
  548. cell.commentClosure = {
  549. [weak self] in
  550. if UserModel.isTokenNil() {
  551. kAppDelegate.setLogin()
  552. }else {
  553. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  554. }
  555. }
  556. cell.userClosure = {
  557. [weak self] in
  558. if UserModel.isTokenNil() {
  559. kAppDelegate.setLogin()
  560. }else {
  561. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  562. let vc = OtherPersonalCenterViewController()
  563. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  564. self?.navigationController?.pushViewController(vc, animated: true)
  565. }
  566. }
  567. }
  568. return cell
  569. }else {
  570. let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  571. cell.communityPostDetailModel = communityPostDetailModel
  572. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  573. cell.frame = tableView.bounds
  574. cell.layoutIfNeeded()
  575. cell.reloadData()
  576. return cell
  577. }
  578. case 2:
  579. let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
  580. cell.heights = heights
  581. cell.communityPostDataModels = communityPostDataModels
  582. cell.frame = tableView.bounds
  583. cell.layoutIfNeeded()
  584. cell.reloadData()
  585. cell.heightModel = heightModel
  586. return cell
  587. default:
  588. return UITableViewCell()
  589. }
  590. }
  591. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  592. switch indexPath.section {
  593. case 0:
  594. break
  595. case 1:
  596. if communityPostCommentModels.isEmpty {
  597. break
  598. }else {
  599. if UserModel.isTokenNil() {
  600. kAppDelegate.setLogin()
  601. }else {
  602. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  603. self.indexPath = indexPath
  604. if communityPostCommentModel?.isDelete != 1 {
  605. CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
  606. [weak self] in
  607. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  608. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
  609. }, deleteClosure: {
  610. [weak self] in
  611. self?.communityPostCommentModel?.isDelete = 1
  612. self?.communityPostCommentModel?.content = "该评论已被删除"
  613. tableView.reloadData()
  614. })
  615. }else {
  616. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  617. communityPostCommentModel = nil
  618. }
  619. }
  620. break
  621. }
  622. case 2:
  623. break
  624. default:
  625. break
  626. }
  627. }
  628. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  629. switch indexPath.section {
  630. case 0:
  631. return heightModel1.height ?? 0
  632. case 1:
  633. if communityPostCommentModels.isEmpty {
  634. return 132
  635. }else {
  636. return communityPostCommentModels[indexPath.row].height ?? 0
  637. }
  638. case 2:
  639. return heightModel.height ?? 0
  640. default:
  641. return 0
  642. }
  643. }
  644. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  645. switch section {
  646. case 0:
  647. return 0.000001
  648. case 1:
  649. if communityPostCommentModels.isEmpty {
  650. return 0.000001
  651. }else {
  652. return 64
  653. }
  654. case 2:
  655. return 52
  656. default:
  657. return 0.000001
  658. }
  659. }
  660. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  661. switch section {
  662. case 0:
  663. return UIView()
  664. case 1:
  665. if communityPostCommentModels.isEmpty {
  666. return UIView()
  667. }else {
  668. let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
  669. headerView.count = count
  670. return headerView
  671. }
  672. case 2:
  673. return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
  674. default:
  675. return UIView()
  676. }
  677. }
  678. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  679. switch section {
  680. case 0:
  681. return 10
  682. case 1:
  683. if communityPostCommentModels.isEmpty {
  684. return 0.000001
  685. }else {
  686. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  687. return 0.000001
  688. }else {
  689. return 51
  690. }
  691. }
  692. case 2:
  693. return 0.000001
  694. default:
  695. return 0.000001
  696. }
  697. }
  698. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  699. switch section {
  700. case 0:
  701. return UIView()
  702. case 1:
  703. if communityPostCommentModels.isEmpty {
  704. return UIView()
  705. }else {
  706. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  707. return UIView()
  708. }else {
  709. let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
  710. footerView.unfoldClosure = {
  711. [weak self] in
  712. self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
  713. }
  714. return footerView
  715. }
  716. }
  717. case 2:
  718. return UIView()
  719. default:
  720. return UIView()
  721. }
  722. }
  723. }
  724. // MARK: - scrollView
  725. extension CommunityRecommendController {
  726. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  727. lastContentOffset = scrollView.contentOffset.y
  728. }
  729. func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
  730. if lastContentOffset < scrollView.contentOffset.y {
  731. sharedButton.isHidden = true
  732. }else{
  733. sharedButton.isHidden = false
  734. }
  735. }
  736. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  737. sharedButton.isHidden = false
  738. }
  739. }