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. self?.nameButton.setTitle(self?.communityPostDetailModel?.username, for: UIControl.State.normal)
  321. if self?.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
  322. self?.followButton.isHidden = true
  323. }else {
  324. self?.followButton.isHidden = false
  325. }
  326. CommunityFollowUserViewModel.setFollowType(followButton: self?.followButton, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  327. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  328. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  329. if PostType(rawValue: self?.communityPostDetailModel?.type ?? "html") == .html {
  330. self?.avatarButton.isHidden = true
  331. self?.nameButton.isHidden = true
  332. self?.followButton.isHidden = true
  333. }else {
  334. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  335. self?.followButton.isHidden = false
  336. }else {
  337. self?.followButton.isHidden = true
  338. }
  339. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth)
  340. if !(self?.communityPostDetailModel?.imgs?.isEmpty ?? true) {
  341. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  342. }else {
  343. if self?.communityPostDetailModel?.img != nil || self?.communityPostDetailModel?.img != "" {
  344. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  345. }
  346. }
  347. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  348. }
  349. self?.setupLayouts()
  350. self?.tableView.reloadData()
  351. self?.readVirueRecordAddApi()
  352. }) { [weak self] loadingStatus in
  353. if loadingStatus == .noData {
  354. DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
  355. [weak self] in
  356. self?.tabBarController?.selectedIndex = 0
  357. self?.navigationController?.popToRootViewController(animated: true)
  358. })
  359. self?.tableView.reloadData()
  360. MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
  361. if self?.tableView.mj_footer != nil {
  362. self?.tableView.isHiddenFooter(true)
  363. }
  364. }else {
  365. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  366. }
  367. }
  368. }
  369. /// 阅读virue
  370. func readVirueRecordAddApi() {
  371. var postDesc : String?
  372. if self.communityPostDetailModel?.title == nil || self.communityPostDetailModel?.title == "" {
  373. postDesc = "\(self.communityPostDetailModel?.content?.prefix(20) ?? "")"
  374. }else {
  375. postDesc = self.communityPostDetailModel?.title
  376. }
  377. 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)")
  378. }
  379. /// 评论列表
  380. ///
  381. /// - Parameters:
  382. /// - postId: 内容id
  383. /// - page: 分页
  384. func communityPostCommentApi(page:Int) {
  385. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page,completion: {
  386. [weak self] (communityPostCommentsModel) -> (Void) in
  387. self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  388. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  389. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  390. self?.communityPostCommentModels.removeAll()
  391. self?.tableView.resetNoMoreData()
  392. }
  393. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  394. self?.tableView.reloadData()
  395. }) { _ in }
  396. }
  397. /// 相关推荐
  398. ///
  399. /// - Parameter page: 分页
  400. func communityPostsApi(page:Int) {
  401. SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(topicIds:"[\(self.communityPostDetailModel?.topicIds ?? "")]", page: page, completion: {
  402. [weak self] (communityPostsModel) -> (Void) in
  403. let communityPostsModel = communityPostsModel as? CommunityPostsModel
  404. if communityPostsModel?.pagination?.currentPage == 1{
  405. self?.communityPostDataModels.removeAll()
  406. self?.tableView.resetNoMoreData()
  407. }
  408. self?.communityPostDataModels = (self?.communityPostDataModels)! + (communityPostsModel?.data!)!
  409. self?.heightList()
  410. self?.tableView.reloadData()
  411. MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityPostsModel?.pagination)
  412. }) {
  413. [weak self] (loadingStatus) in
  414. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  415. }
  416. }
  417. /// 获取高度
  418. func heightList() {
  419. heights.removeAll()
  420. if !(communityPostDataModels.isEmpty) {
  421. for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
  422. //图片高度
  423. var imageHeight : CGFloat!
  424. if index == 0 {
  425. imageHeight = (kScreenWidth - 15)/2
  426. }else {
  427. imageHeight = 240 * kScaleWidth
  428. }
  429. //label高度
  430. var labelHeight : CGFloat = 0
  431. //总间距
  432. let spacingHeght : CGFloat = 35
  433. let str = communityPostDataModel.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(13)
  434. if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
  435. let content = "\(str ?? "")"
  436. labelHeight = (content.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))
  437. }else {
  438. let str = communityPostDataModel.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "")
  439. labelHeight = (str?.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))!
  440. }
  441. //button高度
  442. let buttonHeight = 18
  443. let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
  444. heights.append(totalHeight)
  445. }
  446. }
  447. }
  448. /// 评论
  449. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  450. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  451. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  452. communityCustomCommnetModel.content = text
  453. if communityPostCommentModel != nil {
  454. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  455. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  456. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  457. }
  458. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  459. [weak self] (communityPostCommentIdModel) -> (Void) in
  460. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  461. if self?.communityPostCommentModel == nil { //评
  462. let communityPostCommentModel = CommunityPostCommentModel()
  463. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  464. communityPostCommentModel.content = text
  465. communityPostCommentModel.createdAt = "刚刚"
  466. communityPostCommentModel.id = communityPostCommentIdModel?.id
  467. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  468. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  469. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  470. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
  471. }else { //回评论
  472. let communityPostReplyModel = CommunityPostReplyModel()
  473. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  474. communityPostReplyModel.content = text
  475. communityPostReplyModel.createdAt = "刚刚"
  476. communityPostReplyModel.id = communityPostCommentIdModel?.id
  477. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  478. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  479. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  480. self?.communityPostCommentModel?.replyCount = count
  481. if self?.communityPostCommentModel?.reply == nil {
  482. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  483. }
  484. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  485. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
  486. }
  487. self?.count += 1
  488. self?.communityPostDetailModel?.commentCount = self?.count
  489. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  490. self?.tableView.reloadData()
  491. complete()
  492. }
  493. }
  494. /// 删除帖子
  495. func communityDeleteApi(postId:Int) {
  496. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  497. self?.navigationController?.popViewController(animated: true)
  498. }
  499. }
  500. }
  501. // MARK: - tableView dataSource && delegate
  502. extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
  503. func numberOfSections(in tableView: UITableView) -> Int {
  504. if communityPostDetailModel != nil {
  505. return 3
  506. }else {
  507. return 0
  508. }
  509. }
  510. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  511. switch section {
  512. case 0:
  513. return 1
  514. case 1:
  515. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  516. case 2:
  517. return 1
  518. default:
  519. return 0
  520. }
  521. }
  522. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  523. switch indexPath.section {
  524. case 0:
  525. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  526. let cell = CommunityRecommendDetailHTMLTableViewCell.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. cell.tableView = tableView
  533. return cell
  534. }else {
  535. let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
  536. cell.communityPostDetailModel = communityPostDetailModel
  537. cell.frame = tableView.bounds
  538. cell.layoutIfNeeded()
  539. cell.reloadData()
  540. cell.heightModel = heightModel1
  541. return cell
  542. }
  543. case 1:
  544. if communityPostCommentModels.isEmpty {
  545. let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  546. cell.communityPostDetailModel = communityPostDetailModel
  547. cell.commentClosure = {
  548. [weak self] in
  549. if UserModel.isTokenNil() {
  550. kAppDelegate.setLogin()
  551. }else {
  552. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  553. }
  554. }
  555. cell.userClosure = {
  556. [weak self] in
  557. if UserModel.isTokenNil() {
  558. kAppDelegate.setLogin()
  559. }else {
  560. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  561. let vc = OtherPersonalCenterViewController()
  562. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  563. self?.navigationController?.pushViewController(vc, animated: true)
  564. }
  565. }
  566. }
  567. return cell
  568. }else {
  569. let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  570. cell.communityPostDetailModel = communityPostDetailModel
  571. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  572. cell.frame = tableView.bounds
  573. cell.layoutIfNeeded()
  574. cell.reloadData()
  575. return cell
  576. }
  577. case 2:
  578. let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
  579. cell.heights = heights
  580. cell.communityPostDataModels = communityPostDataModels
  581. cell.frame = tableView.bounds
  582. cell.layoutIfNeeded()
  583. cell.reloadData()
  584. cell.heightModel = heightModel
  585. return cell
  586. default:
  587. return UITableViewCell()
  588. }
  589. }
  590. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  591. switch indexPath.section {
  592. case 0:
  593. break
  594. case 1:
  595. if communityPostCommentModels.isEmpty {
  596. break
  597. }else {
  598. if UserModel.isTokenNil() {
  599. kAppDelegate.setLogin()
  600. }else {
  601. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  602. self.indexPath = indexPath
  603. if communityPostCommentModel?.isDelete != 1 {
  604. CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
  605. [weak self] in
  606. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  607. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
  608. }, deleteClosure: {
  609. [weak self] in
  610. self?.communityPostCommentModel?.isDelete = 1
  611. self?.communityPostCommentModel?.content = "该评论已被删除"
  612. tableView.reloadData()
  613. })
  614. }else {
  615. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  616. communityPostCommentModel = nil
  617. }
  618. }
  619. break
  620. }
  621. case 2:
  622. break
  623. default:
  624. break
  625. }
  626. }
  627. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  628. switch indexPath.section {
  629. case 0:
  630. return heightModel1.height ?? 0
  631. case 1:
  632. if communityPostCommentModels.isEmpty {
  633. return 132
  634. }else {
  635. return communityPostCommentModels[indexPath.row].height ?? 0
  636. }
  637. case 2:
  638. return heightModel.height ?? 0
  639. default:
  640. return 0
  641. }
  642. }
  643. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  644. switch section {
  645. case 0:
  646. return 0.000001
  647. case 1:
  648. if communityPostCommentModels.isEmpty {
  649. return 0.000001
  650. }else {
  651. return 64
  652. }
  653. case 2:
  654. return 52
  655. default:
  656. return 0.000001
  657. }
  658. }
  659. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  660. switch section {
  661. case 0:
  662. return UIView()
  663. case 1:
  664. if communityPostCommentModels.isEmpty {
  665. return UIView()
  666. }else {
  667. let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
  668. headerView.count = count
  669. return headerView
  670. }
  671. case 2:
  672. return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
  673. default:
  674. return UIView()
  675. }
  676. }
  677. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  678. switch section {
  679. case 0:
  680. return 10
  681. case 1:
  682. if communityPostCommentModels.isEmpty {
  683. return 0.000001
  684. }else {
  685. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  686. return 0.000001
  687. }else {
  688. return 51
  689. }
  690. }
  691. case 2:
  692. return 0.000001
  693. default:
  694. return 0.000001
  695. }
  696. }
  697. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  698. switch section {
  699. case 0:
  700. return UIView()
  701. case 1:
  702. if communityPostCommentModels.isEmpty {
  703. return UIView()
  704. }else {
  705. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  706. return UIView()
  707. }else {
  708. let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
  709. footerView.unfoldClosure = {
  710. [weak self] in
  711. self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
  712. }
  713. return footerView
  714. }
  715. }
  716. case 2:
  717. return UIView()
  718. default:
  719. return UIView()
  720. }
  721. }
  722. }
  723. // MARK: - scrollView
  724. extension CommunityRecommendController {
  725. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  726. lastContentOffset = scrollView.contentOffset.y
  727. }
  728. func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
  729. if lastContentOffset < scrollView.contentOffset.y {
  730. sharedButton.isHidden = true
  731. }else{
  732. sharedButton.isHidden = false
  733. }
  734. }
  735. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  736. sharedButton.isHidden = false
  737. }
  738. }