CommunityRecommendController.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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.layer.borderWidth = 0.5
  109. followButton.alpha = 0
  110. return followButton
  111. }()
  112. lazy var tableView: UITableView = {
  113. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  114. tableView.separatorStyle = .none
  115. tableView.backgroundColor = kf7f8faColor
  116. tableView.dataSource = self
  117. tableView.delegate = self
  118. return tableView
  119. }()
  120. lazy var communityPostDetailTableViewHeaderView: CommunityPostDetailTableViewHeaderView = {
  121. let communityPostDetailTableViewHeaderView = CommunityPostDetailTableViewHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0))
  122. return communityPostDetailTableViewHeaderView
  123. }()
  124. lazy var commentView: RecommendBottomCommentView = {
  125. let commentView = RecommendBottomCommentView()
  126. return commentView
  127. }()
  128. lazy var sharedButton: UIButton = {
  129. let sharedButton = UIButton(type: UIButton.ButtonType.custom)
  130. sharedButton.setImage(UIImage.gif(name: "share_iSpt"), for: UIControl.State.normal)
  131. return sharedButton
  132. }()
  133. override func setupData() {
  134. //头像
  135. avatarButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  136. if UserModel.isTokenNil() {
  137. kAppDelegate.setLogin()
  138. }else {
  139. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  140. let vc = OtherPersonalCenterViewController()
  141. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  142. self?.navigationController?.pushViewController(vc, animated: true)
  143. }
  144. }
  145. }).disposed(by: disposeBag)
  146. //用户昵称
  147. nameButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  148. if UserModel.isTokenNil() {
  149. kAppDelegate.setLogin()
  150. }else {
  151. if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
  152. let vc = OtherPersonalCenterViewController()
  153. vc.uid = self?.communityPostDetailModel?.uid ?? 0
  154. self?.navigationController?.pushViewController(vc, animated: true)
  155. }
  156. }
  157. }).disposed(by: disposeBag)
  158. // 关注
  159. followButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  160. if UserModel.isTokenNil() {
  161. kAppDelegate.setLogin()
  162. }else {
  163. CommunityFollowUserViewModel.shared.follow(communityPostDetailModel: (self?.communityPostDetailModel)!, button: (self?.followButton)!)
  164. }
  165. }).disposed(by: disposeBag)
  166. //分享
  167. navigationBar.onClickRightButton = {
  168. [weak self] in
  169. if UserModel.isTokenNil() {
  170. kAppDelegate.setLogin()
  171. }else {
  172. self?.share()
  173. }
  174. }
  175. sharedButton.rx.tap.subscribe(onNext: { [weak self] _ in
  176. if UserModel.isTokenNil() {
  177. kAppDelegate.setLogin()
  178. }else {
  179. self?.share()
  180. }
  181. }).disposed(by: disposeBag)
  182. tableView.addHeader(withBeginRefresh: true, animation: true) {
  183. [weak self] (page) in
  184. self?.communityPostDetailApi()
  185. self?.communityPostCommentApi(page: page)
  186. }
  187. tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
  188. [weak self] (page) in
  189. self?.communityPostsApi(page: page)
  190. })
  191. commentView.bottomClickClosure = {
  192. [weak self] (clickType) in
  193. switch clickType {
  194. case BottomClickType.typeComment:
  195. self?.showKeyBoardCommentView(placeholder: "添加评论...")
  196. case BottomClickType.typeLike:
  197. VirusViewModel.shared.virueRecordAddApiPraise(communityPostDetailModel: self?.communityPostDetailModel,commentView: self?.commentView)
  198. case BottomClickType.typeCollect:
  199. VirusViewModel.shared.virueRecordAddApiCollection(communityPostDetailModel: self?.communityPostDetailModel,commentView: self?.commentView)
  200. }
  201. }
  202. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  203. let followStatusModel = notification.object as? FollowStatusModel
  204. if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
  205. self?.communityPostDetailModel?.isFollow = followStatusModel?.isFollowStatus
  206. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  207. CommunityFollowUserViewModel.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  208. self?.tableView.reloadData()
  209. }
  210. }
  211. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  212. let followStatusModel = notification.object as? FollowStatusModel
  213. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  214. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  215. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  216. self?.tableView.reloadData()
  217. }
  218. }
  219. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  220. let followStatusModel = notification.object as? FollowStatusModel
  221. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  222. self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
  223. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  224. }
  225. if !(self?.communityPostCommentModels.isEmpty ?? true) {
  226. for communityPostDataModel in (self?.communityPostDataModels)! {
  227. if communityPostDataModel.uid == followStatusModel?.uid {
  228. communityPostDataModel.isLike = followStatusModel?.isFollowStatus
  229. }
  230. }
  231. }
  232. self?.tableView.reloadData()
  233. }
  234. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  235. let followStatusModel = notification.object as? FollowStatusModel
  236. if self?.communityPostDetailModel?.id == followStatusModel?.postId {
  237. self?.communityPostDetailModel?.isCollect = followStatusModel?.isFollowStatus
  238. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  239. }
  240. self?.tableView.reloadData()
  241. }
  242. }
  243. /// 显示键盘
  244. func showKeyBoardCommentView(placeholder:String) {
  245. KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
  246. [weak self] text in
  247. self?.communityPostCommentApi(text: text, complete: {
  248. [weak self] in
  249. self?.communityPostCommentModel = nil
  250. })
  251. }) {
  252. [weak self] in
  253. self?.communityPostCommentModel = nil
  254. }
  255. }
  256. /// 分享
  257. func share() {
  258. var title = self.communityPostDetailModel?.title?.prefix(12)
  259. if title == nil || title == "" {
  260. title = self.communityPostDetailModel?.content?.prefix(12) ?? ""
  261. }
  262. let shareCommunityView = ShareCommunityView.shareCommunityView(
  263. uid: self.communityPostDetailModel?.uid ?? 0,
  264. h5Str: (self.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
  265. thumbnailImg: UIImage.imageUrl(self.communityPostDetailModel?.img),
  266. sharedTitle: String(title ?? ""),
  267. sharedDetailTitle: kCommunityPostShareDetailTitle,
  268. shareCommunityViewType: ShareCommunityViewType.postDetail,
  269. completion: {
  270. [weak self] in
  271. 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)
  272. })
  273. shareCommunityView.saveCompletion = {
  274. [weak self] in
  275. let vc = CommunityShareContentViewController()
  276. vc.imgUrl = self?.communityPostDetailModel?.img
  277. let communityPostDetailTopicModel = self?.communityPostDetailModel?.topic?[0]
  278. vc.topicStr = communityPostDetailTopicModel?.name ?? ""
  279. if self?.communityPostDetailModel?.title == "" || self?.communityPostDetailModel?.title == nil {
  280. vc.titleStr = "\(self?.communityPostDetailModel?.content?.prefix(20) ?? "")"
  281. }else {
  282. vc.titleStr = "\(self?.communityPostDetailModel?.title?.prefix(20) ?? "")"
  283. }
  284. vc.avatarStr = self?.communityPostDetailModel?.avatar
  285. vc.nameStr = self?.communityPostDetailModel?.username
  286. vc.H5UrlStr = (self?.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
  287. vc.bean = self?.communityPostDetailModel?.willCollectBean
  288. vc.postId = self?.communityPostDetailModel?.id
  289. vc.uid = self?.communityPostDetailModel?.uid
  290. vc.type = self?.communityPostDetailModel?.type
  291. vc.contentStr = self?.communityPostDetailModel?.content
  292. self?.navigationController?.pushViewController(vc, animated: true)
  293. }
  294. shareCommunityView.deleteCompletion = {
  295. [weak self] in
  296. self?.communityDeleteApi(postId: self?.communityPostDetailModel?.id ?? 0)
  297. }
  298. }
  299. }
  300. extension CommunityRecommendController {
  301. /// 帖子内容
  302. func communityPostDetailApi() {
  303. SwiftMoyaNetWorkServiceCommunity.shared().communityPostDetailApi(id: id, completion: {
  304. [weak self] (communityPostDetailModel) -> (Void) in
  305. self?.tableView.snp.remakeConstraints {[weak self] (make) in
  306. make.top.equalToSuperview().offset(kNavBarTotalHeight)
  307. make.left.right.equalToSuperview()
  308. make.bottom.equalTo((self?.commentView.snp_top)!).offset(0)
  309. }
  310. DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
  311. [weak self] in
  312. self?.tabBarController?.selectedIndex = 0
  313. self?.navigationController?.popToRootViewController(animated: true)
  314. })
  315. self?.communityPostDetailModel = communityPostDetailModel as? CommunityPostDetailModel
  316. self?.communityPostsApi(page:1)
  317. self?.avatarButton.kf.setImage(with: kURLThumbnailsImage(name: self?.communityPostDetailModel?.avatar ?? "", size: kSize30x30Image), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
  318. KingfisherManager.shared.cache.clearMemoryCache()
  319. self?.nameButton.setTitle(self?.communityPostDetailModel?.username, for: UIControl.State.normal)
  320. if self?.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
  321. self?.followButton.alpha = 0
  322. }else {
  323. CommunityFollowUserViewModel.setFollowListType(followButton: self?.followButton, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
  324. }
  325. self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
  326. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  327. if PostType(rawValue: self?.communityPostDetailModel?.type ?? "html") == .html {
  328. self?.avatarButton.alpha = 0
  329. self?.nameButton.alpha = 0
  330. self?.followButton.alpha = 0
  331. }else {
  332. self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth)
  333. if !(self?.communityPostDetailModel?.imgs?.isEmpty ?? true) {
  334. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  335. }else {
  336. if self?.communityPostDetailModel?.img != nil || self?.communityPostDetailModel?.img != "" {
  337. self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
  338. }
  339. }
  340. self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
  341. }
  342. self?.setupLayouts()
  343. self?.tableView.reloadData()
  344. self?.readVirueRecordAddApi()
  345. }) { [weak self] loadingStatus in
  346. if loadingStatus == .noData {
  347. DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
  348. [weak self] in
  349. self?.tabBarController?.selectedIndex = 0
  350. self?.navigationController?.popToRootViewController(animated: true)
  351. })
  352. self?.tableView.reloadData()
  353. MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
  354. if self?.tableView.mj_footer != nil {
  355. self?.tableView.isHiddenFooter(true)
  356. }
  357. }else {
  358. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  359. }
  360. }
  361. }
  362. /// 阅读virue
  363. func readVirueRecordAddApi() {
  364. var postDesc : String?
  365. if self.communityPostDetailModel?.title == nil || self.communityPostDetailModel?.title == "" {
  366. postDesc = "\(self.communityPostDetailModel?.content?.prefix(20) ?? "")"
  367. }else {
  368. postDesc = self.communityPostDetailModel?.title
  369. }
  370. 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)")
  371. }
  372. /// 评论列表
  373. ///
  374. /// - Parameters:
  375. /// - postId: 内容id
  376. /// - page: 分页
  377. func communityPostCommentApi(page:Int) {
  378. SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page,completion: {
  379. [weak self] (communityPostCommentsModel) -> (Void) in
  380. self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
  381. self?.count = self?.communityPostDetailModel?.commentCount ?? 0
  382. if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
  383. self?.communityPostCommentModels.removeAll()
  384. self?.tableView.resetNoMoreData()
  385. }
  386. self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
  387. self?.tableView.reloadData()
  388. }) { _ in }
  389. }
  390. /// 相关推荐
  391. ///
  392. /// - Parameter page: 分页
  393. func communityPostsApi(page:Int) {
  394. SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(topicIds:"[\(self.communityPostDetailModel?.topicIds ?? "")]", page: page, completion: {
  395. [weak self] (communityPostsModel) -> (Void) in
  396. let communityPostsModel = communityPostsModel as? CommunityPostsModel
  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.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityPostsModel?.pagination)
  405. }) {
  406. [weak self] (loadingStatus) in
  407. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
  408. }
  409. }
  410. /// 获取高度
  411. func heightList() {
  412. heights.removeAll()
  413. if !(communityPostDataModels.isEmpty) {
  414. for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
  415. //图片高度
  416. var imageHeight : CGFloat!
  417. if index == 0 {
  418. imageHeight = (kScreenWidth - 15)/2
  419. }else {
  420. imageHeight = 240 * kScaleWidth
  421. }
  422. //label高度
  423. var labelHeight : CGFloat = 0
  424. //总间距
  425. let spacingHeght : CGFloat = 35
  426. let str = communityPostDataModel.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(13)
  427. if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
  428. let content = "\(str ?? "")"
  429. labelHeight = (content.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))
  430. }else {
  431. let str = communityPostDataModel.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "")
  432. labelHeight = (str?.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))!
  433. }
  434. //button高度
  435. let buttonHeight = 18
  436. let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
  437. heights.append(totalHeight)
  438. }
  439. }
  440. }
  441. /// 评论
  442. func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
  443. let communityCustomCommnetModel = CommunityCustomCommnetModel()
  444. communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
  445. communityCustomCommnetModel.content = text
  446. if communityPostCommentModel != nil {
  447. communityCustomCommnetModel.parentId = communityPostCommentModel?.id
  448. communityCustomCommnetModel.replyUid = communityPostCommentModel?.uid
  449. communityCustomCommnetModel.replyUsername = communityPostCommentModel?.username
  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.comment(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. }