PQStuckPointViewModel.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // PQStuckPointViewModel.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2021/4/28.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import BFCommonKit
  10. import BFNetRequestKit
  11. import BFMediaKit
  12. import BFAnalyzeKit
  13. import BFUIKit
  14. public class PQStuckPointViewModel: NSObject {
  15. /// 获取卡点音乐分类列表
  16. /// - Parameters:
  17. /// 通过 parentTagId 来区分层级,第一级会带上 '热门' 标签音乐列表,获取第二级标签列表会带上所选第二级的 '全部' 标签音乐列表
  18. /// - parentTagId: 标签父级 ID(默认 0 第一层)
  19. /// - pageSize: 音乐列表每页个数(默认 20)
  20. /// - complateHandle: <#complateHandle description#>
  21. class func stuckPointMusicCategoryList(parentTagId: Int64 = 0, pageSize _: Int = 20, complateHandle: @escaping (_ categoryList: [PQStuckPointMusicTagsModel], _ msg: String?, _ tagAttributes: ([UICollectionViewLayoutAttributes], CGFloat)?) -> Void) {
  22. BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + stuckPointMusicCategoryUrl, parames: ["parentTagId": parentTagId], commonParams: commonParams()) { response, _, error, _ in
  23. var tagsList = Array<PQStuckPointMusicTagsModel>.init()
  24. if response is NSNull || response == nil {
  25. complateHandle(tagsList, error?.msg, nil)
  26. return
  27. }
  28. let tagsTempArr = response as? [[String: Any]]
  29. if tagsTempArr != nil, (tagsTempArr?.count ?? 0) > 0 {
  30. for dict in tagsTempArr! {
  31. let tempMusic = PQStuckPointMusicTagsModel(jsonDict: dict)
  32. tempMusic.parentTagId = parentTagId
  33. tagsList.append(tempMusic)
  34. }
  35. }
  36. tagsList.first?.isSelected = true
  37. complateHandle(tagsList, nil, (parentTagId != 0) ? PQStuckPointViewModel.dealWithAttributesWithTags(tagsList: tagsList) : nil)
  38. }
  39. }
  40. /// 计算标签frame
  41. /// - Parameter tagsList: <#tagsList description#>
  42. /// - Returns: <#description#>
  43. class func dealWithAttributesWithTags(tagsList: [PQStuckPointMusicTagsModel]) -> ([UICollectionViewLayoutAttributes], CGFloat) {
  44. var attributesArray = [UICollectionViewLayoutAttributes]()
  45. // 遍历数据计算每个item的属性并布局
  46. let lineSpace: CGFloat = 8
  47. let itemSpace: CGFloat = 8
  48. var currtentX: CGFloat = 0
  49. var currtentY: CGFloat = 0
  50. let itemHeight: CGFloat = cDefaultMargin * 3
  51. let maxWidth: CGFloat = cScreenWidth - cDefaultMargin * 12 - 32
  52. for (index, data) in tagsList.enumerated() {
  53. let indexPath = IndexPath(item: index, section: 0)
  54. let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
  55. var width: CGFloat = sizeWithText(text: " \(data.tagName ?? "") ", font: UIFont.systemFont(ofSize: 12), size: CGSize(width: maxWidth, height: itemHeight)).width
  56. if width > maxWidth {
  57. width = maxWidth
  58. }
  59. if (currtentX + width + itemSpace) > maxWidth {
  60. currtentY = currtentY + lineSpace + itemHeight
  61. currtentX = 0
  62. }
  63. attributes.frame = CGRect(x: currtentX, y: currtentY, width: width, height: itemHeight)
  64. currtentX = currtentX + itemSpace + width
  65. attributesArray.append(attributes)
  66. }
  67. return (attributesArray, attributesArray.count > 0 ? currtentY + lineSpace + itemHeight : 0)
  68. }
  69. /// 卡点音乐某个分类下列表
  70. /// - Parameters:
  71. /// - tagId: 标签 ID
  72. /// - parentTagId: 标签父级 ID
  73. /// - pageNum: 音乐列表页码(默认 1)
  74. /// - pageSize: 音乐列表每页个数(默认 20)
  75. /// - videoCount: 选择视频素材个数
  76. /// - imageCount: 选择图片素材个数
  77. /// - totalDuration: 选择素材总时长
  78. /// - complateHandle: <#complateHandle description#>
  79. /// - Returns: <#description#>
  80. class func stuckPointMusicPageList(tagId: Int64, parentTagId: Int64 = 0, pageNum: Int = 1, pageSize: Int = 20,videoCount: Int = 0, imageCount: Int = 0, totalDuration: Float64 = 0, oldDataMusic:[PQVoiceModel]? = nil, complateHandle: @escaping (_ musicPageList: [PQVoiceModel], _ msg: String?) -> Void) {
  81. BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + stuckPointMusicPageUrl, parames: ["tagId": tagId, "parentTagId": parentTagId, "pageNum": pageNum, "pageSize": pageSize], commonParams: commonParams()) { response, _, error, _ in
  82. var musicPageList = Array<PQVoiceModel>.init()
  83. if response is NSNull || response == nil {
  84. complateHandle(musicPageList, error?.msg)
  85. return
  86. }
  87. let tempArr = response as? [[String: Any]]
  88. if tempArr != nil, (tempArr?.count ?? 0) > 0 {
  89. for (_,dict) in tempArr!.enumerated() {
  90. let tempMusic = PQVoiceModel(jsonDict: dict)
  91. tempMusic.cacheTagID = tagId
  92. if tempMusic.rhythmSdata.count > 0 && (videoCount > 0 || imageCount > 0 || totalDuration > 0) {
  93. // tempMusic.endTime = tempMusic.startTime + tempMusic.stuckPointCuttingTime(videoCount: videoCount, imageCount: imageCount, totalDuration: totalDuration)
  94. BFLog(message: "music:\(tempMusic.musicName ?? ""),\(tempMusic.startTime),\(tempMusic.endTime)")
  95. }
  96. if tempMusic.endTime <= tempMusic.startTime {
  97. tempMusic.endTime = tempMusic.startTime + 40
  98. }
  99. let haveIndex = oldDataMusic?.firstIndex(where: { (music) -> Bool in
  100. (music.musicId == tempMusic.musicId)
  101. })
  102. if(haveIndex == nil){
  103. musicPageList.append(tempMusic)
  104. }
  105. }
  106. }
  107. complateHandle(musicPageList, nil)
  108. }
  109. }
  110. /// 获取某个音乐的卡点数据
  111. /// - Parameters:
  112. /// - musicId: <#musicId description#>
  113. /// - originType:音乐来源:1 上传 2 爬取
  114. /// - complateHandle: <#complateHandle description#>
  115. /// - Returns: description
  116. class func stuckPointMusicDetailData(musicId: String, originType: Int, complateHandle: @escaping (_ musicDetaiData: PQVoiceModel?, _ msg: String?) -> Void) {
  117. BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + stuckPointMusicDetailUrl, parames: ["musicId": musicId, "originType": originType], commonParams: commonParams()) { response, _, error, _ in
  118. if response is NSNull || response == nil {
  119. complateHandle(nil, error?.msg)
  120. return
  121. }
  122. let tempDict = (response as? [String: Any])
  123. if tempDict != nil, (tempDict?.keys.count ?? 0) > 0 {
  124. complateHandle(PQVoiceModel(jsonDict: tempDict!), nil)
  125. } else {
  126. complateHandle(nil, nil)
  127. }
  128. }
  129. }
  130. /// 请求再创作项目信息
  131. /// - Parameter projectId: 项目id
  132. /// - Returns: <#description#>
  133. class public func stuckPointProjectMusicInfo(projectId: String, complateHandle: @escaping (_ musicDetaiData: PQVoiceModel?, _ msg: String?) -> Void) {
  134. BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + stuckPointProjectMusicInfoUrl, parames: ["projectId": projectId], commonParams: commonParams()) { response, _, error, _ in
  135. if response is NSNull || response == nil {
  136. complateHandle(nil, error?.msg)
  137. return
  138. }
  139. let tempDict = (response as? [String: Any])
  140. if tempDict != nil, tempDict?.keys.contains("rhythmMusicData") ?? false {
  141. let musicData: PQVoiceModel = PQVoiceModel(jsonDict: tempDict?["rhythmMusicData"] as? [String: Any] ?? [:])
  142. if tempDict?.keys.contains("rhythmMusicIn") ?? false {
  143. musicData.rhythmMusicIn = (Float64("\(tempDict?["rhythmMusicIn"] ?? "0")") ?? 0) / 1_000_000
  144. }
  145. if tempDict?.keys.contains("rhythmMusicOut") ?? false {
  146. musicData.rhythmMusicOut = (Float64("\(tempDict?["rhythmMusicOut"] ?? "0")") ?? 0) / 1_000_000
  147. }
  148. if (tempDict?.keys.contains("rhythmMusicSpeed") ?? false) && "\(tempDict?["rhythmMusicSpeed"] ?? "")" != "<null>" {
  149. musicData.speed = Int("\(tempDict?["rhythmMusicSpeed"] ?? "2")") ?? 2
  150. }
  151. musicData.originProjectId = projectId
  152. complateHandle(musicData, nil)
  153. } else {
  154. complateHandle(nil, nil)
  155. }
  156. }
  157. }
  158. }