//
|
// HomeItemDetailVC.swift
|
// XQMuse
|
//
|
// Created by 无故事王国 on 2024/8/14.
|
//
|
|
import UIKit
|
import JQTools
|
|
class HomeItemDetailVC: BaseVC,PayMusicDelegate{
|
|
@IBOutlet weak var slider_voice: UISlider!
|
@IBOutlet weak var img_background: UIImageView!
|
@IBOutlet weak var view_function: UIView!
|
@IBOutlet weak var btn_play: UIButton!
|
@IBOutlet weak var label_title: UILabel!
|
@IBOutlet weak var label_subtitle: UILabel!
|
@IBOutlet weak var label_commentNum: UILabel!
|
@IBOutlet weak var view_coutdown: UIView!
|
@IBOutlet weak var img_countdonw: UIImageView!
|
@IBOutlet weak var label_countdown: UILabel!
|
@IBOutlet weak var view_playState: UIView!
|
@IBOutlet weak var label_playState: UILabel!
|
@IBOutlet weak var btn_playMode: UIButton!
|
@IBOutlet weak var label_currentTime: UILabel!
|
@IBOutlet weak var label_totalTime: UILabel!
|
|
private var collect_bitem:UIBarButtonItem!
|
private var audioPlayer:AudioPlayer = AudioPlayer.getSharedInstance()
|
private var id:Int!
|
private var timeLook:Int = 0 //观看时间记录
|
private var settingViewModel = UserDefaultSettingViewModel.getSetting()
|
|
private var currentInterval:TimeInterval = 0
|
private var totalInterval:TimeInterval = 0
|
|
|
private var model:MeditationModel?{
|
didSet{
|
if let m = model{
|
label_title.text = m.meditationTitle
|
label_subtitle.text = m.detailDescription
|
label_commentNum.text = "\(m.questionCount)"
|
}
|
}
|
}
|
|
init(id:Int) {
|
super.init(nibName: nil, bundle: nil)
|
self.id = id
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
title = "心泉·疗愈"
|
|
btn_playMode.isSelected = settingViewModel?.playModel == .singleLoop
|
|
if audioPlayer.times.value != nil{
|
startMiniRunloop()
|
}else{
|
endMiniRunloop()
|
}
|
|
Services.getMeditationDetail(id: id).subscribe(onNext: {[unowned self] data in
|
if let m = data.data{
|
self.model = m
|
let dateTuple = Date.jq_formateToTime_tuple(m.meditationSecondList.first ?? 0)
|
self.label_totalTime.text = String(format: "%02ld:%02ld", dateTuple.hour * 60 + dateTuple.minute,dateTuple.second)
|
self.collect_bitem.image = m.favorite == .yes ? UIImage(named: "btn_collect_1_s"):UIImage(named: "btn_collect_1")
|
self.collect_bitem.tintColor = m.favorite == .yes ? UIColor(hexString: "fe5b60"):.white
|
self.img_background.sd_setImage(with: URL(string: data.data?.backgroundUrl ?? ""))
|
if audioPlayer.meditationModel?.id == m.id{
|
audioPlayer.delegate = self
|
}
|
}
|
}).disposed(by: disposeBag)
|
|
//如果从支付过来的,移除支付
|
navigationController!.viewControllers.removeAll { vc in
|
if vc is PaymentOrderResultVC || vc is PaymentOrderVC{
|
return true
|
}
|
return false
|
}
|
}
|
|
override func viewWillAppear(_ animated: Bool) {
|
super.viewWillAppear(animated)
|
(navigationItem.leftBarButtonItem?.customView as? UIButton)?.setImage(UIImage(named: "btn_back")?.withTintColor(.white), for: .normal)
|
navigationController?.navigationBar.scrollEdgeAppearance?.backgroundColor = .clear
|
}
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
|
if let m = model,timeLook > 0{
|
Services.watchMuse(id: m.id, timeLook: timeLook).subscribe(onNext: {_ in
|
NotificationCenter.default.post(name: TreeTaskUpdate_Noti, object: nil)
|
}).disposed(by: disposeBag)
|
}
|
}
|
|
override func setUI() {
|
|
view_playState.alpha = 0
|
collect_bitem = UIBarButtonItem(image: UIImage(named: "btn_collect"), style: .plain, target: self, action: #selector(collectionAction))
|
collect_bitem.tintColor = .white
|
let share_bitem = UIBarButtonItem(image: UIImage(named: "btn_share"), style: .plain, target: self, action: #selector(shareAction))
|
share_bitem.tintColor = .white
|
share_bitem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 15)
|
navigationItem.rightBarButtonItems = [share_bitem,collect_bitem]
|
|
|
let v1 = UIView()
|
v1.backgroundColor = UIColor(hexStr: "#B6DC90")
|
v1.bounds = CGRect(x: 0, y: 0, width: 17, height: 17)
|
v1.jq_borderWidth = 2
|
v1.jq_borderColor = .white
|
v1.jq_cornerRadius = 8.5
|
|
slider_voice.setThumbImage(v1.qmui_snapshotLayerImage(), for: .normal)
|
slider_voice.setThumbImage(v1.qmui_snapshotLayerImage(), for: .highlighted)
|
view_coutdown.isHidden = true
|
|
view_function.jq_gradientColor(colorArr: [UIColor.black.withAlphaComponent(0.35).cgColor,UIColor.clear.cgColor], cornerRadius: 0, startPoint: CGPoint(x: 0, y: 1), endPoint: CGPoint(x: 0, y: 0), bounds: nil, locations: nil)
|
|
}
|
|
override func setRx() {
|
audioPlayer.times.subscribe {[weak self] t in
|
guard let weakSelf = self else { return }
|
guard let time = t,time > 0 else {weakSelf.endMiniRunloop();return}
|
let turple = Date.jq_formateToTime_tuple(time)
|
weakSelf.label_countdown.text = String(format: "%02ld:%02ld", (turple.hour * 60 + turple.minute),turple.second)
|
|
if weakSelf.view_coutdown.isHidden{
|
weakSelf.startMiniRunloop()
|
weakSelf.view_coutdown.isHidden = false
|
}
|
|
}.disposed(by: disposeBag)
|
|
|
slider_voice.rx.value.changed.subscribe(onNext: {[weak self] v in
|
guard let weakSelf = self else { return }
|
let value = (v.double / 100.0) * weakSelf.totalInterval
|
self?.audioPlayer.setBGMRate(value: value)
|
}).disposed(by: disposeBag)
|
}
|
|
private func startMiniRunloop(){
|
// 创建旋转动画
|
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
|
rotationAnimation.fromValue = 0
|
rotationAnimation.toValue = CGFloat.pi * 2
|
rotationAnimation.duration = 3 // 动画持续时间
|
rotationAnimation.repeatCount = .greatestFiniteMagnitude // 无限重复
|
img_countdonw.layer.add(rotationAnimation, forKey: nil)
|
self.view_coutdown.isHidden = false
|
}
|
|
private func endMiniRunloop(){
|
img_countdonw.layer.removeAllAnimations()
|
self.view_coutdown.isHidden = true
|
}
|
|
@IBAction func playAction(_ sender: UIButton) {
|
sender.isSelected = !sender.isSelected
|
if let m = model{
|
|
if sender.isSelected{
|
audioPlayer.playBGMAt(firstPlayIndex: 0, model: m, delegate: self,coverImage: img_background.image)
|
}else{
|
audioPlayer.bgmPlayer?.pause()
|
}
|
}
|
}
|
|
@IBAction func timeAction(_ sender: UIButton) {
|
CountdownChooseListView.show {[weak self] status in
|
guard let weakSelf = self else { return }
|
|
switch status {
|
case .close:
|
weakSelf.audioPlayer.stopTimer()
|
case .choose(let times):
|
weakSelf.audioPlayer.setTimer(times: times * 60)
|
}
|
|
|
|
// if times <= 0{
|
// weakSelf.timer = nil
|
// weakSelf.times = -1
|
// weakSelf.endMiniRunloop()
|
// return
|
// }
|
//
|
// weakSelf.times = times * 60
|
// if weakSelf.timer == nil{
|
// weakSelf.timer = Timer(timeInterval: 1, repeats: true, block: { timer in
|
//
|
// weakSelf.times -= 1
|
//
|
// if weakSelf.times <= 0{
|
// //todo
|
// }
|
// })
|
// weakSelf.timer?.fire()
|
// RunLoop.current.add(weakSelf.timer!, forMode: .default)
|
// weakSelf.startMiniRunloop()
|
// }
|
}
|
}
|
|
@IBAction func commentAction(_ sender: TapBtn) {
|
guard let m = model else{return}
|
|
let vc = CommentListVC(id: m.id)
|
vc.modalPresentationStyle = .custom
|
present(vc, animated: true)
|
}
|
|
@IBAction func playWayAction(_ sender: UIButton) {
|
UIView.animate(withDuration: 0.5) {
|
self.view_playState.alpha = 1
|
}
|
|
UIView.animate(withDuration: 0.5, delay: 1.0) {
|
self.view_playState.alpha = 0
|
}
|
sender.isSelected = !sender.isSelected
|
|
label_playState.text = sender.isSelected ? "当前播放模式已设置为单曲循环":"当前播放模式已设置为顺序播放"
|
settingViewModel?.playModel = sender.isSelected ? .singleLoop:.line
|
|
UserDefaultSettingViewModel.saveSetting(settingViewModel!)
|
}
|
|
@IBAction func settingAction(_ sender: UIButton) {
|
PlaySettingView.show()
|
}
|
|
@objc func collectionAction(){
|
Services.meditationFavorite(id: id).subscribe(onNext: {data in
|
self.model?.favorite.troggle()
|
if self.model?.favorite == .yes{
|
alertSuccess(msg: "收藏成功")
|
self.collect_bitem.image = UIImage(named: "btn_collect_1_s")
|
self.collect_bitem.tintColor = UIColor(hexString: "fe5b60")
|
}else{
|
alertSuccess(msg: "已取消收藏")
|
self.collect_bitem.image = UIImage(named: "btn_collect")
|
self.collect_bitem.tintColor = .white
|
}
|
}).disposed(by: disposeBag)
|
}
|
|
@objc func shareAction(){
|
guard let m = model else {return}
|
|
let string = String(format: "%@%@?id=%ld", ShareUrl,"/healDetail/healDetail",m.id)
|
ShareView.show(URL(string:string)!,title: m.meditationTitle,desc: m.detailDescription)
|
}
|
|
override func viewDidLayoutSubviews() {
|
super.viewDidLayoutSubviews()
|
}
|
|
|
override var preferredStatusBarStyle: UIStatusBarStyle{
|
return .lightContent
|
}
|
|
func playState(_ state:PlayMusicState){
|
print("music:当前状态:\(state)")
|
|
guard audioPlayer.meditationModel?.id == model?.id else {return}
|
|
switch state {
|
case .playing:
|
PayMusicVC.show(model: model!)
|
if btn_play.isHidden == false {btn_play.isHidden = true}
|
hiddenHUD()
|
case .paurse:btn_play.isHidden = false
|
case .end:break
|
case .next:break
|
}
|
}
|
|
func playListen(currentInterval:TimeInterval,totalInterval:TimeInterval){
|
guard audioPlayer.meditationModel?.id == model?.id else {return}
|
guard !totalInterval.isNaN else {return}
|
guard !currentInterval.isNaN else {return}
|
|
self.currentInterval = currentInterval
|
self.totalInterval = totalInterval
|
|
timeLook += 1
|
|
let v = currentInterval / totalInterval * 100
|
print("music:当前时间:\(currentInterval) ---- \(totalInterval) -- \(v)%")
|
|
if !self.slider_voice.isTracking{
|
self.slider_voice.value = Float(v)
|
}
|
|
let current = Date.jq_formateToTime_tuple(Int(currentInterval))
|
label_currentTime.text = String(format: "%02ld:%02ld", current.hour * 60 + current.minute,current.second)
|
|
let total = Date.jq_formateToTime_tuple(Int(totalInterval))
|
label_totalTime.text = String(format: "%02ld:%02ld", total.hour * 60 + total.minute,total.second)
|
}
|
}
|