123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848 |
- //
- // CommunityRecommendController.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/6/13.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import Kingfisher
- class CommunityRecommendController: BaseViewController {
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
-
- deinit {
- NXLLog("deinit")
- if observe != nil {
- NotificationCenter.default.removeObserver(observe!)
- }
- }
- weak var observe : NSObjectProtocol?
- // 帖子Id
- var id : Int = 0
- var communityPostDetailModel : CommunityPostDetailModel?
- // 评论
- var communityPostCommentsModel : CommunityPostCommentsModel?
- var communityPostCommentModels = Array<CommunityPostCommentModel>()
- var communityPostCommentModel : CommunityPostCommentModel?
- // 相关推荐
- var communityPostDataModels = Array<CommunityPostDataModel>()
- // 全部评论
- var count : Int = 0
-
- var indexPath : IndexPath?
- var heights = Array<CGFloat>()
-
- /// 相关推荐高度
- var heightModel = HeightModel()
- /// 内容高度
- var heightModel1 = HeightModel()
- /// 最后滚动的位置
- var lastContentOffset : CGFloat = 0
-
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupData()
- }
-
- override func setupViews() {
-
- view.backgroundColor = kf7f8faColor
- navigationBar.addSubview(avatarButton)
- navigationBar.addSubview(nameButton)
- navigationBar.addSubview(followButton)
- view.addSubview(commentView)
- view.addSubview(tableView)
- view.addSubview(sharedButton)
-
- tableView.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kNavBarTotalHeight)
- make.left.right.equalToSuperview()
- make.bottom.equalToSuperview()
- }
- }
-
- override func setupLayouts() {
- navigationBar.wr_setRightButton(image: kImage(name: "nav_share_black")!)
- avatarButton.snp.makeConstraints { (make) in
- make.left.equalTo(navigationBar.leftButton.snp_right)
- make.size.equalTo(30)
- make.centerY.equalTo(navigationBar.leftButton)
- }
- nameButton.snp.makeConstraints { (make) in
- make.centerY.equalTo(avatarButton)
- make.left.equalTo(avatarButton.snp_right).offset(4)
- make.right.lessThanOrEqualTo(followButton.snp_left).offset(-10)
- }
- followButton.snp_makeConstraints { (make) in
- make.right.equalTo(navigationBar.rightButton.snp_left)
- make.height.equalTo(24)
- make.width.equalTo(60)
- make.centerY.equalTo(navigationBar.leftButton)
- }
-
- commentView.snp.makeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(48+kSafeTabBarHeight)
- make.bottom.equalToSuperview()
- }
- sharedButton.snp.makeConstraints { (make) in
- make.bottom.equalTo(commentView.snp_top).offset(-20)
- make.height.equalTo(99.5)
- make.width.equalTo(82)
- make.right.equalTo(-14)
- }
- }
-
- lazy var avatarButton: UIButton = {
- let avatarButton = UIButton(type: UIButton.ButtonType.custom)
- avatarButton.setImage(kImage(name: "default_avatar"), for: UIControl.State.normal)
- avatarButton.cornerRadius = 15
- avatarButton.masksToBounds = true
- return avatarButton
- }()
-
- lazy var nameButton: UIButton = {
- let nameButton = UIButton(type: UIButton.ButtonType.custom)
- nameButton.setTitle("昵称", for: UIControl.State.normal)
- nameButton.setTitleColor(k262626Color, for: UIControl.State.normal)
- nameButton.titleLabel?.font = kRegularFont14
- return nameButton
- }()
-
- lazy var followButton: UIButton = {
- let followButton = UIButton(type: UIButton.ButtonType.custom)
- followButton.titleLabel?.font = kMediumFont13
- followButton.cornerRadius = 12
- followButton.masksToBounds = true
- followButton.alpha = 0
- return followButton
- }()
-
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
- tableView.separatorStyle = .none
- tableView.backgroundColor = kf7f8faColor
- tableView.dataSource = self
- tableView.delegate = self
- return tableView
- }()
- lazy var communityPostDetailTableViewHeaderView: CommunityPostDetailTableViewHeaderView = {
- let communityPostDetailTableViewHeaderView = CommunityPostDetailTableViewHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0))
- return communityPostDetailTableViewHeaderView
- }()
-
- lazy var commentView: RecommendBottomCommentView = {
- let commentView = RecommendBottomCommentView()
- return commentView
- }()
-
- lazy var sharedButton: UIButton = {
- let sharedButton = UIButton(type: UIButton.ButtonType.custom)
- sharedButton.setImage(UIImage.gif(name: "share_iSpt"), for: UIControl.State.normal)
- return sharedButton
- }()
- override func setupData() {
- //头像
- avatarButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = self?.communityPostDetailModel?.uid ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- }
- }).disposed(by: disposeBag)
- //用户昵称
- nameButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = self?.communityPostDetailModel?.uid ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- }
- }).disposed(by: disposeBag)
- // 关注
- followButton.rx.tap.subscribe(onNext: { [weak self] (data) in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- CommunityFollowUserViewModel.shared.follow(communityPostDetailModel: (self?.communityPostDetailModel)!, button: (self?.followButton)!)
- }
- }).disposed(by: disposeBag)
- //分享
- navigationBar.onClickRightButton = {
- [weak self] in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- self?.share()
- }
- }
- sharedButton.rx.tap.subscribe(onNext: { [weak self] _ in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- self?.share()
- }
- }).disposed(by: disposeBag)
- tableView.addHeader(withBeginRefresh: true, animation: true) {
- [weak self] (page) in
- self?.communityPostDetailApi()
- self?.communityPostCommentApi(page: page)
- }
- tableView.addAutoNormalFooter(withAutomaticallyRefresh: true, loadMoreBlock: {
- [weak self] (page) in
- self?.communityPostsApi(page: page)
- })
-
- commentView.bottomClickClosure = {
- [weak self] (clickType) in
- switch clickType {
- case BottomClickType.typeComment:
- self?.showKeyBoardCommentView(placeholder: "添加评论...")
- case BottomClickType.typeLike:
- VirusViewModel.shared.virueRecordAddApiPraise(communityPostDetailModel: self?.communityPostDetailModel,commentView: self?.commentView)
- case BottomClickType.typeCollect:
- VirusViewModel.shared.virueRecordAddApiCollection(communityPostDetailModel: self?.communityPostDetailModel,commentView: self?.commentView)
- }
- }
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("followApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let followStatusModel = notification.object as? FollowStatusModel
- if self?.communityPostDetailModel?.uid == followStatusModel?.uid {
- self?.communityPostDetailModel?.isFollow = followStatusModel?.isFollowStatus
- self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
- CommunityFollowUserViewModel.setFollowType(followButton: (self?.followButton)!, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
- self?.tableView.reloadData()
- }
- }
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("unlikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let followStatusModel = notification.object as? FollowStatusModel
- if self?.communityPostDetailModel?.id == followStatusModel?.postId {
- self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
- self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
- self?.tableView.reloadData()
- }
- }
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("islikeApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let followStatusModel = notification.object as? FollowStatusModel
- if self?.communityPostDetailModel?.id == followStatusModel?.postId {
- self?.communityPostDetailModel?.isDislike = followStatusModel?.isFollowStatus
- self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
- }
- if !(self?.communityPostCommentModels.isEmpty ?? true) {
- for communityPostDataModel in (self?.communityPostDataModels)! {
- if communityPostDataModel.uid == followStatusModel?.uid {
- communityPostDataModel.isLike = followStatusModel?.isFollowStatus
- }
- }
- }
- self?.tableView.reloadData()
- }
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("isCollectApi"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let followStatusModel = notification.object as? FollowStatusModel
- if self?.communityPostDetailModel?.id == followStatusModel?.postId {
- self?.communityPostDetailModel?.isCollect = followStatusModel?.isFollowStatus
- self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
- }
- self?.tableView.reloadData()
- }
- }
-
- /// 显示键盘
- func showKeyBoardCommentView(placeholder:String) {
- KeyBoardInputView.show(placeholder: placeholder, inputViewResultClosure: {
- [weak self] text in
- self?.communityPostCommentApi(text: text, complete: {
- [weak self] in
- self?.communityPostCommentModel = nil
- })
- }) {
- [weak self] in
- self?.communityPostCommentModel = nil
- }
- }
-
-
- /// 分享
- func share() {
- var title = self.communityPostDetailModel?.title
- if title == nil || title == "" {
- title = self.communityPostDetailModel?.content
- }
- let shareCommunityView = ShareCommunityView.shareCommunityView(
- uid: self.communityPostDetailModel?.uid ?? 0,
- h5Str: (self.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
- thumbnailImg: UIImage.imageUrl(self.communityPostDetailModel?.img),
- sharedTitle: String(title ?? ""),
- sharedDetailTitle: kCommunityPostShareDetailTitle,
- shareCommunityViewType: ShareCommunityViewType.postDetail,
- completion: {
- [weak self] in
- 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)
- })
- shareCommunityView.saveCompletion = {
- [weak self] in
- let vc = CommunityShareContentViewController()
- vc.imgUrl = self?.communityPostDetailModel?.img
- let communityPostDetailTopicModel = self?.communityPostDetailModel?.topic?[0]
- vc.topicStr = communityPostDetailTopicModel?.name ?? ""
- if self?.communityPostDetailModel?.title == "" || self?.communityPostDetailModel?.title == nil {
- vc.titleStr = "\(self?.communityPostDetailModel?.content?.prefix(20) ?? "")"
-
- }else {
- vc.titleStr = "\(self?.communityPostDetailModel?.title?.prefix(20) ?? "")"
- }
- vc.avatarStr = self?.communityPostDetailModel?.avatar
- vc.nameStr = self?.communityPostDetailModel?.username
- vc.H5UrlStr = (self?.communityPostDetailModel?.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
- vc.bean = self?.communityPostDetailModel?.willCollectBean
- vc.postId = self?.communityPostDetailModel?.id
- vc.uid = self?.communityPostDetailModel?.uid
- vc.type = self?.communityPostDetailModel?.type
- vc.contentStr = self?.communityPostDetailModel?.content
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- shareCommunityView.deleteCompletion = {
- [weak self] in
- self?.communityDeleteApi(postId: self?.communityPostDetailModel?.id ?? 0)
- }
- }
-
- }
- extension CommunityRecommendController {
-
-
- /// 帖子内容
- func communityPostDetailApi() {
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostDetailApi(id: id, completion: {
- [weak self] (communityPostDetailModel) -> (Void) in
-
- self?.tableView.snp.remakeConstraints {[weak self] (make) in
- make.top.equalToSuperview().offset(kNavBarTotalHeight)
- make.left.right.equalToSuperview()
- make.bottom.equalTo((self?.commentView.snp_top)!).offset(0)
- }
-
- DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
- [weak self] in
- self?.tabBarController?.selectedIndex = 0
- self?.navigationController?.popToRootViewController(animated: true)
- })
- self?.communityPostDetailModel = communityPostDetailModel as? CommunityPostDetailModel
- self?.communityPostsApi(page:1)
- self?.avatarButton.kf.setImage(with: kURLImage(name: self?.communityPostDetailModel?.avatar ?? ""), for: UIControl.State.normal, placeholder: kImage(name: "default_avatar"))
- KingfisherManager.shared.cache.clearMemoryCache()
- self?.nameButton.setTitle(self?.communityPostDetailModel?.username, for: UIControl.State.normal)
- if self?.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
- self?.followButton.alpha = 0
- }else {
- CommunityFollowUserViewModel.setFollowListType(followButton: self?.followButton, followType: FollowType(rawValue: self?.communityPostDetailModel?.isFollow ?? 0) ?? .futureFollow)
- }
- self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
- self?.count = self?.communityPostDetailModel?.commentCount ?? 0
-
- if PostType(rawValue: self?.communityPostDetailModel?.type ?? "html") == .html {
- self?.avatarButton.alpha = 0
- self?.nameButton.alpha = 0
- self?.followButton.alpha = 0
- }else {
- self?.avatarButton.alpha = 1
- self?.nameButton.alpha = 1
- self?.followButton.alpha = 1
- self?.communityPostDetailTableViewHeaderView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth)
- if !(self?.communityPostDetailModel?.imgs?.isEmpty ?? true) {
- self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
- }else {
- if self?.communityPostDetailModel?.img != nil || self?.communityPostDetailModel?.img != "" {
- self?.tableView.tableHeaderView = self?.communityPostDetailTableViewHeaderView
- }
- }
-
- self?.communityPostDetailTableViewHeaderView.communityPostDetailModel = self?.communityPostDetailModel
- }
- self?.setupLayouts()
- self?.tableView.reloadData()
- self?.readVirueRecordAddApi()
- }) { [weak self] loadingStatus in
- if loadingStatus == .noData {
- DIYEmptyView.emptyNoDataActionTableView(tableView: self?.tableView, imageStr: .three, detailStr: .three, btnTitleStr: .two, btnClickBlock: {
- [weak self] in
- self?.tabBarController?.selectedIndex = 0
- self?.navigationController?.popToRootViewController(animated: true)
- })
- self?.tableView.reloadData()
- MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
- if self?.tableView.mj_footer != nil {
- self?.tableView.isHiddenFooter(true)
- }
- }else {
- MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
- }
- }
- }
-
-
- /// 阅读virue
- func readVirueRecordAddApi() {
- var postDesc : String?
- if self.communityPostDetailModel?.title == nil || self.communityPostDetailModel?.title == "" {
- postDesc = "\(self.communityPostDetailModel?.content?.prefix(20) ?? "")"
- }else {
- postDesc = self.communityPostDetailModel?.title
- }
-
- 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)")
- }
-
- /// 评论列表
- ///
- /// - Parameters:
- /// - postId: 内容id
- /// - page: 分页
- func communityPostCommentApi(page:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentsApi(postId: id, page: page,completion: {
- [weak self] (communityPostCommentsModel) -> (Void) in
- self?.communityPostCommentsModel = communityPostCommentsModel as? CommunityPostCommentsModel
- self?.count = self?.communityPostDetailModel?.commentCount ?? 0
- if self?.communityPostCommentsModel?.pagination?.currentPage == 1{
- self?.communityPostCommentModels.removeAll()
- self?.tableView.resetNoMoreData()
- }
- self?.communityPostCommentModels = (self?.communityPostCommentModels)! + (self?.communityPostCommentsModel?.data!)!
- self?.tableView.reloadData()
- }) { _ in }
- }
-
- /// 相关推荐
- ///
- /// - Parameter page: 分页
- func communityPostsApi(page:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostsApi(topicIds:"[\(self.communityPostDetailModel?.topicIds ?? "")]", page: page, completion: {
- [weak self] (communityPostsModel) -> (Void) in
- let communityPostsModel = communityPostsModel as? CommunityPostsModel
- if communityPostsModel?.pagination?.currentPage == 1{
- self?.communityPostDataModels.removeAll()
- self?.tableView.resetNoMoreData()
- }
- self?.communityPostDataModels = (self?.communityPostDataModels)! + (communityPostsModel?.data!)!
- self?.heightList()
- self?.tableView.reloadData()
- MJRefreshManager.mjRefreshManagerPaginationNoHiddenFooter(tableView: self?.tableView, pagination: communityPostsModel?.pagination)
- }) {
- [weak self] (loadingStatus) in
-
- MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView,loadingStatus: loadingStatus)
- }
- }
-
- /// 获取高度
- func heightList() {
- heights.removeAll()
- if !(communityPostDataModels.isEmpty) {
- for (index,communityPostDataModel) in (communityPostDataModels.enumerated()) {
- //图片高度
- var imageHeight : CGFloat!
- if index == 0 {
- imageHeight = (kScreenWidth - 15)/2
- }else {
- imageHeight = 240 * kScaleWidth
- }
-
- //label高度
- var labelHeight : CGFloat = 0
- //总间距
- let spacingHeght : CGFloat = 35
-
- let str = communityPostDataModel.content?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(20)
-
- if communityPostDataModel.title == "" || communityPostDataModel.title == nil {
- let content = "\(str ?? "")"
- labelHeight = (content.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))
- }else {
- let str = "\(communityPostDataModel.title?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\r", with: "").prefix(20) ?? "")"
- labelHeight = (str.heightForComment(font: kMediumFont13!, width: (((kScreenWidth - 15)/2) - 20)))
- }
-
- //button高度
- let buttonHeight = 18
- let totalHeight = CGFloat(imageHeight!) + CGFloat(labelHeight) + CGFloat(spacingHeght) + CGFloat(buttonHeight)
- heights.append(totalHeight)
- }
- }
- }
-
-
- /// 评论
- func communityPostCommentApi(text:String,complete: @escaping () -> ()) {
- let communityCustomCommnetModel = CommunityCustomCommnetModel()
- communityCustomCommnetModel.postId = communityPostDetailModel?.id ?? 0
- communityCustomCommnetModel.content = text
- if communityPostCommentModel != nil {
- communityCustomCommnetModel.parentId = communityPostCommentModel?.id
- }
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostCommentApi(communityCustomCommnetModel: communityCustomCommnetModel) {
- [weak self] (communityPostCommentIdModel) -> (Void) in
-
- let communityPostCommentIdModel = communityPostCommentIdModel as? CommunityPostCommentIdModel
- if self?.communityPostCommentModel == nil { //评
- let communityPostCommentModel = CommunityPostCommentModel()
- communityPostCommentModel.avatar = UserModel.shared().getModel()?.avatarurl
- communityPostCommentModel.content = text
- communityPostCommentModel.createdAt = "刚刚"
- communityPostCommentModel.id = communityPostCommentIdModel?.id
- communityPostCommentModel.username = UserModel.shared().getModel()?.username
- communityPostCommentModel.uid = UserModel.shared().getModel()?.uid
- self?.communityPostCommentModels.insert(communityPostCommentModel, at: 0)
- VirusViewModel.shared.comment(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text)
- }else { //回评论
- let communityPostReplyModel = CommunityPostReplyModel()
- communityPostReplyModel.avatar = UserModel.shared().getModel()?.avatarurl
- communityPostReplyModel.content = text
- communityPostReplyModel.createdAt = "刚刚"
- communityPostReplyModel.id = communityPostCommentIdModel?.id
- communityPostReplyModel.username = UserModel.shared().getModel()?.username
- communityPostReplyModel.uid = UserModel.shared().getModel()?.uid
- let count = self?.communityPostCommentModel?.replyCount ?? 0 + 1
- self?.communityPostCommentModel?.replyCount = count
- if self?.communityPostCommentModel?.reply == nil {
- self?.communityPostCommentModel?.reply = Array<CommunityPostReplyModel>()
- }
- self?.communityPostCommentModel?.reply?.insert(communityPostReplyModel, at: 0)
- VirusViewModel.shared.commentReply(communityPostDetailModel: (self?.communityPostDetailModel)!, id: communityPostCommentIdModel?.id ?? 0,content: text, communityPostCommentModel: self?.communityPostCommentModel)
- }
- self?.count += 1
- self?.communityPostDetailModel?.commentCount = self?.count
- self?.commentView.communityPostDetailModel = self?.communityPostDetailModel
- self?.tableView.reloadData()
- complete()
- }
- }
-
- /// 删除帖子
- func communityDeleteApi(postId:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
- self?.navigationController?.popViewController(animated: true)
- }
- }
- }
- // MARK: - tableView dataSource && delegate
- extension CommunityRecommendController: UITableViewDataSource, UITableViewDelegate {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- if communityPostDetailModel != nil {
- return 3
- }else {
- return 0
- }
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- switch section {
- case 0:
- return 1
- case 1:
- return communityPostCommentModels.isEmpty ? 1 : communityPostCommentModels.count
- case 2:
- return 1
- default:
- return 0
- }
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- switch indexPath.section {
- case 0:
-
- if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
- let cell = CommunityRecommendDetailHTMLTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.communityPostDetailModel = communityPostDetailModel
- cell.frame = tableView.bounds
- cell.layoutIfNeeded()
- cell.reloadData()
- cell.heightModel = heightModel1
- cell.tableView = tableView
- return cell
- }else {
- let cell = RecommendDetailContentCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.communityPostDetailModel = communityPostDetailModel
- cell.frame = tableView.bounds
- cell.layoutIfNeeded()
- cell.reloadData()
- cell.heightModel = heightModel1
- return cell
- }
-
- case 1:
- if communityPostCommentModels.isEmpty {
- let cell = RecommendNoneCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.communityPostDetailModel = communityPostDetailModel
- cell.commentClosure = {
- [weak self] in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- self?.showKeyBoardCommentView(placeholder: "添加评论...")
- }
- }
- cell.userClosure = {
- [weak self] in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- if self?.communityPostDetailModel?.uid != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = self?.communityPostDetailModel?.uid ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- }
- }
- return cell
- }else {
- let cell = RecommendMajorCommentCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.communityPostDetailModel = communityPostDetailModel
- cell.communityPostCommentModel = communityPostCommentModels[indexPath.row]
- cell.frame = tableView.bounds
- cell.layoutIfNeeded()
- cell.reloadData()
- return cell
- }
- case 2:
- let cell = RecommendSimilarCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.heights = heights
- cell.communityPostDataModels = communityPostDataModels
- cell.frame = tableView.bounds
- cell.layoutIfNeeded()
- cell.reloadData()
- cell.heightModel = heightModel
- return cell
- default:
- return UITableViewCell()
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- switch indexPath.section {
- case 0:
- break
- case 1:
- if communityPostCommentModels.isEmpty {
- break
- }else {
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- communityPostCommentModel = self.communityPostCommentModels[indexPath.row]
- self.indexPath = indexPath
-
- if communityPostCommentModel?.isDelete != 1 {
- CommentReplyView.commentReplyView(id: communityPostCommentModel?.id, uid: communityPostCommentModel?.uid,userName: communityPostCommentModel?.username ?? "", content: communityPostCommentModel?.content ?? "", replyClosure: {
- [weak self] in
- self?.communityPostCommentModel = self?.communityPostCommentModels[indexPath.row]
- self?.showKeyBoardCommentView(placeholder: "回复:@\(self?.communityPostCommentModel?.username ?? "")")
- }, deleteClosure: {
- [weak self] in
- self?.communityPostCommentModel?.isDelete = 1
- self?.communityPostCommentModel?.content = "该评论已被删除"
- self?.tableView.reloadData()
- })
- }else {
- SwiftProgressHUD.shared().showText("该评论已删除,暂时不能评论")
- communityPostCommentModel = nil
- }
-
-
- }
- break
- }
- case 2:
- break
- default:
- break
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- switch indexPath.section {
- case 0:
- return heightModel1.height ?? 0
- case 1:
- if communityPostCommentModels.isEmpty {
- return 132
- }else {
- return communityPostCommentModels[indexPath.row].height ?? 0
- }
- case 2:
- return heightModel.height ?? 0
- default:
- return 0
- }
-
-
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- switch section {
- case 0:
- return 0.000001
- case 1:
- if communityPostCommentModels.isEmpty {
- return 0.000001
- }else {
- return 64
- }
- case 2:
- return 52
- default:
- return 0.000001
- }
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- switch section {
- case 0:
- return UIView()
- case 1:
- if communityPostCommentModels.isEmpty {
- return UIView()
- }else {
- let headerView = RecommendCommentHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 64))
- headerView.count = count
- return headerView
- }
- case 2:
- return RecommendSimilarHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 52))
- default:
- return UIView()
- }
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- switch section {
- case 0:
- return 10
- case 1:
- if communityPostCommentModels.isEmpty {
- return 0.000001
- }else {
- if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
- return 0.000001
- }else {
- return 51
- }
- }
- case 2:
- return 0.000001
- default:
- return 0.000001
- }
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- switch section {
- case 0:
- return UIView()
- case 1:
- if communityPostCommentModels.isEmpty {
- return UIView()
- }else {
-
- if communityPostCommentsModel?.pagination?.currentPage ?? 0 >= communityPostCommentsModel?.pagination?.totalPages ?? 0 {
- return UIView()
- }else {
- let footerView = RecommendCommentFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 51))
- footerView.unfoldClosure = {
- [weak self] in
- self?.communityPostCommentApi(page: (self?.communityPostCommentsModel?.pagination?.currentPage ?? 0) + 1)
- }
- return footerView
- }
- }
- case 2:
- return UIView()
- default:
- return UIView()
- }
- }
-
- }
- // MARK: - scrollView
- extension CommunityRecommendController {
-
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- if PostType(rawValue: communityPostDetailModel?.type ?? "html") == .html {
- let offsetY = scrollView.contentOffset.y
- if (offsetY > kSafeStatusBarHeight) {
- let alpha = (offsetY - kSafeStatusBarHeight) / CGFloat(kNavBarTotalHeight)
- self.avatarButton.alpha = alpha
- self.nameButton.alpha = alpha
- if self.communityPostDetailModel?.uid == UserModel.shared().getModel()?.uid {
- self.followButton.alpha = 0
- }else {
- self.followButton.alpha = alpha
- }
- } else {
- self.avatarButton.alpha = 0
- self.nameButton.alpha = 0
- self.followButton.alpha = 0
- }
- }
- }
-
- func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
- lastContentOffset = scrollView.contentOffset.y
- }
-
- func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
- if lastContentOffset < scrollView.contentOffset.y {
- sharedButton.isHidden = true
- }else{
- sharedButton.isHidden = false
- }
- }
-
- func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
- sharedButton.isHidden = false
- }
- }
|