PQStuckPointMusicSearchController.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // PQStuckPointMusicSearchController.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2021/5/6.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import BFCommonKit
  10. class PQStuckPointMusicSearchController: PQStuckPointMusicContentController {
  11. // 选中的总时长
  12. var selectedTotalDuration: Float64 = 0
  13. // 选择的总数
  14. var selectedDataCount: Int = 0
  15. // 选择的图片总数
  16. var selectedImageDataCount: Int = 0
  17. // 当前搜索页数
  18. var pageNum: Int = 1
  19. // 当前搜索关键字
  20. var searchWord: String?
  21. // 当前选中的index
  22. var lastSelectedIndex: IndexPath?
  23. // 热搜数据
  24. var hotList: [Any] = Array<Any>.init()
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. emptyData?.title = "没有搜索到相关音乐"
  28. emptyData?.emptyImage = "pic_search_empty"
  29. emptyRemindView.remindLab.font = UIFont.systemFont(ofSize: 14)
  30. emptyRemindView.remindLab.textColor = UIColor.hexColor(hexadecimal: "#999999")
  31. emptyRemindView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200)
  32. emptyRemindView.emptyData = emptyData
  33. refreshHandle = { [weak self] _, _ in
  34. self?.loadRequestData(isRefresh: false)
  35. }
  36. }
  37. /// 开始搜索
  38. /// - Parameter keyword: 搜索文字
  39. /// - Returns: <#description#>
  40. func loadSearchData(keyword: String?) {
  41. if keyword == nil || (keyword?.count ?? 0) <= 0 {
  42. cShowHUB(superView: nil, msg: "请先输入搜索内容")
  43. return
  44. }
  45. BFLog(message: "背景音乐--开始搜索背景音乐")
  46. if keyword != searchWord || (keyword == searchWord && itemList.count <= 0) {
  47. showEmptyView()
  48. itemList.removeAll()
  49. collectionView.reloadData()
  50. pageNum = 0
  51. searchWord = keyword
  52. // SWNetRequest.cancelTask(url: PQENVUtil.shared.materialSearchApi + searchBGMMaterialUrl)
  53. loadRequestData()
  54. }
  55. }
  56. // 发起搜索请求
  57. @objc func loadRequestData(isRefresh: Bool = true) {
  58. pageNum = pageNum + 1
  59. PQBaseViewModel.searchBGMListData(searchWord, pageNum, 20, videoCount: selectedDataCount - selectedImageDataCount, imageCount: selectedImageDataCount, totalDuration: selectedTotalDuration) { [weak self] bgmList, msg in
  60. // 处理请求数据
  61. BFLog(message: "背景音乐--搜索背景音乐成功")
  62. if bgmList.count <= 0 {
  63. self?.pageNum = (self?.pageNum ?? 0) - 1
  64. }
  65. if bgmList.count <= 0, (self?.itemList.count ?? 0) <= 0, (self?.hotList.count ?? 0) > 0 {
  66. if !(self?.hotList.first is PQEmptyModel){
  67. self?.hotList.insert(PQEmptyModel(), at: 0)
  68. }
  69. self?.configMusicListData(isRefresh: true, musicListData: self?.hotList ?? [])
  70. } else {
  71. self?.configMusicListData(isRefresh: isRefresh, musicListData: bgmList)
  72. }
  73. // 点击上报:用户在搜索框输入文字然后按回车-返回结果后上报
  74. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_searchSyncedUpMusic, pageSource: .sp_stuck_searchSyncedUpMusic, extParams: ["searchText": self?.searchWord ?? "", "searchResultNumber": bgmList.count, "isSuccess": msg == nil], remindmsg: "卡点视频数据上报-(点击上报:用户在搜索框输入文字然后按回车)")
  75. }
  76. }
  77. /// 清空搜索数据
  78. /// - Returns: <#description#>
  79. func clearData() {
  80. BFLog(message: "背景音乐--清空背景音乐搜索数据")
  81. hotList.forEach { item in
  82. if item is PQVoiceModel {
  83. (item as? PQVoiceModel)?.isSelected = false
  84. (item as? PQVoiceModel)?.isPlaying = false
  85. }
  86. }
  87. itemList.removeAll()
  88. searchWord = nil
  89. pageNum = 0
  90. lastSelectedIndex = nil
  91. collectionView.reloadData()
  92. }
  93. }