CommunityRecommendController.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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
  258. if title == nil || title == "" {
  259. title = self.communityPostDetailModel?.content
  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: kURLImage(name: self?.communityPostDetailModel?.avatar ?? ""), 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. }
  451. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
  452. [weak self] (communityPostCommentIdModel) -> (Void) in
  453. let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
  454. if self?.communityPostCommentModel == nil { //评
  455. let communityPostCommentModel = CommunityPostCommentModel()
  456. communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
  457. communityPostCommentModel.content = text
  458. communityPostCommentModel.createdAt = "刚刚"
  459. communityPostCommentModel.id = communityPostCommentIdModel?.id
  460. communityPostCommentModel.username = UserModel.shared().getModel()?.username
  461. communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
  462. self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
  463. VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
  464. }else { //回评论
  465. let communityPostReplyModel = CommunityPostReplyModel()
  466. communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
  467. communityPostReplyModel.content = text
  468. communityPostReplyModel.createdAt = "刚刚"
  469. communityPostReplyModel.id = communityPostCommentIdModel?.id
  470. communityPostReplyModel.username = UserModel.shared().getModel()?.username
  471. communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
  472. let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
  473. self?.communityPostCommentModel?.replyCount = count
  474. if self?.communityPostCommentModel?.reply == nil {
  475. self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
  476. }
  477. self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
  478. VirusViewModel.shared.commentReply(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
  479. }
  480. self?.count += 1
  481. self?.communityPostDetailModel?.commentCount = self?.count
  482. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  483. self?.tableView.reloadData()
  484. complete()
  485. }
  486. }
  487. /// 删除帖子
  488. func communityDeleteApi(postId:Int) {
  489. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  490. self?.navigationController?.popViewController(animated: true)
  491. }
  492. }
  493. }
  494. // MARK: - tableView dataSource && delegate
  495. extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
  496. func numberOfSections(in tableView: UITableView) -> Int {
  497. if communityPostDetailModel != nil {
  498. return 3
  499. }else {
  500. return 0
  501. }
  502. }
  503. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  504. switch section {
  505. case 0:
  506. return 1
  507. case 1:
  508. return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
  509. case 2:
  510. return 1
  511. default:
  512. return 0
  513. }
  514. }
  515. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  516. switch indexPath.section {
  517. case 0:
  518. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  519. let cell = CommunityRecommendDetailHTMLTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  520. cell.communityPostDetailModel = communityPostDetailModel
  521. cell.frame = tableView.bounds
  522. cell.layoutIfNeeded()
  523. cell.reloadData()
  524. cell.heightModel = heightModel1
  525. cell.tableView = tableView
  526. return cell
  527. }else {
  528. let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
  529. cell.communityPostDetailModel = communityPostDetailModel
  530. cell.frame = tableView.bounds
  531. cell.layoutIfNeeded()
  532. cell.reloadData()
  533. cell.heightModel = heightModel1
  534. return cell
  535. }
  536. case 1:
  537. if communityPostCommentModels.isEmpty {
  538. let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  539. cell.communityPostDetailModel = communityPostDetailModel
  540. cell.commentClosure = {
  541. [weak self] in
  542. if UserModel.isTokenNil() {
  543. kAppDelegate.setLogin()
  544. }else {
  545. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  546. }
  547. }
  548. cell.userClosure = {
  549. [weak self] in
  550. if UserModel.isTokenNil() {
  551. kAppDelegate.setLogin()
  552. }else {
  553. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  554. let vc = OtherPersonalCenterViewController()
  555. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  556. self?.navigationController?.pushViewController(vc, animated: true)
  557. }
  558. }
  559. }
  560. return cell
  561. }else {
  562. let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
  563. cell.communityPostDetailModel = communityPostDetailModel
  564. cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
  565. cell.frame = tableView.bounds
  566. cell.layoutIfNeeded()
  567. cell.reloadData()
  568. return cell
  569. }
  570. case 2:
  571. let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
  572. cell.heights = heights
  573. cell.communityPostDataModels = communityPostDataModels
  574. cell.frame = tableView.bounds
  575. cell.layoutIfNeeded()
  576. cell.reloadData()
  577. cell.heightModel = heightModel
  578. return cell
  579. default:
  580. return UITableViewCell()
  581. }
  582. }
  583. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  584. switch indexPath.section {
  585. case 0:
  586. break
  587. case 1:
  588. if communityPostCommentModels.isEmpty {
  589. break
  590. }else {
  591. if UserModel.isTokenNil() {
  592. kAppDelegate.setLogin()
  593. }else {
  594. communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
  595. self.indexPath = indexPath
  596. if communityPostCommentModel?.isDelete != 1 {
  597. CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
  598. [weak self] in
  599. self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
  600. self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
  601. }, deleteClosure: {
  602. [weak self] in
  603. self?.communityPostCommentModel?.isDelete = 1
  604. self?.communityPostCommentModel?.content = "该评论已被删除"
  605. self?.tableView.reloadData()
  606. })
  607. }else {
  608. SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
  609. communityPostCommentModel = nil
  610. }
  611. }
  612. break
  613. }
  614. case 2:
  615. break
  616. default:
  617. break
  618. }
  619. }
  620. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  621. switch indexPath.section {
  622. case 0:
  623. return heightModel1.height ?? 0
  624. case 1:
  625. if communityPostCommentModels.isEmpty {
  626. return 132
  627. }else {
  628. return communityPostCommentModels[indexPath.row].height ?? 0
  629. }
  630. case 2:
  631. return heightModel.height ?? 0
  632. default:
  633. return 0
  634. }
  635. }
  636. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  637. switch section {
  638. case 0:
  639. return 0.000001
  640. case 1:
  641. if communityPostCommentModels.isEmpty {
  642. return 0.000001
  643. }else {
  644. return 64
  645. }
  646. case 2:
  647. return 52
  648. default:
  649. return 0.000001
  650. }
  651. }
  652. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  653. switch section {
  654. case 0:
  655. return UIView()
  656. case 1:
  657. if communityPostCommentModels.isEmpty {
  658. return UIView()
  659. }else {
  660. let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
  661. headerView.count = count
  662. return headerView
  663. }
  664. case 2:
  665. return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
  666. default:
  667. return UIView()
  668. }
  669. }
  670. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  671. switch section {
  672. case 0:
  673. return 10
  674. case 1:
  675. if communityPostCommentModels.isEmpty {
  676. return 0.000001
  677. }else {
  678. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  679. return 0.000001
  680. }else {
  681. return 51
  682. }
  683. }
  684. case 2:
  685. return 0.000001
  686. default:
  687. return 0.000001
  688. }
  689. }
  690. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  691. switch section {
  692. case 0:
  693. return UIView()
  694. case 1:
  695. if communityPostCommentModels.isEmpty {
  696. return UIView()
  697. }else {
  698. if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
  699. return UIView()
  700. }else {
  701. let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
  702. footerView.unfoldClosure = {
  703. [weak self] in
  704. self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
  705. }
  706. return footerView
  707. }
  708. }
  709. case 2:
  710. return UIView()
  711. default:
  712. return UIView()
  713. }
  714. }
  715. }
  716. // MARK: - scrollView
  717. extension CommunityRecommendController {
  718. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  719. if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
  720. let offsetY = scrollView.contentOffset.y
  721. if (offsetY > kSafeStatusBarHeight) {
  722. let alpha = (offsetY - kSafeStatusBarHeight) / CGFloat(kNavBarTotalHeight)
  723. self.avatarButton.alpha = alpha
  724. self.nameButton.alpha = alpha
  725. if self.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
  726. self.followButton.alpha = 0
  727. }else {
  728. self.followButton.alpha = alpha
  729. }
  730. } else {
  731. self.avatarButton.alpha = 0
  732. self.nameButton.alpha = 0
  733. self.followButton.alpha = 0
  734. }
  735. }
  736. }
  737. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  738. lastContentOffset = scrollView.contentOffset.y
  739. }
  740. func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
  741. if lastContentOffset < scrollView.contentOffset.y {
  742. sharedButton.isHidden = true
  743. }else{
  744. sharedButton.isHidden = false
  745. }
  746. }
  747. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  748. sharedButton.isHidden = false
  749. }
  750. }