|
@@ -9,18 +9,28 @@ import Foundation
|
|
|
import BFUIKit
|
|
|
import BFMediaKit
|
|
|
import BFCommonKit
|
|
|
+import BFNetRequestKit
|
|
|
+import Alamofire
|
|
|
+
|
|
|
|
|
|
class BFMusicSearchController: BFBaseViewController {
|
|
|
|
|
|
var choseAction : ((PQVoiceModel?) -> Void)?
|
|
|
|
|
|
+ var cutActionCallback: ((PQVoiceModel) -> Void)?
|
|
|
+
|
|
|
var loadedTimeRangesObserver : NSKeyValueObservation?
|
|
|
|
|
|
- var chosedCell : BFMuicInfoCell?
|
|
|
+ var chosedCell : BFMusicInfoSearchCell?
|
|
|
+
|
|
|
+ var searchResults = [PQVoiceModel]()
|
|
|
+
|
|
|
+ var currPage = 1
|
|
|
|
|
|
// 试听音乐
|
|
|
let player:AVPlayer = {
|
|
|
let p = AVPlayer(playerItem: nil)
|
|
|
+
|
|
|
return p
|
|
|
}()
|
|
|
|
|
@@ -40,19 +50,31 @@ class BFMusicSearchController: BFBaseViewController {
|
|
|
|
|
|
let closeBtn = UIButton()
|
|
|
closeBtn.setImage(imageInRecordScreenKit(by: "search_close"), for: .normal)
|
|
|
- closeBtn.frame = CGRect(x: 18, y: statusBarHeight + 2, width: 40, height: 40)
|
|
|
+ closeBtn.frame = CGRect(x: 12, y: statusBarHeight + 2, width: 40, height: 40)
|
|
|
+ closeBtn.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
|
|
|
view.addSubview(closeBtn)
|
|
|
|
|
|
- let searBar = UISearchBar(frame: CGRect(x: 62, y: statusBarHeight + 4, width: cScreenWidth - 62 - 18, height: 36))
|
|
|
- searBar.barStyle = .black
|
|
|
- searBar.prompt = "歌名/歌手名"
|
|
|
-
|
|
|
- view.addSubview(searBar)
|
|
|
-
|
|
|
+ let searchTF = UITextField(frame: CGRect(x: 62, y: statusBarHeight + 4, width: cScreenWidth - 62 - 18, height: 36))
|
|
|
+ let leftv = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
|
|
|
+ let iv = UIImageView(frame: CGRect(x: 6, y: 0, width: 20, height: 20))
|
|
|
+ iv.contentMode = .scaleAspectFit
|
|
|
+ iv.image = imageInRecordScreenKit(by: "search")
|
|
|
+ leftv.addSubview(iv)
|
|
|
+ searchTF.leftView = leftv
|
|
|
+ searchTF.leftViewMode = .always
|
|
|
+ searchTF.clearButtonMode = .whileEditing
|
|
|
+ searchTF.placeholder = "歌名/歌手名"
|
|
|
+ searchTF.delegate = self
|
|
|
+ searchTF.returnKeyType = .search
|
|
|
+ searchTF.textColor = UIColor.white
|
|
|
+ searchTF.backgroundColor = UIColor.hexColor(hexadecimal: "#1d1d1d")
|
|
|
+ searchTF.layer.cornerRadius = 18
|
|
|
+ view.addSubview(searchTF)
|
|
|
+
|
|
|
view.addSubview(musicTb)
|
|
|
musicTb.snp.makeConstraints { make in
|
|
|
make.left.right.bottom.equalToSuperview()
|
|
|
- make.top.equalTo(searBar.snp.bottom).offset(4)
|
|
|
+ make.top.equalTo(searchTF.snp.bottom).offset(4)
|
|
|
}
|
|
|
|
|
|
loadedTimeRangesObserver = player.observe(\AVPlayer.currentItem?.loadedTimeRanges, options: [.new, .initial]) { [weak self] (player, change) in
|
|
@@ -73,24 +95,41 @@ class BFMusicSearchController: BFBaseViewController {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @objc func closeAction(){
|
|
|
+ self.dismiss(animated: true, completion: nil)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
- return 4
|
|
|
+ return searchResults.count
|
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "BFMusicInfoSearchCell")
|
|
|
|
|
|
if let cell = cell as? BFMusicInfoSearchCell{
|
|
|
+ cell.data = searchResults[indexPath.row]
|
|
|
+
|
|
|
cell.useCallback = {[weak self] in
|
|
|
guard let wself = self else { return }
|
|
|
wself.choseAction?(cell.data)
|
|
|
wself.dismiss(animated: true, completion: nil)
|
|
|
}
|
|
|
+ cell.cutCallBack = {[weak self] in
|
|
|
+ guard let wself = self else { return }
|
|
|
+ if let data = cell.data {
|
|
|
+ wself.dismiss(animated: true) {[weak self] in
|
|
|
+ guard let wself = self else { return }
|
|
|
+ wself.cutActionCallback?(data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ cell?.selectionStyle = .none
|
|
|
+
|
|
|
return cell!
|
|
|
}
|
|
|
|
|
@@ -99,10 +138,9 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
- if let cell = tableView.cellForRow(at: indexPath) as? BFMuicInfoCell {
|
|
|
-//
|
|
|
-// chosedMusic = cell.data
|
|
|
-// chosedMusic?.isSelected = true
|
|
|
+
|
|
|
+ if let cell = tableView.cellForRow(at: indexPath) as? BFMusicInfoSearchCell {
|
|
|
+
|
|
|
cell.changeSelected(true)
|
|
|
|
|
|
chosedCell = cell
|
|
@@ -128,11 +166,83 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
|
|
|
- let cell = tableView.cellForRow(at: indexPath) as? BFMuicInfoCell
|
|
|
+ let cell = tableView.cellForRow(at: indexPath) as? BFMusicInfoSearchCell
|
|
|
cell?.status = .normal
|
|
|
cell?.changeSelected(false)
|
|
|
|
|
|
chosedCell = nil
|
|
|
player.pause()
|
|
|
}
|
|
|
+
|
|
|
+ // MARK: - 搜素接口
|
|
|
+ func requestSearchMusic(keyword: String, pageNum:Int) {
|
|
|
+
|
|
|
+ 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
|
|
|
+ DispatchQueue.global().async {[weak self] in
|
|
|
+ guard let wself = self else { return }
|
|
|
+
|
|
|
+ var bgmList = [PQVoiceModel]()
|
|
|
+ if response is NSNull || response == nil {
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ cShowHUB(superView: nil, msg: "搜索失败,请重试!")
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let tempArr = (response as? [String: Any])?["entityList"] as? [[String: Any]]
|
|
|
+ if tempArr != nil {
|
|
|
+ for item in tempArr! {
|
|
|
+ let tempModel = PQVoiceModel(jsonDict: item)
|
|
|
+ tempModel.volume = 30
|
|
|
+ tempModel.voiceType = VOICETYPT.BGM.rawValue
|
|
|
+ bgmList.append(tempModel)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if pageNum == 1 {
|
|
|
+ wself.searchResults.removeAll()
|
|
|
+ }
|
|
|
+ wself.searchResults.append(contentsOf: bgmList)
|
|
|
+ DispatchQueue.main.async {[weak self] in
|
|
|
+ guard let wself = self else { return }
|
|
|
+
|
|
|
+ if wself.searchResults.count > 0 {
|
|
|
+ wself.musicTb.tableFooterView = UIView()
|
|
|
+ }else{
|
|
|
+ wself.musicTb.tableFooterView = wself.createNoDataView()
|
|
|
+ }
|
|
|
+ wself.musicTb.reloadData()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func createNoDataView() -> UIView {
|
|
|
+ let back = UIView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: 200))
|
|
|
+
|
|
|
+ let l = UILabel(frame: CGRect(x: 40, y: 60, width: cScreenWidth - 80, height: 80))
|
|
|
+ l.text = "没有搜索到相关音乐\n换个词再试试吧"
|
|
|
+ l.textColor = UIColor.hexColor(hexadecimal: "#787878")
|
|
|
+ l.textAlignment = .center
|
|
|
+ l.numberOfLines = 2
|
|
|
+ l.font = UIFont.systemFont(ofSize: 17)
|
|
|
+ back.addSubview(l)
|
|
|
+
|
|
|
+ return back
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+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)
|
|
|
+ }else{
|
|
|
+ cShowHUB(superView: nil, msg: "搜索不能为空")
|
|
|
+ }
|
|
|
+
|
|
|
+ textField.resignFirstResponder()
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|