Pārlūkot izejas kodu

Merge branch 'dev' of https://git.yishihui.com/iOS/BFRecordScreenKit into dev
合并代码

jsonwang 3 gadi atpakaļ
vecāks
revīzija
b3c1e0fb87

+ 33 - 9
BFRecordScreenKit/Classes/RecordScreen/Controller/BFMusicSearchController.swift

@@ -11,7 +11,7 @@ import BFMediaKit
 import BFCommonKit
 import BFNetRequestKit
 import Alamofire
-
+import MJRefresh
 
 class BFMusicSearchController: BFBaseViewController {
     
@@ -28,6 +28,8 @@ class BFMusicSearchController: BFBaseViewController {
     var searchResults = [PQVoiceModel]()
     
     var currPage = 1
+    
+    var keywork : String?
 
     // 试听音乐
     let player:AVPlayer = {
@@ -68,6 +70,12 @@ class BFMusicSearchController: BFBaseViewController {
         tb.register(BFMusicInfoSearchCell.self, forCellReuseIdentifier: "BFMusicInfoSearchCell")
         tb.tableFooterView = UIView()
         tb.backgroundColor = .clear
+        tb.mj_footer = MJRefreshAutoFooter.init(refreshingBlock: {[weak self] in
+            guard let wself = self else { return }
+            if wself.searchResults.count > 0 {
+                wself.requestSearchMusic()
+            }
+        })
         return tb
     }()
     
@@ -203,8 +211,9 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
                         NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] notice in
                             guard let wself = self else { return }
                             wself.player.currentItem?.seek(to: .zero, completionHandler: nil)
-                            wself.choseCell()?.status = .pause
-                            wself.chosedCellStatu = .pause
+//                            wself.choseCell()?.status = .pause
+//                            wself.chosedCellStatu = .pause
+                            wself.player.play()
                         }
                         
                         cell.status = .loading
@@ -234,9 +243,13 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
     }
     
     // MARK: - 搜素接口
