CommunityRecommendController.swift 36 KB

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