CommunityRecommendController.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. class CommunityRecommendController: BaseViewController {
  10. deinit {
  11. NXLLog("deinit")
  12. self.view = nil
  13. if observe != nil {
  14. NotificationCenter.default.removeObserver(observe!)
  15. }
  16. }
  17. weak var observe : NSObjectProtocol?
  18. // 帖子Id
  19. var id : Int = 0
  20. var communityPostDetailModel : CommunityPostDetailModel?
  21. // 评论
  22. var communityPostCommentsModel : CommunityPostCommentsModel?
  23. var communityPostCommentModels = Array<CommunityPostCommentModel>()
  24. var communityPostCommentModel : CommunityPostCommentModel?
  25. // 相关推荐
  26. var communityPostDataModels = Array<CommunityPostDataModel>()
  27. // 全部评论
  28. var count : Int = 0
  29. var indexPath : IndexPath?
  30. var heights = Array<CGFloat>()
  31. /// 相关推荐高度
  32. var heightModel = HeightModel()
  33. /// 内容高度
  34. var heightModel1 = HeightModel()
  35. /// 最后滚动的位置
  36. var lastContentOffset : CGFloat = 0
  37. var emptyView : DIYEmptyView?
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. setupViews()
  41. setupData()
  42. }
  43. override func setupViews() {
  44. view.backgroundColor = kf7f8faColor
  45. navigationBar.addSubview(avatarButton)
  46. navigationBar.addSubview(nameButton)
  47. navigationBar.addSubview(followButton)
  48. view.addSubview(commentView)
  49. view.addSubview(tableView)
  50. view.addSubview(sharedButton)
  51. tableView.snp.makeConstraints { (make) in
  52. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  53. make.left.right.equalToSuperview()
  54. make.bottom.equalTo(commentView.snp_top).offset(0)
  55. }
  56. }
  57. override func setupLayouts() {
  58. navigationBar.wr_setRightButton(image: kImage(name: "nav_share_black")!)
  59. avatarButton.snp.makeConstraints { (make) in
  60. make.left.equalTo(navigationBar.leftButton.snp_right)
  61. make.size.equalTo(30)
  62. make.centerY.equalTo(navigationBar.leftButton)
  63. }
  64. nameButton.snp.makeConstraints { (make) in
  65. make.centerY.equalTo(avatarButton)
  66. make.left.equalTo(avatarButton.snp_right).offset(4)
  67. }
  68. followButton.snp_makeConstraints { (make) in
  69. make.right.equalTo(navigationBar.rightButton.snp_left)
  70. make.height.equalTo(24)
  71. make.width.equalTo(60)
  72. make.centerY.equalTo(navigationBar.leftButton)
  73. }
  74. commentView.snp.makeConstraints { (make) in
  75. make.left.right.equalToSuperview()
  76. make.height.equalTo(48+kSafeTabBarHeight)
  77. make.bottom.equalToSuperview()
  78. }
  79. sharedButton.snp.makeConstraints { (make) in
  80. make.bottom.equalTo(commentView.snp_top).offset(-20)
  81. make.height.equalTo(71)
  82. make.width.equalTo(78)
  83. make.right.equalTo(-14)
  84. }
  85. }
  86. lazy var avatarButton: UIButton = {
  87. let avatarButton = UIButton(type: UIButton.ButtonType.custom)
  88. avatarButton.setImage(kImage(name: "default_avatar"), for: UIControl.State.normal)
  89. avatarButton.cornerRadius = 15
  90. avatarButton.masksToBounds = true
  91. return avatarButton
  92. }()
  93. lazy var nameButton: UIButton = {
  94. let nameButton = UIButton(type: UIButton.ButtonType.custom)
  95. nameButton.setTitle("昵称", for: UIControl.State.normal)
  96. nameButton.setTitleColor(k262626Color, for: UIControl.State.normal)
  97. nameButton.titleLabel?.font = kRegularFont14
  98. return nameButton
  99. }()
  100. lazy var followButton: UIButton = {
  101. let followButton = UIButton(type: UIButton.ButtonType.custom)
  102. followButton.titleLabel?.font = kMediumFont13
  103. followButton.cornerRadius = 12
  104. followButton.masksToBounds = true
  105. followButton.layer.borderWidth = 0.5
  106. followButton.isHidden = true
  107. return followButton
  108. }()
  109. lazy var tableView: UITableView = {
  110. [unowned self] in
  111. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  112. tableView.separatorStyle = .none
  113. tableView.backgroundColor = kf7f8faColor
  114. tableView.dataSource = self
  115. tableView.delegate = self
  116. tableView.estimatedSectionFooterHeight = 0
  117. tableView.estimatedSectionHeaderHeight = 0
  118. tableView.estimatedRowHeight = 0
  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(kImage(name: "share_pic_poster_floating"), 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.shared.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, title: "向好友传递美好生活,送TA 500U米",
  266. detailTitle: "越多好友看到,收获U米越多",
  267. h5Str: (self.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
  268. thumbnailImg: UIImage.imageUrl(self.communityPostDetailModel?.img),
  269. sharedTitle: String(title ?? ""),
  270. sharedDetailTitle: kCommunityPostDetailTitleShare,
  271. shareCommunityViewType: ShareCommunityViewType.postDetail,
  272. completion: {
  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: { [weak self] (communityPostDetailModel) -> (Void) in
  306. DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
  307. self?.tabBarController?.selectedIndex = 0
  308. self?.navigationController?.popToRootViewController(animated: true)
  309. })
  310. self?.communityPostDetailModel = communityPostDetailModel as? CommunityPostDetailModel
  311. self?.communityPostsApi(page:1)
  312. self?.avatarButton.kf.setImage(with: kURLImage(name: self?.communityPostDetailModel?.avatar ?? ""), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
  313. self?.nameButton.setTitle(self?.communityPostDetailModel?.username, for: UIControl.State.normal)
  314. if self?.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
  315. self?.followButton.isHidden = true
  316. }else {
  317. self?.followButton.isHidden = false
  318. }
  319. CommunityFollowUserViewModel.shared.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  320. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  321. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  322. if PostType(rawValue: self?.communityPostDetailModel?.type ?? "html") == .html {
  323. self?.avatarButton.isHidden = true
  324. self?.nameButton.isHidden = true
  325. self?.followButton.isHidden = true
  326. }else {
  327. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  328. self?.followButton.isHidden = false
  329. }else {
  330. self?.followButton.isHidden = true
  331. }
  332. if !(self?.communityPostDetailModel?.imgs?.isEmpty ?? true) {
  333. let height = getImageHeight(imgStr: (self?.communityPostDetailModel?.imgs?[0])!)
  334. let width = getImageWidth(imgStr: (self?.communityPostDetailModel?.imgs?[0])!)
  335. let scaleHeight = kScreenWidth*height/width
  336. if scaleHeight >= (500 * kScaleWidth) {
  337. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 500 * kScaleWidth)
  338. }else {
  339. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth*height/width)
  340. }
  341. self?.communityPostDetailTableViewHeaderView.tableView = self?.tableView
  342. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  343. }else {
  344. if self?.communityPostDetailModel?.img != nil || self?.communityPostDetailModel?.img != "" {
  345. let height = getImageHeight(imgStr: (self?.communityPostDetailModel?.img)!)
  346. let width = getImageWidth(imgStr: (self?.communityPostDetailModel?.img)!)
  347. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth*height/width)
  348. self?.communityPostDetailTableViewHeaderView.tableView = self?.tableView
  349. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  350. }
  351. }
  352. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  353. }
  354. self?.setupLayouts()
  355. self?.tableView.reloadData()
  356. self?.readVirueRecordAddApi()
  357. }) { [weak self] loadingStatus in
  358. MJRefreshManager.hiddenHeaderWithFooter(tableView: self?.tableView)
  359. }
  360. }
  361. /// 阅读virue
  362. func readVirueRecordAddApi() {
  363. var postDesc : String?
  364. if self.communityPostDetailModel?.title == nil || self.communityPostDetailModel?.title == "" {
  365. postDesc = "\(self.communityPostDetailModel?.content?.prefix(20) ?? "")"
  366. }else {
  367. postDesc = self.communityPostDetailModel?.title
  368. }
  369. 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)")
  370. }
  371. /// 评论列表
  372. ///
  373. /// - Parameters:
  374. /// - postId: 内容id
  375. /// - page: 分页
  376. func communityPostCommentApi(page:Int) {
  377. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page,completion: {
  378. [weak self] (communityPostCommentsModel) -> (Void) in
  379. self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  380. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  381. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  382. self?.communityPostCommentModels.removeAll()
  383. self?.tableView.resetNoMoreData()
  384. }
  385. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  386. self?.tableView.reloadData()
  387. }) { _ in }
  388. }
  389. /// 相关推荐
  390. ///
  391. /// - Parameter page: 分页
  392. func communityPostsApi(page:Int) {
  393. SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(topicIds:"[\(self.communityPostDetailModel?.topicIds ?? "")]", page: page, completion: {
  394. [weak self] (communityPostsModel) -> (Void) in
  395. let communityPostsModel = communityPostsModel as? CommunityPostsModel
  396. if communityPostsModel?.pagination?.currentPage ?? 1 <= communityPostsModel?.pagination?.totalPages ?? 1 {
  397. if communityPostsModel?.pagination?.currentPage == 1{
  398. self?.communityPostDataModels.removeAll()
  399. self?.tableView.resetNoMoreData()
  400. }
  401. self?.communityPostDataModels = (self?.communityPostDataModels)! + (communityPostsModel?.data!)!
  402. self?.heightList()
  403. self?.tableView.reloadData()
  404. MJRefreshManager.hiddenHeaderWithFooter(tableView: self?.tableView, currentPage: communityPostsModel?.pagination?.currentPage, totalPages: communityPostsModel?.pagination?.totalPages)
  405. }else {
  406. MJRefreshManager.hiddenFooterNoMoreData(tableView: self?.tableView)
  407. }
  408. }) {
  409. [weak self] (loadingStatus) in
  410. MJRefreshManager.hiddenHeaderWithFooterNONetWork(tableView: self?.tableView)
  411. }
  412. }
  413. /// 获取高度
  414. func heightList() {
  415. heights.removeAll()
  416. if !(communityPostDataModels.isEmpty) {
  417. for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
  418. //图片高度
  419. var imageHeight : CGFloat!
  420. if index == 0 {
  421. imageHeight = (kScreenWidth - 15)/2
  422. }else {
  423. imageHeight = 240 * kScaleWidth
  424. }
  425. //label高度
  426. var labelHeight : CGFloat = 0
  427. //总间距
  428. let spacingHeght : CGFloat = 35
  429. let str = communityPostDataModel.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(13)
  430. if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
  431. let content = "\(str ?? "")"
  432. labelHeight = (content.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))
  433. }else {
  434. let str = communityPostDataModel.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "")
  435. labelHeight = (str?.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))!
  436. }
  437. //button高度
  438. let buttonHeight = 18
  439. let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
  440. heights.append(totalHeight)
  441. }
  442. }
  443. }
  444. /// 评论
  445. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  446. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  447. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  448. communityCustomCommnetModel.content = text
  449. if communityPostCommentModel != nil {
  450. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  451. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  452. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  453. }
  454. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  455. [weak self] (communityPostCommentIdModel) -> (Void) in
  456. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  457. if self?.communityPostCommentModel == nil { //评
  458. let communityPostCommentModel = CommunityPostCommentModel()
  459. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  460. communityPostCommentModel.content = text
  461. communityPostCommentModel.createdAt = "刚刚"
  462. communityPostCommentModel.id = communityPostCommentIdModel?.id
  463. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  464. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  465. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  466. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
  467. }else { //回评论
  468. let communityPostReplyModel = CommunityPostReplyModel()
  469. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  470. communityPostReplyModel.content = text
  471. communityPostReplyModel.createdAt = "刚刚"
  472. communityPostReplyModel.id = communityPostCommentIdModel?.id
  473. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  474. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  475. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  476. self?.communityPostCommentModel?.replyCount = count
  477. if self?.communityPostCommentModel?.reply == nil {
  478. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  479. }
  480. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  481. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
  482. }
  483. self?.count += 1
  484. self?.communityPostDetailModel?.commentCount = self?.count
  485. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  486. self?.tableView.reloadData()
  487. complete()
  488. }
  489. }
  490. /// 删除帖子
  491. func communityDeleteApi(postId:Int) {
  492. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  493. self?.navigationController?.popViewController(animated: true)
  494. }
  495. }
  496. }
  497. // MARK: - tableView dataSource && delegate
  498. extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
  499. func numberOfSections(in tableView: UITableView) -> Int {
  500. if communityPostDetailModel != nil {
  501. return 3
  502. }else {
  503. return 0
  504. }
  505. }
  506. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  507. switch section {
  508. case 0:
  509. return 1
  510. case 1:
  511. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  512. case 2:
  513. return 1
  514. default:
  515. return 0
  516. }
  517. }
  518. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  519. switch indexPath.section {
  520. case 0:
  521. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  522. let cell = CommunityRecommendDetailHTMLTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  523. cell.communityPostDetailModel = communityPostDetailModel
  524. cell.frame = tableView.bounds
  525. cell.layoutIfNeeded()
  526. cell.reloadData()
  527. cell.heightModel = heightModel1
  528. cell.tableView = tableView
  529. return cell
  530. }else {
  531. let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
  532. cell.communityPostDetailModel = communityPostDetailModel
  533. cell.frame = tableView.bounds
  534. cell.layoutIfNeeded()
  535. cell.reloadData()
  536. cell.heightModel = heightModel1
  537. return cell
  538. }
  539. case 1:
  540. if communityPostCommentModels.isEmpty {
  541. let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  542. cell.communityPostDetailModel = communityPostDetailModel
  543. cell.commentClosure = {
  544. [weak self] in
  545. if UserModel.isTokenNil() {
  546. kAppDelegate.setLogin()
  547. }else {
  548. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  549. }
  550. }
  551. cell.userClosure = {
  552. [weak self] in
  553. if UserModel.isTokenNil() {
  554. kAppDelegate.setLogin()
  555. }else {
  556. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  557. let vc = OtherPersonalCenterViewController()
  558. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  559. self?.navigationController?.pushViewController(vc, animated: true)
  560. }
  561. }
  562. }
  563. return cell
  564. }else {
  565. let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  566. cell.communityPostDetailModel = communityPostDetailModel
  567. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  568. cell.frame = tableView.bounds
  569. cell.layoutIfNeeded()
  570. cell.reloadData()
  571. return cell
  572. }
  573. case 2:
  574. let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
  575. cell.heights = heights
  576. cell.communityPostDataModels = communityPostDataModels
  577. cell.frame = tableView.bounds
  578. cell.layoutIfNeeded()
  579. cell.reloadData()
  580. cell.heightModel = heightModel
  581. return cell
  582. default:
  583. return UITableViewCell()
  584. }
  585. }
  586. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  587. switch indexPath.section {
  588. case 0:
  589. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  590. }else {
  591. }
  592. case 1:
  593. if communityPostCommentModels.isEmpty {
  594. break
  595. }else {
  596. if UserModel.isTokenNil() {
  597. kAppDelegate.setLogin()
  598. }else {
  599. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  600. self.indexPath = indexPath
  601. if communityPostCommentModel?.isDelete != 1 {
  602. CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
  603. [weak self] in
  604. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  605. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
  606. }, deleteClosure: {
  607. [weak self] in
  608. self?.communityPostCommentModel?.isDelete = 1
  609. self?.communityPostCommentModel?.content = "该评论已被删除"
  610. tableView.reloadData()
  611. })
  612. }else {
  613. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  614. communityPostCommentModel = nil
  615. }
  616. }
  617. break
  618. }
  619. case 2:
  620. break
  621. default:
  622. break
  623. }
  624. }
  625. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  626. switch indexPath.section {
  627. case 0:
  628. return heightModel1.height ?? 0
  629. case 1:
  630. if communityPostCommentModels.isEmpty {
  631. return 132
  632. }else {
  633. return communityPostCommentModels[indexPath.row].height ?? 0
  634. }
  635. case 2:
  636. return heightModel.height ?? 0
  637. default:
  638. return 0
  639. }
  640. }
  641. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  642. switch section {
  643. case 0:
  644. return 0.000001
  645. case 1:
  646. if communityPostCommentModels.isEmpty {
  647. return 0.000001
  648. }else {
  649. return 64
  650. }
  651. case 2:
  652. return 52
  653. default:
  654. return 0.000001
  655. }
  656. }
  657. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  658. switch section {
  659. case 0:
  660. return UIView()
  661. case 1:
  662. if communityPostCommentModels.isEmpty {
  663. return UIView()
  664. }else {
  665. let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
  666. headerView.count = count
  667. return headerView
  668. }
  669. case 2:
  670. return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
  671. default:
  672. return UIView()
  673. }
  674. }
  675. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  676. switch section {
  677. case 0:
  678. return 10
  679. case 1:
  680. if communityPostCommentModels.isEmpty {
  681. return 0.000001
  682. }else {
  683. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  684. return 0.000001
  685. }else {
  686. return 51
  687. }
  688. }
  689. case 2:
  690. return 0.000001
  691. default:
  692. return 0.000001
  693. }
  694. }
  695. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  696. switch section {
  697. case 0:
  698. return UIView()
  699. case 1:
  700. if communityPostCommentModels.isEmpty {
  701. return UIView()
  702. }else {
  703. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  704. return UIView()
  705. }else {
  706. let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
  707. footerView.unfoldClosure = {
  708. [weak self] in
  709. self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
  710. }
  711. return footerView
  712. }
  713. }
  714. case 2:
  715. return UIView()
  716. default:
  717. return UIView()
  718. }
  719. }
  720. }
  721. // MARK: - scrollView
  722. extension CommunityRecommendController {
  723. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  724. lastContentOffset = scrollView.contentOffset.y
  725. }
  726. func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
  727. if lastContentOffset < scrollView.contentOffset.y {
  728. sharedButton.isHidden = true
  729. }else{
  730. sharedButton.isHidden = false
  731. }
  732. }
  733. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  734. sharedButton.isHidden = false
  735. }
  736. }