123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- //
- // CommunityViewController.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/6/11.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 社区首页
- import UIKit
- import JXSegmentedView
- import SwiftyMediator
- enum CommunityModuleTopType : String {
- case communityModuleFollowTop = "CommunityModuleFollowTop"
- case communityModuleRecommendTop = "CommunityModuleRecommendTop"
- case communityModulePlanetTop = "CommunityModulePlanetTop"
- }
- var communityModuleTopType :CommunityModuleTopType?
- class CommunityViewController: BaseViewController {
-
- deinit {
- if observe != nil {
- NotificationCenter.default.removeObserver(observe!)
- }
- }
- weak var observe : NSObjectProtocol?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupLayouts()
- setupData()
- }
-
- override func setupViews() {
- navigationBar.isHidden = true
- view.addSubview(segmentedView)
- view.addSubview(listContainerView)
- view.addSubview(navigationBarbgView)
- view.addSubview(communityNavigationBarView)
-
- }
-
- override func setupLayouts() {
-
- navigationBarbgView.snp.makeConstraints { (make) in
- make.top.equalTo(kNavBarTotalHeight+48)
- make.left.right.equalToSuperview()
- make.height.equalTo(62)
- }
-
- communityNavigationBarView.snp.makeConstraints { (make) in
- make.top.equalTo(kSafeStatusBarHeight)
- make.left.right.equalToSuperview()
- make.height.equalTo(kNavBarHeight)
- }
-
- segmentedView.snp.makeConstraints { (make) in
- make.left.equalToSuperview()
- make.right.equalToSuperview()
- make.height.equalTo(48)
- make.top.equalTo(kNavBarTotalHeight)
- }
-
- listContainerView.snp.makeConstraints { (make) in
- make.top.equalTo(segmentedView.snp.bottom)
- make.left.right.bottom.equalToSuperview()
- }
- }
-
- override func setupData() {
- //发布
- communityNavigationBarView.leftButton.rx.tap.subscribe(onNext: { (data) in
-
- }).disposed(by: disposeBag)
- //搜索
- communityNavigationBarView.searchClosure = {
- [weak self] in
- let vc = SearchViewController()
- vc.searchType = .content
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- //彩虹豆
- communityNavigationBarView.rightButtonClosure = {
- [weak self] in
- self?.reloadSegmentedStarView()
- }
-
- // 找朋友 查看全部推荐关注
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityFindFriends"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let vc = CommunityFindFriendsViewController()
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- // 推荐分享
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityRecommendShare"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let communityRecommendDataModel = notification.object as? CommunityRecommendDataModel
- let communityRecommendTopicModel = communityRecommendDataModel?.topic?[0]
-
- self?.share(
- imgUrl: communityRecommendDataModel?.img,
- topicStr: communityRecommendTopicModel?.name,
- titleStr: communityRecommendDataModel?.title,
- avatarStr: communityRecommendDataModel?.avatar,
- nameStr: communityRecommendDataModel?.name,
- H5UrlStr: communityRecommendDataModel?.h5url,
- bean: communityRecommendDataModel?.willCollectBean,
- postId: communityRecommendDataModel?.id,
- uid: communityRecommendDataModel?.uid,
- type: communityRecommendDataModel?.type,
- contentStr: communityRecommendDataModel?.content
- )
-
- }
-
- // 关注分享
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityFollowShare"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- let communityFollowDataModel = notification.object as? CommunityFollowDataModel
- let communityFollowTopicModel = communityFollowDataModel?.relateData?.topic?[0]
-
- self?.share(
- imgUrl: communityFollowDataModel?.relateData?.img,
- topicStr: communityFollowTopicModel?.name,
- titleStr: communityFollowDataModel?.relateData?.title,
- avatarStr: communityFollowDataModel?.relateData?.avatar,
- nameStr: communityFollowDataModel?.relateData?.username,
- H5UrlStr: communityFollowDataModel?.relateData?.h5url,
- bean: communityFollowDataModel?.relateData?.willCollectBean,
- postId: communityFollowDataModel?.relateData?.id,
- uid: communityFollowDataModel?.relateData?.uid,
- type: communityFollowDataModel?.relateData?.type,
- contentStr: communityFollowDataModel?.relateData?.content)
-
- }
-
- // 查看全部热门视频
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityPopularVideo"), object: nil, queue: OperationQueue.main) {
- [weak self] (notification) in
- let id = notification.object as? Int
- let vc = CommunityVideoListController()
- vc.contentId = id ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- // 查看其他用户中心
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityUser"), object: nil, queue: OperationQueue.main) {
- [weak self] (notification) in
- let uid = notification.object as? Int
-
- if uid != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = uid ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- }
-
- // 内容详情页面
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityContentDetailClosure"), object: nil, queue: OperationQueue.main) {
- [weak self] (notification) in
- let id = notification.object as? Int
- let vc = CommunityRecommendController()
- vc.id = id ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- // 视频播放页面
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityContentVideoClosure"), object: nil, queue: OperationQueue.main) {
- [weak self] (notification) in
- let id = notification.object as? Int
- let vc = CommunityVideoListController()
- vc.contentId = id ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- // banner跳转
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityBannerClosure"), object: nil, queue: OperationQueue.main) {[weak self] (notification) in
- let communityRecommendTypeDataModel = notification.object as? CommunityRecommendTypeDataModel
- let communityBannerType = CommunityBannerType(rawValue: communityRecommendTypeDataModel?.type ?? 0)
- switch communityBannerType {
- case .none?:
- break
- case .content?: //内容
- let postType = PostType(rawValue: communityRecommendTypeDataModel?.postType ?? "image")
- switch postType {
- case .image?:
- let vc = CommunityRecommendController()
- vc.id = communityRecommendTypeDataModel?.linkContentId ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- break
- case .html?:
- let vc = CommunityRecommendController()
- vc.id = communityRecommendTypeDataModel?.linkContentId ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- break
- case .video?:
- let vc = CommunityVideoListController()
- vc.contentId = communityRecommendTypeDataModel?.linkContentId ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- break
- default:
- break
- }
- break
- case .user?: //找朋友
- if communityRecommendTypeDataModel?.linkContentId ?? 0 != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = communityRecommendTypeDataModel?.linkContentId ?? 0
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- break
- case .activity?: //活动
- Mediator.push(H5RouterModuleType.pushActivityId(id: "\(communityRecommendTypeDataModel?.linkContentId ?? 0)"))
- break
- case .topic?: //话题
- Mediator.push(CommunityRouterModuleType.pushFeaturedTopics(id: communityRecommendTypeDataModel?.linkContentId ?? 0))
- break
- default:
- break
-
- }
- }
-
- // 待发布
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("DismissFromPublishEditVc"), object: nil, queue: OperationQueue.main) {[weak self] (notification) in
- self?.reloadSegmentedFollowView()
- self?.navigationBarbgView.isHidden = false
- let progressView = PublishUploadProgressView.publishUploadProgressView(attachedView: (self?.navigationBarbgView)!)
-
- let mediaType = notification.object as? PublishMediaType
- if mediaType == PublishMediaType.video {
- PublishUploadManager.shared().progressView = progressView
- PublishUploadManager.shared().communityVideoUploadAuthApi()
- } else {
- PublishUploadManager.shared().progressView = progressView
- PublishUploadManager.shared().uploadAllImages(totalTimes: 0)
- }
-
- }
-
- }
-
- // 分享
- func share(imgUrl : String?,
- topicStr : String?,
- titleStr : String?,
- avatarStr : String?,
- nameStr : String?,
- H5UrlStr : String?,
- bean : Int?,
- postId : Int?,
- uid : Int?,
- type : String?,
- contentStr : String?) {
- var title = titleStr?.prefix(12)
- if title == nil || title == "" {
- title = contentStr?.prefix(12) ?? ""
- }
- let shareCommunityView = ShareCommunityView.shareCommunityView(
- uid: uid ?? 0, title: "向好友传递美好生活,送TA 500彩虹豆",
- detailTitle: "越多好友看到,收获彩虹豆越多",
- h5Str: (H5UrlStr ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
- thumbnailImg: UIImage.imageUrl(imgUrl),
- sharedTitle: String(title ?? ""),
- sharedDetailTitle: kCommunityPostDetailTitleShare,
- shareCommunityViewType: ShareCommunityViewType.postDetail,
- completion: {
- VirusViewModel.shared.forwarVirueRecordAddApi(postId: postId, postAuthorUid: "\(uid ?? 0)", title: titleStr, content: contentStr, postType: type, postCover: imgUrl)
- })
- shareCommunityView.saveCompletion = {
- [weak self] in
- let vc = CommunityShareContentViewController()
- vc.imgUrl = imgUrl
- vc.topicStr = topicStr ?? ""
- if titleStr == "" || titleStr == nil {
- vc.titleStr =
- String(contentStr?.prefix(20) ?? "")
- }else {
- vc.titleStr = String(titleStr?.prefix(20) ?? "")
-
- }
- vc.avatarStr = avatarStr
- vc.nameStr = nameStr ?? ""
- vc.H5UrlStr = (H5UrlStr ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
- vc.bean = bean ?? 0
- vc.postId = postId
- vc.uid = uid
- vc.type = type
- vc.contentStr = contentStr
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- shareCommunityView.deleteCompletion = {
- [weak self] in
- self?.communityDeleteApi(postId: postId ?? 0)
- }
- }
-
- /// 删除帖子
- func communityDeleteApi(postId:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) {(data) -> (Void) in}
- }
-
- //选中推荐
- func reloadSegmentedFollowView() {
- self.segmentedView.selectItemAt(index: 0)
- self.segmentedView.defaultSelectedIndex = 0
- self.listContainerView.defaultSelectedIndex = 0
- self.segmentedDataSource.reloadData(selectedIndex: 0)
- self.listContainerView.reloadData()
-
- }
-
- //选中推荐
- func reloadSegmentedView() {
- self.segmentedView.selectItemAt(index: 1)
- self.segmentedView.defaultSelectedIndex = 1
- self.listContainerView.defaultSelectedIndex = 1
- self.segmentedDataSource.reloadData(selectedIndex: 1)
- self.listContainerView.reloadData()
- }
- //选中星球
- func reloadSegmentedStarView() {
- self.segmentedView.selectItemAt(index: 2)
- self.segmentedView.defaultSelectedIndex = 2
- self.listContainerView.defaultSelectedIndex = 2
- self.segmentedDataSource.reloadData(selectedIndex: 2)
- self.listContainerView.reloadData()
-
- }
-
- /// navigationBar
- private lazy var navigationBarbgView: UIView = {
- let navigationBarbgView = UIView()
- navigationBarbgView.isHidden = true
- return navigationBarbgView
- }()
- /// navigationBar
- private lazy var communityNavigationBarView: CommunityNavigationBarView = {
- let communityNavigationBarView = CommunityNavigationBarView()
-
- return communityNavigationBarView
- }()
-
- //1.初始化JXSegmentedViewx
- private lazy var segmentedView: JXSegmentedView = {
- let segmentedView = JXSegmentedView()
- segmentedView.delegate = self
- segmentedView.dataSource = segmentedDataSource
- segmentedView.indicators = [indicator]
- segmentedView.contentScrollView = listContainerView.scrollView
- segmentedView.defaultSelectedIndex = 1
- return segmentedView
- }()
-
- //2.初始化dataSource
- private lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
- let segmentedDataSource = JXSegmentedTitleDataSource()
- segmentedDataSource.titles = ["关注","推荐","星球"]
- segmentedDataSource.isTitleColorGradientEnabled = true
- segmentedDataSource.isItemSpacingAverageEnabled = true
- segmentedDataSource.isTitleZoomEnabled = true
- segmentedDataSource.titleNormalColor = kbbbbbbColor
- segmentedDataSource.titleSelectedColor = k333333Color
- segmentedDataSource.titleNormalFont = kRegularFont14!
- segmentedDataSource.titleSelectedFont = kBoldFont22
-
- //reloadData(selectedIndex:)方法一定要调用,方法内部会刷新数据源数组
- segmentedDataSource.reloadData(selectedIndex: 1)
-
- return segmentedDataSource
- }()
- //3.初始化指示器indicator
- private lazy var indicator: JXSegmentedIndicatorLineView = {
- let indicator = JXSegmentedIndicatorLineView()
- indicator.indicatorColor = k333333Color
- indicator.indicatorHeight = 4
- indicator.indicatorWidth = 20
- return indicator
- }()
-
- //4.初始化JXSegmentedListContainerView
- private lazy var listContainerView: JXSegmentedListContainerView = {
- let listContainerView = JXSegmentedListContainerView(dataSource: self)
- listContainerView.didAppearPercent = 0.01
- listContainerView.defaultSelectedIndex = 1
- return listContainerView
- }()
-
- }
- extension CommunityViewController : JXSegmentedViewDelegate {
- //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,而不关心具体是点击还是滚动选中的情况。
- func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
- if index == 0 {
- communityModuleTopType = .communityModuleFollowTop
- }
- if index == 1 {
- communityModuleTopType = .communityModuleRecommendTop
- }
- if index == 2 {
- communityModuleTopType = .communityModulePlanetTop
- }
-
- }
-
- // 点击选中的情况才会调用该方法
- func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {
- //传递didClickSelectedItemAt事件给listContainerView,必须调用!!!
- listContainerView.didClickSelectedItem(at: index)
- }
-
- // 滚动选中的情况才会调用该方法
- func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {
-
- }
-
- // 正在滚动中的回调
- func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {
- //传递scrolling事件给listContainerView,必须调用!!!
- listContainerView.segmentedViewScrolling(from: leftIndex, to: rightIndex, percent: percent, selectedIndex: segmentedView.selectedIndex)
- }
-
- /// 是否允许点击选中目标index的item
- func segmentedView(_ segmentedView: JXSegmentedView, canClickItemAt index: Int) -> Bool {
- return true
- }
- }
- extension CommunityViewController :JXSegmentedListContainerViewDataSource {
-
- func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
- if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
- return titleDataSource.dataSource.count
- }
- return 0
- }
- func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
- if index == 0 {
- let vc = CommunityFollowViewController()
- communityModuleTopType = .communityModuleFollowTop
- return vc
- }else if index == 1 {
- let vc = CommunityRecommnendViewController()
- communityModuleTopType = .communityModuleRecommendTop
- return vc
- }else {
- let vc = CommunityPlanetViewController()
- communityModuleTopType = .communityModulePlanetTop
- return vc
- }
- }
- }
|