|
@@ -24,8 +24,31 @@ class BFChooseMusicView: UIView {
|
|
|
var cutActionCallback: ((PQVoiceModel) -> Void)?
|
|
|
|
|
|
// 各类别音乐列表,以类别id为key
|
|
|
- var musicArray = [Int64 : [PQVoiceModel]]()
|
|
|
-
|
|
|
+ var musicDic = [Int64 : [PQVoiceModel]]()
|
|
|
+
|
|
|
+ var searchModel : PQVoiceModel?{
|
|
|
+ didSet {
|
|
|
+ if let searchModel = searchModel {
|
|
|
+ if let catogory = categories.first(where: { mod in
|
|
|
+ mod.tagName == "热门"
|
|
|
+ }){
|
|
|
+ if let tid = catogory.tagId {
|
|
|
+ var arr = [PQVoiceModel]()
|
|
|
+ arr.append(searchModel)
|
|
|
+ if let list = musicDic[tid]{
|
|
|
+ arr.append(contentsOf: list)
|
|
|
+ }
|
|
|
+ musicDic[tid] = arr
|
|
|
+ searchModel.isSelected = true
|
|
|
+ chosedMusic?.isSelected = false
|
|
|
+ chosedMusic = searchModel
|
|
|
+
|
|
|
+ musicTb.reloadData()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
// 选择的音乐类别
|
|
|
var categorySelectIndex : Int = 1
|
|
|
|
|
@@ -42,7 +65,7 @@ class BFChooseMusicView: UIView {
|
|
|
// 选中的音乐
|
|
|
var chosedMusic : PQVoiceModel?
|
|
|
|
|
|
- var chosedCell : BFMuicInfoCell?
|
|
|
+ var chosedIndexPath : IndexPath?
|
|
|
|
|
|
var loadedTimeRangesObserver : NSKeyValueObservation?
|
|
|
|
|
@@ -181,9 +204,9 @@ class BFChooseMusicView: UIView {
|
|
|
guard let wself = self else { return }
|
|
|
|
|
|
guard let ranges = change.newValue as? [CMTimeRange] else { return }
|
|
|
- if let cell = wself.chosedCell, let totalDur = player.currentItem?.duration, totalDur.isValid, CMTimeCompare(ranges.first?.duration ?? .zero, totalDur) >= 0{
|
|
|
+ if let indx = wself.chosedIndexPath, let cell = wself.musicTb.cellForRow(at: indx) as? BFMuicInfoCell, let totalDur = player.currentItem?.duration, totalDur.isValid, CMTimeCompare(ranges.first?.duration ?? .zero, totalDur) >= 0{
|
|
|
if (player.currentItem?.asset as? AVURLAsset)?.url.absoluteString ?? "b" == cell.data?.musicPath ?? "a" {
|
|
|
- if cell.status == .loading{
|
|
|
+ if cell.status != .playing{
|
|
|
cell.status = .playing
|
|
|
}
|
|
|
}
|
|
@@ -243,10 +266,10 @@ class BFChooseMusicView: UIView {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if wself.musicArray[tagId] == nil {
|
|
|
- wself.musicArray[tagId] = [PQVoiceModel]()
|
|
|
+ if wself.musicDic[tagId] == nil {
|
|
|
+ wself.musicDic[tagId] = [PQVoiceModel]()
|
|
|
}
|
|
|
- let oldDataMusic : [PQVoiceModel] = wself.musicArray[tagId]!
|
|
|
+ let oldDataMusic : [PQVoiceModel] = wself.musicDic[tagId]!
|
|
|
|
|
|
if let tempArr = response as? [[String: Any]], tempArr.count > 0 {
|
|
|
for (_, dict) in tempArr.enumerated() {
|
|
@@ -266,7 +289,7 @@ class BFChooseMusicView: UIView {
|
|
|
}
|
|
|
|
|
|
if musicPageList.count > 0 {
|
|
|
- wself.musicArray[tagId]!.append(contentsOf: musicPageList)
|
|
|
+ wself.musicDic[tagId]!.append(contentsOf: musicPageList)
|
|
|
if tagId == wself.categories[wself.categorySelectIndex].tagId{
|
|
|
wself.musicTb.reloadData()
|
|
|
}
|
|
@@ -297,11 +320,18 @@ class BFChooseMusicView: UIView {
|
|
|
categoryView.reloadData()
|
|
|
}
|
|
|
|
|
|
+ func choseCell() -> BFMuicInfoCell? {
|
|
|
+ if let indpx = chosedIndexPath, let cell = musicTb.cellForRow(at: indpx) as? BFMuicInfoCell {
|
|
|
+ return cell
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
|
|
|
@objc func btnAction(btn:UIButton) {
|
|
|
|
|
|
player.pause()
|
|
|
- chosedCell?.status = .pause
|
|
|
+
|
|
|
+ choseCell()?.status = .pause
|
|
|
|
|
|
switch btn.tag{
|
|
|
case 1001:
|
|
@@ -322,7 +352,7 @@ class BFChooseMusicView: UIView {
|
|
|
func cancelChooseMusic() {
|
|
|
chosedMusic = nil
|
|
|
player.pause()
|
|
|
- chosedCell?.changeSelected(false)
|
|
|
+ choseCell()?.changeSelected(false)
|
|
|
}
|
|
|
|
|
|
@objc func dimiss(){
|
|
@@ -341,8 +371,14 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
|
|
|
wself.cutActionCallback?(data)
|
|
|
}
|
|
|
}
|
|
|
- if let tagid = categories[categorySelectIndex].tagId, let arr = musicArray[tagid] {
|
|
|
+ if let tagid = categories[categorySelectIndex].tagId, let arr = musicDic[tagid] {
|
|
|
cell.data = arr[indexPath.row]
|
|
|
+ if cell.data?.isSelected ?? false {
|
|
|
+ chosedIndexPath = indexPath
|
|
|
+ chosedMusic = cell.data
|
|
|
+ }else{
|
|
|
+ cell.status = .normal
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return cell
|
|
@@ -354,19 +390,19 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
|
|
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
if categories.count > 1 {
|
|
|
- return musicArray[categories[categorySelectIndex].tagId ?? 0]?.count ?? 0
|
|
|
+ return musicDic[categories[categorySelectIndex].tagId ?? 0]?.count ?? 0
|
|
|
}
|
|
|
return 0
|
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
if let cell = tableView.cellForRow(at: indexPath) as? BFMuicInfoCell {
|
|
|
-
|
|
|
+ chosedMusic?.isSelected = false
|
|
|
chosedMusic = cell.data
|
|
|
chosedMusic?.isSelected = true
|
|
|
cell.changeSelected(true)
|
|
|
|
|
|
- chosedCell = cell
|
|
|
+ chosedIndexPath = indexPath
|
|
|
|
|
|
if cell.status == .normal {
|
|
|
if let urlStr = cell.data?.musicPath, let url = URL(string: urlStr){
|
|
@@ -393,7 +429,7 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
|
|
|
cell?.status = .normal
|
|
|
cell?.changeSelected(false)
|
|
|
|
|
|
- chosedCell = nil
|
|
|
+ chosedIndexPath = nil
|
|
|
player.pause()
|
|
|
}
|
|
|
}
|