PQStuckPointMaterialController.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // PQStuckPointMaterialController.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2021/4/26.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import Photos
  10. import BFCommonKit
  11. import BFMaterialKit
  12. public class PQStuckPointMaterialController: PQBaseViewController {
  13. public var isToPublicHandle:((_ isReCreate:Bool,_ selectedTotalDuration: Float64,_ selectedDataCount:Int,_ selectedImageDataCount: Int,_ mStickers: [PQEditVisionTrackMaterialsModel]?,_ stuckPointMusicData: PQVoiceModel?,_ editProjectModel: PQEditProjectModel?) -> Void)?
  14. // 按钮高
  15. let choseBtnH: CGFloat = cDefaultMargin * 3
  16. // 按钮宽
  17. let choseBtnW: CGFloat = cDefaultMargin * 5
  18. // 操作视图高度
  19. let topImageH: CGFloat = 76
  20. // 底部操作视图高度
  21. let bottomH: CGFloat = cDefaultMargin * 5
  22. // 间隔
  23. let margin: CGFloat = 12
  24. // 选择的总数
  25. var selectedDataCount: Int = 0
  26. // 选择的图片总数
  27. var selectedImageDataCount: Int = 0
  28. // 再创作音乐数据
  29. public var reCreateMusicData: PQVoiceModel?
  30. lazy var changeCollecBtn: UIButton = {
  31. let changeCollecBtn = UIButton(frame: CGRect(x: cDefaultMargin * 5, y: cDevice_iPhoneStatusBarHei, width: cScreenWidth - cDefaultMargin * 10, height: cDefaultMargin * 4))
  32. changeCollecBtn.titleLabel?.lineBreakMode = .byTruncatingTail
  33. changeCollecBtn.tintColor = PQBFConfig.shared.styleTitleColor
  34. changeCollecBtn.setTitle("全部", for: .normal)
  35. changeCollecBtn.setImage(UIImage.moduleImage(named: "icon_selected_down", moduleName: "BFFramework",isAssets: false)?.withRenderingMode(.alwaysTemplate), for: .normal)
  36. changeCollecBtn.setImage(UIImage.moduleImage(named: "icon_selected_up", moduleName: "BFFramework",isAssets: false)?.withRenderingMode(.alwaysTemplate), for: .selected)
  37. changeCollecBtn.setTitleColor(PQBFConfig.shared.styleTitleColor, for: .normal)
  38. changeCollecBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
  39. changeCollecBtn.tag = 1
  40. changeCollecBtn.imagePosition(at: .right, space: cDefaultMargin / 2)
  41. changeCollecBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  42. return changeCollecBtn
  43. }()
  44. lazy var albumController: BFPhotoAlbumController = {
  45. let albumController = BFPhotoAlbumController()
  46. albumController.isTopShow = true
  47. albumController.categoryH = cDefaultMargin * 40
  48. addChild(albumController)
  49. view.insertSubview(albumController.view, belowSubview: navHeadImageView!)
  50. albumController.updateViewFrame(frame: CGRect(x: 0, y: navHeadImageView?.frame.maxY ?? cDevice_iPhoneNavBarAndStatusBarHei, width: view.frame.width, height: view.frame.height - (navHeadImageView?.frame.maxY ?? cDevice_iPhoneNavBarAndStatusBarHei)))
  51. albumController.selectedHandle = { [weak self] seletedData in
  52. self?.albumSelectedHandle(seletedData: seletedData)
  53. }
  54. return albumController
  55. }()
  56. lazy var choseLocalAllBtn: UIButton = {
  57. let choseLocalAllBtn = UIButton(frame: CGRect(x: (view.frame.width - choseBtnW * 3) / 4, y: cDevice_iPhoneNavBarAndStatusBarHei + margin, width: choseBtnW, height: choseBtnH))
  58. choseLocalAllBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#999999"), for: .normal)
  59. choseLocalAllBtn.setTitleColor(PQBFConfig.shared.styleTitleColor, for: .selected)
  60. choseLocalAllBtn.setTitle("全部", for: .normal)
  61. choseLocalAllBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  62. choseLocalAllBtn.addCorner(corner: 6)
  63. choseLocalAllBtn.tag = 10
  64. choseLocalAllBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  65. return choseLocalAllBtn
  66. }()
  67. lazy var choseLocalVideoBtn: UIButton = {
  68. let choseLocalVideoBtn = UIButton(frame: CGRect(x: choseLocalAllBtn.frame.maxX + (view.frame.width - choseBtnW * 3) / 4, y: choseLocalAllBtn.frame.minY, width: choseBtnW, height: choseBtnH))
  69. choseLocalVideoBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#999999"), for: .normal)
  70. choseLocalVideoBtn.setTitleColor(PQBFConfig.shared.styleTitleColor, for: .selected)
  71. choseLocalVideoBtn.setTitle("视频", for: .normal)
  72. choseLocalVideoBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  73. choseLocalVideoBtn.addCorner(corner: 6)
  74. choseLocalVideoBtn.tag = 11
  75. choseLocalVideoBtn.isSelected = true
  76. choseLocalVideoBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  77. return choseLocalVideoBtn
  78. }()
  79. lazy var choseLocalImageBtn: UIButton = {
  80. let choseLocalImageBtn = UIButton(frame: CGRect(x: choseLocalVideoBtn.frame.maxX + (view.frame.width - choseBtnW * 3) / 4, y: choseLocalAllBtn.frame.minY, width: choseBtnW, height: choseBtnH))
  81. choseLocalImageBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#999999"), for: .normal)
  82. choseLocalImageBtn.setTitleColor(PQBFConfig.shared.styleTitleColor, for: .selected)
  83. choseLocalImageBtn.setTitle("照片", for: .normal)
  84. choseLocalImageBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  85. choseLocalImageBtn.addCorner(corner: 6)
  86. choseLocalImageBtn.tag = 12
  87. choseLocalImageBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  88. return choseLocalImageBtn
  89. }()
  90. lazy var choseLineView: UIView = {
  91. let choseLineView = UIView(frame: CGRect(x: 0, y: 0, width: 25, height: 3))
  92. choseLineView.frame.origin.y = (navHeadImageView?.frame.maxY ?? 0) - 6
  93. choseLineView.center.x = choseLocalVideoBtn.center.x
  94. choseLineView.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  95. return choseLineView
  96. }()
  97. // 顶部提示视图
  98. // lazy var materialHeadView: PQStuckPointMaterialHeadView = {
  99. // let materialHeadView: PQStuckPointMaterialHeadView = PQStuckPointMaterialHeadView(frame: CGRect(x: 0, y: navHeadImageView?.frame.maxY ?? cDevice_iPhoneNavBarAndStatusBarHei, width: view.frame.width, height: topImageH))
  100. // return materialHeadView
  101. // }()
  102. // 底部操作视图
  103. lazy var bottomRemindView: UIView = {
  104. let bottomRemindView = UIView(frame: CGRect(x: 0, y: view.frame.height - (bottomH + cSafeAreaHeight), width: view.frame.width, height: bottomH + cSafeAreaHeight))
  105. bottomRemindView.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  106. return bottomRemindView
  107. }()
  108. // 确定按钮
  109. lazy var confirmBtn: UIButton = {
  110. let confirmBtn = UIButton(frame: CGRect(x: bottomRemindView.frame.width - cDefaultMargin * 9 - margin, y: margin / 2, width: cDefaultMargin * 9, height: bottomH - margin))
  111. confirmBtn.backgroundColor = PQBFConfig.shared.otherTintColor
  112. // confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#EE0051")
  113. confirmBtn.setTitle("确定", for: .normal)
  114. confirmBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#999999"), for: .normal)
  115. confirmBtn.setTitleColor(UIColor.white, for: .selected)
  116. confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
  117. confirmBtn.addCorner(corner: 3)
  118. confirmBtn.tag = 13
  119. confirmBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  120. return confirmBtn
  121. }()
  122. // 底部提示视图
  123. lazy var bottomRemindLab: UILabel = {
  124. let bottomRemindLab = UILabel(frame: CGRect(x: margin, y: 0, width: bottomRemindView.frame.width - margin * 3 - confirmBtn.frame.width, height: bottomH))
  125. bottomRemindLab.attributedText = NSAttributedString(string: "至少选择 1 个视频或 2 张照片")
  126. bottomRemindLab.textAlignment = .left
  127. bottomRemindLab.textColor = PQBFConfig.shared.styleTitleColor
  128. bottomRemindLab.font = UIFont.systemFont(ofSize: 14, weight: .medium)
  129. return bottomRemindLab
  130. }()
  131. /// 已选素材列表
  132. lazy var materialListView: BFSelectedMaterialListView = {
  133. let materialListView = BFSelectedMaterialListView(frame: CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: 88))
  134. materialListView.deletedMaterialHandle = { [weak self] materialData, isDissmiss in
  135. /// 处理已经选择的数据
  136. self?.dealWithSelectedMaterials(isDissmiss: isDissmiss, isChose: false, materialData: materialData)
  137. }
  138. materialListView.detailMaterialHandle = { [weak self] _, materialData in
  139. let detailVc: BFMaterialDetailController = BFMaterialDetailController()
  140. detailVc.materialDetailClickHandle = { [weak self] isMaterialSelected, materialData in
  141. if isMaterialSelected != materialData?.isSelected {
  142. self?.photoMaterialVc.updateMaterials(isSelected:!(isMaterialSelected ?? false), materialData: materialData)
  143. }
  144. }
  145. if !(materialData?.isSelected ?? false) {
  146. materialData?.selectedIndex = (self?.selectedDataCount ?? 0) + 1
  147. }
  148. detailVc.isStuckPoint = true
  149. detailVc.materialData = materialData
  150. self?.navigationController?.pushViewController(detailVc, animated: true)
  151. }
  152. return materialListView
  153. }()
  154. /// 图片加载视图
  155. lazy var photoMaterialVc: BFPhotosMaterialController = {
  156. let photoMaterialVc = BFPhotosMaterialController()
  157. photoMaterialVc.isShowMediaTag = false
  158. photoMaterialVc.imageDuration = 1
  159. addChild(photoMaterialVc)
  160. view.insertSubview(photoMaterialVc.view, belowSubview: bottomRemindView)
  161. photoMaterialVc.updateFrame(newFrame: CGRect(x: 0, y: (navHeadImageView?.frame.maxY ?? cDevice_iPhoneNavBarAndStatusBarHei) + margin / 2, width: view.frame.width, height: view.frame.height - ((navHeadImageView?.frame.maxY ?? cDevice_iPhoneNavBarAndStatusBarHei) + margin + bottomRemindView.frame.height)))
  162. photoMaterialVc.selectedMaterialHandle = { [weak self] currentMaterialData, selectedPhotoData, selectedTotalDuration, imageCount in
  163. self?.dealWithSelectedMaterial(materialData: currentMaterialData, selectedDatas: selectedPhotoData, totalDuration: selectedTotalDuration, imageCount: imageCount)
  164. /// 处理已经选择的数据
  165. self?.dealWithSelectedMaterials(isDissmiss: selectedPhotoData.count <= 0, isChose: true, materialData: currentMaterialData)
  166. }
  167. photoMaterialVc.detailMaterialHandle = { [weak self] _, currentMaterialData in
  168. let detailVc: BFMaterialDetailController = BFMaterialDetailController()
  169. detailVc.isStuckPoint = true
  170. detailVc.materialDetailClickHandle = { [weak self] isMaterialSelected, materialData in
  171. if isMaterialSelected != materialData?.isSelected {
  172. self?.photoMaterialVc.updateMaterials(isSelected:!(isMaterialSelected ?? false), materialData: materialData)
  173. }
  174. }
  175. if !(currentMaterialData?.isSelected ?? false) {
  176. currentMaterialData?.selectedIndex = (self?.selectedDataCount ?? 0) + 1
  177. }
  178. detailVc.materialData = currentMaterialData
  179. self?.navigationController?.pushViewController(detailVc, animated: true)
  180. }
  181. photoMaterialVc.emptyRefreshHandle = { [weak self] msgType in
  182. self?.btnClick(sender: msgType == .video ? self?.choseLocalImageBtn : (msgType == .image ? self?.choseLocalVideoBtn : self?.choseLocalAllBtn))
  183. }
  184. photoMaterialVc.scrollViewDidScroll = { [weak self] _ in
  185. // 更新frame
  186. // self?.updateMaterialHeadFrame(contentOffset: contentOffset)
  187. }
  188. return photoMaterialVc
  189. }()
  190. override public func viewDidLoad() {
  191. super.viewDidLoad()
  192. leftButton(image: UIImage.init(named: "upload_delete"), tintColor: PQBFConfig.shared.styleTitleColor)
  193. navHeadImageView?.addSubview(changeCollecBtn)
  194. navHeadImageView?.frame.size.height = cDevice_iPhoneNavBarAndStatusBarHei + margin * 2 + choseBtnH
  195. navHeadImageView?.addSubview(choseLocalAllBtn)
  196. navHeadImageView?.addSubview(choseLocalVideoBtn)
  197. navHeadImageView?.addSubview(choseLocalImageBtn)
  198. navHeadImageView?.addSubview(choseLineView)
  199. // view.insertSubview(materialHeadView, belowSubview: navHeadImageView!)
  200. view.addSubview(bottomRemindView)
  201. bottomRemindView.addSubview(confirmBtn)
  202. bottomRemindView.addSubview(bottomRemindLab)
  203. photoMaterialVc.msgType = .video
  204. view.bringSubviewToFront(navHeadImageView!)
  205. view.insertSubview(materialListView, belowSubview: bottomRemindView)
  206. // 卡点音乐素材选择曝光上报
  207. PQEventTrackViewModel.baseReportUpload(businessType: .bt_windowView, objectType: .ot_view_selectSyncedUpMaterial, pageSource: .sp_stuck_selectMaterial, extParams: nil, remindmsg: "卡点视频数据上报-(曝光上报:卡点视频素材选择页)")
  208. // 注册通知
  209. addNotification(self, selector: #selector(dismissVc), name: cFinishedPublishedNotiKey, object: nil)
  210. }
  211. /// 按钮点击事件
  212. /// - Parameter sender: <#sender description#>
  213. /// - Returns: <#description#>
  214. @objc func btnClick(sender: UIButton?) {
  215. switch sender?.tag {
  216. case 1: // 选择图库
  217. sender?.isSelected = !(sender?.isSelected ?? false)
  218. if sender?.isSelected ?? false {
  219. albumController.showCategoryView()
  220. } else {
  221. albumController.dismissCategoryView()
  222. }
  223. case 10, 11, 12: // 筛选全部图库
  224. choseLocalAllBtn.isSelected = sender?.tag == 10
  225. choseLocalVideoBtn.isSelected = sender?.tag == 11
  226. choseLocalImageBtn.isSelected = sender?.tag == 12
  227. if sender?.tag == 11 {
  228. photoMaterialVc.msgType = .video
  229. } else if sender?.tag == 12 {
  230. photoMaterialVc.msgType = .image
  231. } else {
  232. photoMaterialVc.msgType = .all
  233. }
  234. UIView.animate(withDuration: 0.3, delay: 0, options: .allowUserInteraction) { [weak self] in
  235. self?.choseLineView.center.x = sender?.center.x ?? 0
  236. } completion: { _ in
  237. }
  238. case 13:
  239. if confirmBtn.isSelected {
  240. reCreateMusicData?.endTime = (reCreateMusicData?.startTime ?? 0) + (reCreateMusicData?.stuckPointCuttingTime(videoCount: selectedDataCount - selectedImageDataCount, imageCount: selectedImageDataCount, totalDuration: photoMaterialVc.selectedTotalDuration) ?? 0)
  241. let stuckPointMusicVc = PQStuckPointMusicController()
  242. stuckPointMusicVc.selectedMusicData = reCreateMusicData
  243. stuckPointMusicVc.selectedDataCount = selectedDataCount
  244. stuckPointMusicVc.selectedImageDataCount = selectedImageDataCount
  245. stuckPointMusicVc.selectedTotalDuration = photoMaterialVc.selectedTotalDuration
  246. stuckPointMusicVc.selectedPhotoData = photoMaterialVc.selectedPhotoData
  247. navigationController?.pushViewController(stuckPointMusicVc, animated: true)
  248. // 卡点视频素材确认按钮
  249. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_confirmMaterial, pageSource: .sp_stuck_selectMaterial, extParams: ["materialNumber": selectedDataCount], remindmsg: "卡点视频数据上报-(点击上报:卡点视频素材确认按钮)")
  250. }
  251. default:
  252. break
  253. }
  254. }
  255. public override func backBtnClick() {
  256. super.backBtnClick()
  257. if isPresent {
  258. postNotification(name: cFinishedPublishedNotiKey)
  259. }
  260. // 卡点视频返回按钮点击上报
  261. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_click_back, pageSource: .sp_stuck_selectMaterial, extParams: nil, remindmsg: "卡点视频数据上报-(点击上报:返回按钮)")
  262. }
  263. // 返回
  264. @objc func dismissVc() {
  265. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) { [weak self] in
  266. if self?.isPresent ?? false {
  267. self?.dismiss(animated: true, completion: nil)
  268. }
  269. }
  270. }
  271. deinit {
  272. PQNotification.removeObserver(self)
  273. }
  274. /// 图库选择的回调
  275. /// - Parameter seletedData: <#seletedData description#>
  276. /// - Returns: <#description#>
  277. func albumSelectedHandle(seletedData: PHAsset?) {
  278. changeCollecBtn.setTitle(seletedData?.title ?? "全部", for: .normal)
  279. changeCollecBtn.isSelected = false
  280. changeCollecBtn.imagePosition(at: .right, space: cDefaultMargin / 2)
  281. photoMaterialVc.assetCollection = seletedData?.assetCollection
  282. photoMaterialVc.msgType = photoMaterialVc.msgType
  283. // photoMaterialVc.msgType = .video
  284. // choseLocalAllBtn.isSelected = false
  285. // choseLocalVideoBtn.isSelected = true
  286. // choseLocalImageBtn.isSelected = false
  287. }
  288. /// 点击选择的回调
  289. /// - Parameter materialData: <#materialData description#>
  290. /// - Returns: <#description#>
  291. func dealWithSelectedMaterial(materialData _: PHAsset?, selectedDatas: [PHAsset]?, totalDuration: Float64, imageCount: Int) {
  292. selectedDataCount = selectedDatas?.count ?? 0
  293. selectedImageDataCount = imageCount
  294. confirmBtn.isSelected = (selectedDataCount > 0 && (imageCount >= 2 || selectedDataCount > imageCount))
  295. if confirmBtn.isSelected {
  296. confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  297. } else {
  298. confirmBtn.backgroundColor = PQBFConfig.shared.otherTintColor
  299. }
  300. if selectedDataCount <= 0 {
  301. bottomRemindLab.attributedText = NSAttributedString(string: "至少选择 1 个视频或 2 张照片")
  302. } else if selectedImageDataCount == 1, selectedDataCount == selectedImageDataCount {
  303. let att = NSMutableAttributedString(string: "至少选择 1 个视频或 2 张照片")
  304. bottomRemindLab.attributedText = att
  305. } else {
  306. let att = NSMutableAttributedString(string: "素材总时长 \(totalDuration.formatDurationToHMS())")
  307. att.setAttributes([.foregroundColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)], range: NSRange(location: 6, length: "\(totalDuration.formatDurationToHMS())".count))
  308. bottomRemindLab.attributedText = att
  309. }
  310. confirmBtn.setTitle(selectedDataCount > 0 ? "确定(\(selectedDataCount))" : "确定", for: .normal)
  311. }
  312. /// 处理已经选择的数据
  313. /// - Returns: <#description#>
  314. func dealWithSelectedMaterials(isDissmiss: Bool, isChose: Bool, materialData: PHAsset?) {
  315. // 添加当前素材
  316. if materialData != nil {
  317. if isChose {
  318. materialListView.addMaterialData(materialData: materialData!)
  319. } else {
  320. photoMaterialVc.deSeletedMaterialData(materialData: materialData)
  321. }
  322. }
  323. if isChose && !isDissmiss && materialListView.frame.minY > (bottomRemindView.frame.minY - 88) {
  324. UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction) { [weak self] in
  325. self?.materialListView.frame = CGRect(x: 0, y: (self?.bottomRemindView.frame.minY ?? 0) - 88, width: cScreenWidth, height: 88)
  326. self?.photoMaterialVc.updateFrame(newFrame: CGRect(x: 0, y: self?.photoMaterialVc.view.frame.minY ?? 0, width: self?.photoMaterialVc.view.frame.width ?? 0, height: (self?.photoMaterialVc.view.frame.height ?? 0) - 88))
  327. } completion: { _ in
  328. }
  329. } else if isDissmiss && materialListView.frame.minY != cScreenHeigth {
  330. UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction) { [weak self] in
  331. self?.materialListView.frame = CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: 88)
  332. self?.photoMaterialVc.updateFrame(newFrame: CGRect(x: 0, y: self?.photoMaterialVc.view.frame.minY ?? 0, width: self?.photoMaterialVc.view.frame.width ?? 0, height: (self?.photoMaterialVc.view.frame.height ?? 0) + 88))
  333. } completion: { _ in
  334. }
  335. }
  336. }
  337. }