-    func requestSearchMusic(keyword: String, pageNum:Int) {
+    func requestSearchMusic() {
+        if keywork?.count ?? 0 < 1 {
+            cShowHUB(superView: nil, msg: "搜索不能为空")
+            return
+        }
         
-        BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.materialSearchApi + searchBGMMaterialUrl, parames: ["keyWord": keyword, "pageNo": pageNum, "pageSize": 20], commonParams: commonParams(), encoding: JSONEncoding.default, isJsonEncodingNormal: true) { [weak self] response, _, error, _ in
+        BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.materialSearchApi + searchBGMMaterialUrl, parames: ["keyWord": keywork, "pageNo": currPage, "pageSize": 20], commonParams: commonParams(), encoding: JSONEncoding.default, isJsonEncodingNormal: true) { [weak self] response, _, error, _ in
             DispatchQueue.global().async {[weak self] in
                 guard let wself = self else { return }
                 
@@ -257,15 +270,16 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
                         bgmList.append(tempModel)
                     }
                 }
-                if pageNum == 1 {
+                if wself.currPage == 1 {
                     wself.searchResults.removeAll()
                 }
                 wself.searchResults.append(contentsOf: bgmList)
+                wself.currPage += 1
                 DispatchQueue.main.async {[weak self] in
                     guard let wself = self else { return }
                     
                     if wself.searchResults.count > 0 {
-                        wself.musicTb.tableFooterView = UIView()
+//                        wself.musicTb.tableFooterView = UIView()
                     }else{
                         wself.musicTb.tableFooterView = wself.createNoDataView()
                     }
@@ -288,19 +302,29 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
         
         return back
     }
+    
+    func resetRearch() {
+        player.pause()
+        chosedCellStatu = .normal
+        chosedIndexPath = nil
+        currPage = 1
+    }
 }
 
 extension BFMusicSearchController : UITextFieldDelegate {
     
     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         if let keyword = textField.text, keyword.replacingOccurrences(of: " ", with: "", options: .literal, range: nil).count > 0 {
-            currPage = 1
-            requestSearchMusic(keyword: keyword, pageNum: 1)
+            resetRearch()
+            keywork = keyword
+            requestSearchMusic()
         }else{
             cShowHUB(superView: nil, msg: "搜索不能为空")
         }
         
         textField.resignFirstResponder()
+        
+        
         return true
     }
     

+ 1 - 1
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -854,10 +854,10 @@ public class BFRecordScreenController: BFBaseViewController {
                 
         view.addSubview(bottomeView)
 //        view.addSubview(subtitleLabel)
-        view.addSubview(addMusicBtn)
         view.addSubview(choseMusicPanel)
         
         view.addSubview(playBtn)
+        view.addSubview(addMusicBtn)
 //        view.addSubview(avatarView)
 //        view.addSubview(openCameraBtn)
 //        view.addSubview(drawPinBtn)

+ 88 - 51
BFRecordScreenKit/Classes/RecordScreen/View/BFChooseMusicView.swift

@@ -11,12 +11,23 @@ import BFUIKit
 import BFMediaKit
 import BFCommonKit
 import BFNetRequestKit
+import Alamofire
+import MJRefresh
 
 
 enum BFChooseMusicViewClickType {
     case sure, cancle, search
 }
 
+struct MusicCategoryModel {
+    var id : Int64
+    var name: String?
+    var page = 1
+    mutating func changePage() {
+        page += 1
+    }
+}
+
 
 class BFChooseMusicView: UIView {
     
@@ -30,11 +41,11 @@ class BFChooseMusicView: UIView {
         willSet{
             if searchModel != nil {
                 if let catogory = categories.first(where: { mod in
-                    mod.tagName == "热门"
+                    mod.name == "热门"
                 }){
-                    if let tid = catogory.tagId, let _ = musicDic[tid] {
+                    if let _ = musicDic[catogory.id] {
                         // 找到热门的数组
-                        musicDic[tid]?.removeFirst()
+                        musicDic[catogory.id]?.removeFirst()
                     }
                 }
             }
@@ -42,14 +53,14 @@ class BFChooseMusicView: UIView {
         didSet {
             if let searchModel = searchModel {
                 if let catogory = categories.first(where: { mod in
-                    mod.tagName == "热门"
+                    mod.name == "热门"
                 }){
-                    if let tid = catogory.tagId, let list = musicDic[tid] {
+                    if let list = musicDic[catogory.id] {
                         var arr = [PQVoiceModel]()
                         arr.append(searchModel)
                         if list.first?.musicId != searchModel.musicId {
                             arr.append(contentsOf: list)
-                            musicDic[tid] = arr
+                            musicDic[catogory.id] = arr
                             searchModel.isSelected = true
                             chosedMusic?.isSelected = false
                             chosedMusic = searchModel
@@ -64,8 +75,9 @@ class BFChooseMusicView: UIView {
     // 选择的音乐类别
     var categorySelectIndex : Int = 1
     
+    var currPage : Int64 = 1
     // 音乐类别
-    var categories = [PQStuckPointMusicTagsModel]()
+    var categories = [MusicCategoryModel]()
     
     var musicVolume = 0.0 {
         didSet{
@@ -136,8 +148,15 @@ class BFChooseMusicView: UIView {
         tb.dataSource = self
         tb.separatorColor = UIColor.hexColor(hexadecimal: "#272727")
         tb.register(BFMuicInfoCell.self, forCellReuseIdentifier: "BFMuicInfoCell")
-        tb.tableFooterView = UIView()
         tb.backgroundColor = .clear
+//        tb.mj_footer = MJRefreshAutoFooter.init(refreshingBlock: {[weak self] in
+//            guard let wself = self else { return }
+//
+//            let cate = wself.categories[wself.categorySelectIndex]
+//            if (wself.musicDic[cate.id]?.count ?? 0) > 0 {
+//                wself.getMusicsForCategory()
+//            }
+//        })
         return tb
     }()
     
@@ -238,79 +257,95 @@ class BFChooseMusicView: UIView {
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
-    
+
+    //MARK: - 请求数据
     func prepareData() {
-        BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + stuckPointMusicCategoryUrl, parames: ["parentTagId": 0], commonParams: commonParams(), isJsonEncodingNormal: true, timeoutInterval: 15) {[weak self] response, _, error, _ in
+        let categoryListUrl = "produce-center/bgm/app/getAllBgmCates"
+        
+        BFNetRequestAdaptor.postRequestData(url: onlinePQTvApi + categoryListUrl, parames: [:], commonParams: ["baseInfo":commonParams()], encoding: JSONEncoding.default, isJsonEncodingNormal: false, timeoutInterval: 15) {[weak self] response, _, error, _ in
             guard let wself = self else { return }
             
-            var tagsList = Array<PQStuckPointMusicTagsModel>.init()
+            var tagsList = [MusicCategoryModel]()
             if response is NSNull || response == nil {
                 cShowHUB(superView: nil, msg: "音乐分类获取失败")
                 return
             }
             let tagsTempArr = response as? [[String: Any]]
-            if tagsTempArr != nil, (tagsTempArr?.count ?? 0) > 0 {  
+            if tagsTempArr != nil, (tagsTempArr?.count ?? 0) > 0 {
                 for dict in tagsTempArr! {
-                    let tempMusic = PQStuckPointMusicTagsModel(jsonDict: dict)
-                    tempMusic.parentTagId = 0
-                    tagsList.append(tempMusic)
+                    let cid = (dict["cateId"] as? Int64 ?? 0)
+                    if cid > 0 {
+                        let tempMusic = MusicCategoryModel(id: cid, name: dict["cateName"] as? String)
+                        tagsList.append(tempMusic)
+                    }
                 }
             }
-            tagsList.first?.isSelected = true
             
-            let tempMusic = PQStuckPointMusicTagsModel()
-            tempMusic.tagId = -1
+            let tempMusic = MusicCategoryModel(id: -1, name: "")
             tagsList.insert(tempMusic, at: 0)
             wself.categories.append(contentsOf: tagsList)
             wself.categoryView.reloadData()
             if wself.categories.count > 1 {
                 wself.categorySelectIndex = 1
-                if let firstTagid = wself.categories[1].tagId{
-                    wself.getMusicsForCategory(tagId:firstTagid, pageNum: 1)
-                }
+                wself.getMusicsForCategory()
             }
         }
     }
     
-    func getMusicsForCategory(tagId:Int64, pageNum:Int64) {
-        BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + stuckPointMusicPageUrl, parames: ["tagId": tagId, "parentTagId": 0, "pageNum": pageNum, "pageSize": 20], commonParams: commonParams()) {[weak self] response, _, error, _ in
+    func getMusicsForCategory() {
+        let musicListUrl = "produce-center/bgm/pageCateBgm"
+        let cate = categories[categorySelectIndex]
+        BFNetRequestAdaptor.postRequestData(url: onlinePQTvApi + musicListUrl, parames: ["cateId": cate.id, "pageNum": cate.page, "pageSize": 100], commonParams: commonParams(), encoding: JSONEncoding.default) {[weak self] response, _, error, _ in
             guard let wself = self else { return }
             
-            var musicPageList = [PQVoiceModel]()
+            wself.musicTb.mj_footer?.endRefreshing()
+            
             if response is NSNull || response == nil {
                 cShowHUB(superView: nil, msg: "该分类下无音乐")
                 return
             }
             
-            if wself.musicDic[tagId] == nil {
-                wself.musicDic[tagId] = [PQVoiceModel]()
+            if wself.musicDic[cate.id] == nil {
+                wself.musicDic[cate.id] = [PQVoiceModel]()
             }
-            let oldDataMusic : [PQVoiceModel] = wself.musicDic[tagId]!
+
+            var musicPageList = [PQVoiceModel]()
+            let oldDataMusic : [PQVoiceModel] = wself.musicDic[cate.id]!
             
-            if let tempArr = response as? [[String: Any]], tempArr.count > 0 {
-                for (_, dict) in tempArr.enumerated() {
-                    let tempMusic = PQVoiceModel(jsonDict: dict)
-                    tempMusic.cacheTagID = tagId
-                    if tempMusic.endTime <= tempMusic.startTime {
-                        tempMusic.endTime = tempMusic.startTime + 40
+            if let dict = response as? [String: Any] {
+                if let tempArr = dict["objs"] as? [[String: Any]] , tempArr.count > 0 {
+                    for (_, dic) in tempArr.enumerated() {
+                        let tempMusic = PQVoiceModel(jsonDict: dic)
+                        tempMusic.cacheTagID = cate.id
+                        if tempMusic.endTime <= tempMusic.startTime {
+                            tempMusic.endTime = tempMusic.startTime + 40
+                        }
+                        
+                        let haveIndex = oldDataMusic.firstIndex(where: { (music) -> Bool in
+                            music.musicId == tempMusic.musicId
+                        })
+                        if haveIndex == nil {
+                            musicPageList.append(tempMusic)
+                        }
                     }
-
-                    let haveIndex = oldDataMusic.firstIndex(where: { (music) -> Bool in
-                        music.musicId == tempMusic.musicId
-                    })
-                    if haveIndex == nil {
-                        musicPageList.append(tempMusic)
+                }
+                
+                if musicPageList.count > 0 {
+                    if let ind = wself.categories.firstIndex(where: {$0.id == cate.id}) {
+                        wself.categories[ind].changePage()
+                        
+                    }
+                    wself.musicDic[cate.id]!.append(contentsOf: musicPageList)
+                    if cate.id == wself.categories[wself.categorySelectIndex].id{
+                        wself.musicTb.reloadData()
                     }
                 }
-            }
-            
-            if musicPageList.count > 0 {
-                wself.musicDic[tagId]!.append(contentsOf: musicPageList)
-                if tagId == wself.categories[wself.categorySelectIndex].tagId{
-                    wself.musicTb.reloadData()
+                if (dict["totalSize"] as? Int64 ?? 0) <= (dict["offset"] as? Int64 ?? -1) {
+                    // 不再需要数据拉新
                 }
             }
             
+            
         }
     }
     
@@ -384,7 +419,8 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
                 wself.cutActionCallback?(data)
             }
         }
-        if let tagid = categories[categorySelectIndex].tagId, let arr = musicDic[tagid] {
+        let tagid = categories[categorySelectIndex].id
+        if let arr = musicDic[tagid] {
             cell.data = arr[indexPath.row]
             if cell.data?.isSelected ?? false {
                 chosedIndexPath = indexPath
@@ -404,7 +440,7 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
     
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         if categories.count > 1 {
-            return musicDic[categories[categorySelectIndex].tagId ?? 0]?.count ?? 0
+            return musicDic[categories[categorySelectIndex].id]?.count ?? 0
         }
         return 0
     }
@@ -441,8 +477,9 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
                         NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] notice in
                             guard let wself = self else { return }
                             wself.player.currentItem?.seek(to: .zero, completionHandler: nil)
-                            wself.choseCell()?.status = .pause
-                            wself.chosedCellStatu = .pause
+//                            wself.choseCell()?.status = .pause
+//                            wself.chosedCellStatu = .pause
+                            wself.player.play()
                         }
                     }else {
                         cell.status = .playing
@@ -492,7 +529,7 @@ extension BFChooseMusicView : UICollectionViewDelegate, UICollectionViewDataSour
             oldCell?.isChoosed = false
             newCell?.isChoosed = true
             
-            getMusicsForCategory(tagId: categories[categorySelectIndex].tagId ?? 0, pageNum: 1)
+            getMusicsForCategory()
             
         }else {
             cancelChooseMusic()
@@ -508,7 +545,7 @@ extension BFChooseMusicView: BFFlowLayoutDelegate{
         
         var itemWidth = 30.0
         
-        if let title = categories[indexPath.row].tagName, title.count > 0{
+        if let title = categories[indexPath.row].name, title.count > 0{
             let ww = title.textAutoWidth(height: 20, font: UIFont.systemFont(ofSize: 15, weight: .regular))
             itemWidth = max(30.0, ww)
         }

+ 3 - 4
BFRecordScreenKit/Classes/RecordScreen/View/Cell/BFMusicCategoryCell.swift

@@ -82,19 +82,18 @@ class BFMusicCategoryCell: UICollectionViewCell {
         fatalError("init(coder:) has not been implemented")
     }
     
-    func addData(dic:PQStuckPointMusicTagsModel) {
+    func addData(dic:MusicCategoryModel) {
         icon.isHidden = true
         titleL.isHidden = true
-        if let title = dic.tagName, title.count > 0 {
+        if let title = dic.name, title.count > 0 {
             titleL.isHidden = false
             titleL.text = title
             icon.image = nil
-        }else if let tagid = dic.tagId, tagid == -1 {
+        }else if dic.id == -1 {
             icon.isHidden = false
             titleL.text = ""
             icon.image = imageInRecordScreenKit(by: "voiceDeleteS")
         }
     }
-
     
 }