杨锴
2024-11-06 63f7ed967433acee3ae8764c7a077e15c29c41f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
//  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)
                }
}