123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- //
- // CommunityViewController.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/6/11.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 社区首页
- import UIKit
- import JXSegmentedView
- import SwiftyMediator
- import Kingfisher
- var communityNavigationBarIsHidden: Bool = false
- var communityIndex: Int = 1
- enum CommunityModuleTopType : String {
- case communityModuleFollowTop = "CommunityModuleFollowTop"
- case communityModuleRecommendTop = "CommunityModuleRecommendTop"
- case communityModulePlanetTop = "CommunityModulePlanetTop"
- }
- var communityModuleTopType :CommunityModuleTopType?
- class CommunityViewController: BaseViewController {
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- //KingfisherManager.shared.cache.clearDiskCache()
- KingfisherManager.shared.cache.clearMemoryCache()
- }
-
- deinit {
- NXLLog("deinit")
- if observe != nil {
- NotificationCenter.default.removeObserver(observe!)
- }
- }
- weak var observe : NSObjectProtocol?
-
- var communityRecommendCategorysModel: CommunityRecommendCategorysModel?
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- self.view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-kTabBarTotalHeight)
- self.view.layoutIfNeeded()
-
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- self.view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-kTabBarTotalHeight)
- self.view.layoutIfNeeded()
- }
-
- 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)
- // view.addSubview(linebgImageView)
-
- }
-
- override func setupLayouts() {
- 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(44)
- make.top.equalTo(kNavBarTotalHeight)
- }
- listContainerView.snp.makeConstraints { (make) in
- make.top.equalTo(segmentedView.snp.bottom)
- make.left.right.bottom.equalToSuperview()
- }
-
- navigationBarbgView.snp.makeConstraints { (make) in
- make.top.equalTo(segmentedView.snp.bottom)
- make.left.right.equalToSuperview()
- make.height.equalTo(62)
- }
- // linebgImageView.snp.makeConstraints { (make) in
- // make.top.equalTo(segmentedView.snp.bottom)
- // make.left.right.equalToSuperview()
- // make.height.equalTo(20)
- // }
- }
-
- override func setupData() {
-
- communityPostCategoryApi()
- //搜索
- communityNavigationBarView.searchClosure = {
- [weak self] in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- let vc = SearchViewController()
- vc.searchType = .content
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- }
- //U米
- communityNavigationBarView.rightButtonClosure = {
- [weak self] in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- self?.tabBarController?.selectedIndex = 1
- }
- }
-
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("login"), object: nil, queue: OperationQueue.main, using: {
- [weak self] (notification) in
- self?.reloadSegmentedView()
- })
-
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("loginOut"), object: nil, queue: OperationQueue.main, using: {
- [weak self] (notification) in
- self?.reloadSegmentedView()
-
- })
-
- // 找朋友 查看全部推荐关注
- 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?.username,
- 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
- vc.departureVc = .hotVideos
- 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("DismissFromPublishEditVc"), object: nil, queue: OperationQueue.main) {[weak self] (notification) in
- self?.reloadSegmentedFollowView()
- let aliyunVodUpToYoPublishModel = notification.object as? AliyunVodUpToYoPublishModel
- AliyunVodUpToyoPublishManager.shared.publish(aliyunVodUpToYoPublishModel: aliyunVodUpToYoPublishModel, view: self?.navigationBarbgView)
-
- }
-
- // 滚动
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("CommunityViewControllerScrollView"), object: nil, queue: OperationQueue.main) {[weak self] (notification) in
- let navigationBarIsHidden = notification.object as? Bool
-
- if communityNavigationBarIsHidden { //隐藏
- if !(navigationBarIsHidden!) {
- self?.communityNavigationBarView.snp.updateConstraints { (make) in
- make.top.equalTo(kSafeStatusBarHeight)
- }
- self?.segmentedView.snp.updateConstraints { (make) in
- make.top.equalTo(kNavBarTotalHeight)
- }
- }
- }else {
- if (navigationBarIsHidden!) {
- self?.communityNavigationBarView.snp.updateConstraints { (make) in
- make.top.equalTo(-kNavBarTotalHeight)
- }
- self?.segmentedView.snp.updateConstraints { (make) in
- make.top.equalTo(kSafeStatusBarHeight)
- }
- }
- }
- communityNavigationBarIsHidden = navigationBarIsHidden!
- UIView.animate(withDuration: 0.15, animations: { [weak self] in
- self?.view.layoutIfNeeded()
- self?.listContainerView.layoutSubviews()
- }) { (complete) in}
- }
-
- }
-
- // 分享
- 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
- if title == nil || title == "" {
- title = contentStr ?? ""
- }
- let shareCommunityView = ShareCommunityView.shareCommunityView(
- postId: postId ?? 0, uid: uid ?? 0,
- h5Str: (H5UrlStr ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
- thumbnailImg: UIImage.imageUrl(imgUrl),
- sharedTitle: String(title ?? ""),
- sharedDetailTitle: kCommunityPostShareDetailTitle,
- 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
- 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.indicators = [indicator]
- segmentedView.contentScrollView = listContainerView.scrollView
- segmentedView.defaultSelectedIndex = 1
- segmentedView.contentEdgeInsetLeft = 14
- segmentedView.contentEdgeInsetRight = 14
- return segmentedView
- }()
-
- //2.初始化dataSource
- private lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
- let segmentedDataSource = JXSegmentedTitleDataSource()
- segmentedDataSource.isTitleColorGradientEnabled = true
- segmentedDataSource.isItemSpacingAverageEnabled = false
- segmentedDataSource.isTitleZoomEnabled = true
- segmentedDataSource.titleNormalColor = kbbbbbbColor
- segmentedDataSource.titleSelectedColor = k333333Color
- segmentedDataSource.titleNormalFont = kRegularFont15!
- segmentedDataSource.titleSelectedFont = kRegularFont15
- segmentedDataSource.titleSelectedZoomScale = 1.13
- segmentedDataSource.isTitleStrokeWidthEnabled = true
-
- //reloadData(selectedIndex:)方法一定要调用,方法内部会刷新数据源数组
- segmentedDataSource.reloadData(selectedIndex: 1)
-
- return segmentedDataSource
- }()
-
- /// 刷新数据
- func reloadDataDefault() {
- self.setupViews()
- self.setupLayouts()
- segmentedDataSource.titles = ["关注","推荐"]
- segmentedView.dataSource = segmentedDataSource
- listContainerView.defaultSelectedIndex = 0
- segmentedView.defaultSelectedIndex = 0
- segmentedDataSource.reloadData(selectedIndex: 0)
- listContainerView.reloadData()
- }
-
- //3.初始化指示器indicator
- private lazy var indicator: JXSegmentedIndicatorLineView = {
- let indicator = JXSegmentedIndicatorLineView()
- indicator.indicatorColor = kThemeColor
- indicator.indicatorHeight = 3
- indicator.indicatorWidth = 14
- return indicator
- }()
-
- //4.初始化JXSegmentedListContainerView
- private lazy var listContainerView: JXSegmentedListContainerView = {
- let listContainerView = JXSegmentedListContainerView(dataSource: self)
- listContainerView.didAppearPercent = 0.01
- listContainerView.defaultSelectedIndex = 1
- return listContainerView
- }()
-
- lazy var linebgImageView: UIImageView = {
- let linebgImageView = UIImageView()
- linebgImageView.image = kImage(name: "navbar_shadow_pic_up")
- return linebgImageView
- }()
-
- var index : Int?
-
- /// 推荐分类
- func communityPostCategoryApi() {
- SwiftMoyaNetWorkServiceCommunity.shared().communityPostCategoryApi(completion: { [weak self] (communityRecommendCategorysModel) -> (Void) in
- self?.communityRecommendCategorysModel = communityRecommendCategorysModel as? CommunityRecommendCategorysModel
- self?.communityRecommendCategorysModel?.categoryListName?.insert("关注", at: 0)
- self?.segmentedDataSource.titles = self?.communityRecommendCategorysModel?.categoryListName ?? ["关注","推荐"]
- self?.segmentedView.dataSource = self?.segmentedDataSource
-
- self?.listContainerView.defaultSelectedIndex = 1
- self?.segmentedView.defaultSelectedIndex = 1
- self?.segmentedDataSource.reloadData(selectedIndex: 1)
- self?.listContainerView.reloadData()
- self?.segmentedView.reloadData()
- // self?.reloadSegmentedView()
- }) { [weak self] (loadingStatus) in
- self?.reloadDataDefault()
- }
- }
-
- }
- 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
- // if (communityNavigationBarIsHidden) {
- // self.communityNavigationBarView.snp.updateConstraints { (make) in
- // make.top.equalTo(kSafeStatusBarHeight)
- // }
- // self.segmentedView.snp.updateConstraints { (make) in
- // make.top.equalTo(kNavBarTotalHeight)
- // }
- // communityNavigationBarIsHidden = false
- // UIView.animate(withDuration: 0.15, animations: { [weak self] in
- // self?.view.layoutIfNeeded()
- // self?.listContainerView.layoutSubviews()
- // }) { (complete) in}
- // }
- // }
- // communityIndex = index
- listContainerView.didClickSelectedItem(at: index)
- // self.index = index
-
- }
-
- // 点击选中的情况才会调用该方法
- func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {
- //传递didClickSelectedItemAt事件给listContainerView,必须调用!!!
- }
-
- // 滚动选中的情况才会调用该方法
- 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 {
- let vc = CommunityRecommnendViewController()
- vc.communityRecommendCategoryModel = communityRecommendCategorysModel?.categoryList?[index-1]
- communityModuleTopType = .communityModuleRecommendTop
- return vc
- }
- }
- }
|