杨锴
2024-11-15 745e4fc64ca2912c011d925f12e638ed6d81b367
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//
//  CourseDetialVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/16.
//
 
import UIKit
import JQTools
import SPPageMenu
import QMUIKit
 
enum CourseDetialStyle{
                case style1 //有视频
                case style2 //无视频
}
 
let CourseRefresh_Noti = Notification.Name.init("CourseRefresh_Noti")
 
class CourseDetialVC: BaseVC {
 
                private var tableView:UITableView?
    private var collect_bitem:UIBarButtonItem!
    private var share_bitem:UIBarButtonItem!
                private let studyBtn = QMUIButton(type: .custom)
                private var headerView = CourseDetailHeaderView.jq_loadNibView()
                private var barStyle:UIStatusBarStyle = .lightContent
                private var scrollShowCell = false
                private var currentShowIndex:IndexPath = IndexPath(row: 0, section: 0)
                var isAnimationing = false
                private var style:CourseDetialStyle = .style1
//                private var courseId:Int!
                private var courseDetailModel:CourseModel?
 
                private var section0TCell:CourseDetail_1_TCell!
                private var section1TCell:CourseDetail_2_TCell!
                private var section2TCell:CourseDetail_3_TCell!
                private var section0Height:Double = 0
 
                private(set) var pageMenu:SPPageMenu = {
                                let pageMenu = SPPageMenu(frame: .zero, trackerStyle: .line)
                                // 追踪线
                                pageMenu.trackerWidth = 27.5
                                pageMenu.setTrackerHeight(11, cornerRadius:0)
                                pageMenu.trackerFollowingMode = .always
                                pageMenu.contentInset = UIEdgeInsets(top: 0, left: 10.5, bottom: 15, right: 19.5)
                                pageMenu.setItems(["简介","章节","相关推荐"], selectedItemIndex: 0)
                                pageMenu.backgroundColor = UIColor(hexString: "#F6F6F6")
                                pageMenu.tracker.image = UIImage(named: "icon_unline")
                                pageMenu.tracker.backgroundColor = .clear
 
                                // 分割线
                                pageMenu.dividingLine.backgroundColor = .clear
                                pageMenu.dividingLineHeight = 1
 
                                // item
                                pageMenu.spacing = 27.0
                                pageMenu.permutationWay = .scrollAdaptContent
 
                                // 字体
                                pageMenu.selectedItemTitleFont = UIFont.systemFont(ofSize: 18, weight: .bold)
                                pageMenu.unSelectedItemTitleFont = UIFont.systemFont(ofSize: 15, weight: .bold)
                                // 颜色
                                pageMenu.selectedItemTitleColor = UIColor(hexStr: "#282828")
                                pageMenu.unSelectedItemTitleColor = UIColor(hexStr: "#AAAAAA")
                                return pageMenu
                }()
 
                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
                                navigationController?.navigationBar.standardAppearance.backgroundColor = .clear
                }
 
                init(courseModel:CourseModel) {
                                super.init(nibName: nil, bundle: nil)
                                self.courseDetailModel = courseModel
                }
                
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
                
                override func viewDidLoad() {
                                super.viewDidLoad()
                                title = "课程详情"
                                getData()
    }
 
                override func setUI() {
                                super.setUI()
 
                                pageMenu.delegate = self
                                tableView = UITableView(frame: .zero, style: .plain)
                                tableView!.contentInsetAdjustmentBehavior = .never
                                tableView!.separatorStyle = .none
                                tableView!.delegate = self
                                tableView!.dataSource = self
                                tableView!.showsVerticalScrollIndicator = false
                                if #available(iOS 16.0, *) {
                                                tableView!.sectionHeaderTopPadding = 0
                                }
                                tableView!.backgroundColor = UIColor(hexString: "#f6f6f6")
                                tableView!.register(UINib(nibName: "CourseDetail_1_TCell", bundle: nil), forCellReuseIdentifier: "_CourseDetail_1_TCell")
                                tableView!.register(UINib(nibName: "CourseDetail_2_TCell", bundle: nil), forCellReuseIdentifier: "_CourseDetail_2_TCell")
                                tableView!.register(UINib(nibName: "CourseDetail_3_TCell", bundle: nil), forCellReuseIdentifier: "_CourseDetail_3_TCell")
 
                                section0TCell = (tableView!.dequeueReusableCell(withIdentifier: "_CourseDetail_1_TCell") as! CourseDetail_1_TCell)
                                section1TCell = (tableView!.dequeueReusableCell(withIdentifier: "_CourseDetail_2_TCell") as! CourseDetail_2_TCell)
                                section2TCell = (tableView!.dequeueReusableCell(withIdentifier: "_CourseDetail_3_TCell") as! CourseDetail_3_TCell)
 
                                view.addSubview(tableView!)
                                tableView!.snp.makeConstraints { make in
                                                make.edges.equalToSuperview()
                                }
 
                                DispatchQueue.main.async {
                                                self.headerView.frame = CGRect(x: 0, y: 0, width: JQ_ScreenW, height: 413)
                                                self.tableView!.tableHeaderView = self.headerView
                                }
 
        collect_bitem = UIBarButtonItem(image: UIImage(named: "btn_collect"), style: .plain, target: self, action: #selector(collectionAction))
        collect_bitem.tintColor = .white
        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]
 
        section0TCell.webView.scrollView.rx.observe(CGSize.self, "contentSize").map { (size) -> CGFloat? in
            if let size = size{
                return size.height
            }
            return nil
        }.subscribe(onNext: { [weak self](height) in
            if let height = height{
                if height > self?.section0Height ?? 0{
                    self?.section0Height = height
                    self?.section0TCell.cons_webHei.constant = height
                    self?.tableView?.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .none)
                }
            }
        }).disposed(by: disposeBag)
                }
 
                private func getData(){
        if let m = courseDetailModel{
            collect_bitem.image = m.isCollect == .yes ? UIImage(named: "btn_collect_1_s"):UIImage(named: "btn_collect")
           collect_bitem.tintColor = m.isCollect == .yes ? UIColor(hexString: "#fe5b60"):.white
           headerView.setCourseModel(m)
           section1TCell.setItems(m)
           section2TCell.setItems(m.list2)
 
            if m.courseType == .online{
                style = .style1
                pageMenu.setItems(["简介","章节","相关推荐"], selectedItemIndex: 0)
 
                //是否需要购买
                var  needPayment:Bool = true
                switch m.chargeType{
                    case .free:needPayment = false
                    case .payment:needPayment = !(m.isBuy == .yes)
                    case .vipFree:needPayment = !UserViewModel.getAvatarInfo().checkVip()
                }
 
                if needPayment{
                    let attribute = AttributedStringbuilder.build().add(string: "    疗愈币", withFont: .systemFont(ofSize: 12,weight: .bold), withColor: UIColor(hexString: "#F6F6F6")!).add(string: "\(m.iosPrice.jq_formatFloat)", withFont: .systemFont(ofSize: 21.71, weight: .bold), withColor: UIColor(hexString: "#F6F6F6")!).add(string: "  立即购买     ", withFont: .systemFont(ofSize: 16, weight: .bold), withColor: UIColor(hexString: "#F6F6F6")!).mutableAttributedString
                    studyBtn.setAttributedTitle(attribute, for: .normal)
                }else{
                    let attribute = AttributedStringbuilder.build().add(string: "    立即学习    ", withFont: .systemFont(ofSize: 16, weight: .bold), withColor: UIColor(hexString: "#F6F6F6")!).mutableAttributedString
                    studyBtn.setAttributedTitle(attribute, for: .normal)
                }
 
                setFootView()
 
            }else{
                style = .style2
                DispatchQueue.main.asyncAfter(delay: 0.5) {
                    self.headerView.setVideo(url: m.detailUrl, coverImageUrl: m.coverUrl.jq_urlEncoded(), delegate: self)
                }
                pageMenu.setItems(["简介"], selectedItemIndex: 0)
            }
            tableView?.reloadData()
        }
                }
 
                private func setFootView(){
                                let footView = UIView()
                                footView.backgroundColor = .white
                                view.addSubview(footView)
                                footView.snp.makeConstraints { make in
                                                make.left.right.bottom.equalToSuperview()
                                                make.height.equalTo(84)
                                }
 
        if courseDetailModel?.chargeType == .payment || courseDetailModel?.chargeType == .free{
            let giftBtn = QMUIButton(type: .custom)
            giftBtn.imagePosition = .left
            giftBtn.spacingBetweenImageAndTitle = 7.5
            giftBtn.setTitleColor(UIColor(hexString: "#464646"), for: .normal)
            giftBtn.setImage(UIImage(named: "btn_sendGift"), for: .normal)
            giftBtn.setTitle("赠送好友", for: .normal)
            giftBtn.addTarget(self, action: #selector(sendGift(_:)), for: .touchUpInside)
            giftBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .bold)
            footView.addSubview(giftBtn)
            giftBtn.snp.makeConstraints { make in
                make.left.equalTo(26)
                make.top.equalTo(26.5)
            }
        }
 
                                studyBtn.setTitleColor(UIColor.white, for: .normal)
                                studyBtn.setTitle("    立即学习    ", for: .normal)
                                studyBtn.addTarget(self, action: #selector(handleAction(_:)), for: .touchUpInside)
                                studyBtn.cornerRadius = 17
                                studyBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16.57, weight: .bold)
                                studyBtn.setBackgroundColor(color: UIColor(hexString: "#8AAE65")!, forState: .normal)
                                footView.addSubview(studyBtn)
                                studyBtn.snp.makeConstraints { make in
                                                make.right.equalTo(-20)
            make.top.equalTo(22)
                                                make.height.equalTo(34)
                                }
                }
 
                override func setRx() {
 
                                NotificationCenter.default.rx.notification(CourseRefresh_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self]data in
                                                self?.getData()
                                }).disposed(by: disposeBag)
                }
 
    @objc func collectionAction(){
        guard let courseId = courseDetailModel?.id else { return }
 
        Services.clouseFavorite(id: courseId).subscribe(onNext: {[weak self]data in
            self?.courseDetailModel?.isCollect.troggle()
            if self?.courseDetailModel?.isCollect == .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 = courseDetailModel else{return}
 
        let path = courseDetailModel!.courseType == .offline ? "/courseDetail/offLine":"/courseDetail/onLine"
        let string = String(format: "%@%@?courseId=%ld", ShareUrl,path,m.id)
        ShareView.show(URL(string: string)!,title: "传递心灵温暖,一起感受疗愈力量",desc: m.description)
    }
 
                @objc func handleAction(_ btn:QMUIButton){
 
        if let m = courseDetailModel{
            guard m.list.count > 0 else {alert(msg: "当前课程未配置章节");return}
 
            if m.chargeType == .free || (m.chargeType == .vipFree && m.isVip == .yes) || (m.chargeType == .payment && m.isBuy == .yes){
                let vc = CourseDetialVideoVC(items: m.list, selectIndex: IndexPath(row: 0, section: 0))
                push(vc: vc)
            }else{
                let vc = PaymentOrderVC(courseItemModel: m, type: .course, giftToOther: false, showType: .horizontal)
                push(vc: vc)
            }
                                }
                }
 
                @objc func sendGift(_ btn:QMUIButton){
        if let price = courseDetailModel?.iosPrice{
                                                CourseSendGiftView.show(price:price) {
                guard sceneDelegate!.checkisLoginState() else{return}
                let vc = PaymentOrderVC(courseItemModel: self.courseDetailModel!,type: .course,giftToOther: true, showType: .horizontal)
                                                                self.push(vc: vc)
                                                }
                                }
                }
 
                override var preferredStatusBarStyle: UIStatusBarStyle{
                                return barStyle
                }
}
 
extension CourseDetialVC:UITableViewDataSource{
                func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                                if style == .style1{
                                                return 3
                                }else if style == .style2{
                                                return 1
                                }else{
                                                return 0
                                }
                }
 
                func numberOfSections(in tableView: UITableView) -> Int {
                                return 1
                }
 
                func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                                return UITableView.automaticDimension
                }
 
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
                                if indexPath.row == 0{
                                                section0TCell.webView.loadHTMLString(courseDetailModel?.briefIntroduction.jq_wrapHtml() ?? "", baseURL: nil)
                                                section0TCell.backgroundColor = UIColor(hexString: "#f6f6f6")
                                                return section0TCell
                                }
 
                                if indexPath.row == 1{
                                                section1TCell.backgroundColor = UIColor(hexString: "#f6f6f6")
                                                return section1TCell
                                }
 
                                if indexPath.row == 2{
                                                section2TCell.backgroundColor = UIColor(hexString: "#f6f6f6")
                                                return section2TCell
                                }
 
                                var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
                                if cell == nil{
                                                cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
                                }
                                return cell!
                }
 
                func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                                var header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header")
 
                                if header == nil{
                                                header = UITableViewHeaderFooterView(reuseIdentifier: "header")
                                                header?.addSubview(pageMenu)
                                                pageMenu.snp.makeConstraints { make in
                                                                make.edges.equalTo(header!)
                                                }
                                }
 
                                return header
                }
 
                func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                                return 73.5
                }
}
 
extension CourseDetialVC:UITableViewDelegate{
 
 
}
 
extension CourseDetialVC:UIScrollViewDelegate{
 
                func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
                                isAnimationing = false
                }
 
                func scrollViewDidScroll(_ scrollView: UIScrollView) {
 
                                let v = min(scrollView.contentOffset.y / JQ_NavBarHeight, 1)
                                navigationController?.navigationBar.standardAppearance.backgroundColor = .white.withAlphaComponent(v)
 
                                if v > 0{
                                                (navigationItem.leftBarButtonItem?.customView as? UIButton)?.setImage(UIImage(named: "btn_back")?.withTintColor(.black.withAlphaComponent(v)), for: .normal)
 
            (navigationItem.rightBarButtonItem?.customView as? UIButton)?.setImage(UIImage(named: "btn_back")?.withTintColor(.black.withAlphaComponent(v)), for: .normal)
            collect_bitem.tintColor = .black
            share_bitem.tintColor = .black
 
                                                self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:Def_NavFontColor.withAlphaComponent(v),.font:Def_NavFont]
                                                barStyle = .darkContent
 
                                }else{
            collect_bitem.tintColor =  self.courseDetailModel?.isCollect == .yes ? UIColor(hexString: "#fe5b60"):.white
            share_bitem.tintColor = .white
 
                                                (navigationItem.leftBarButtonItem?.customView as? UIButton)?.setImage(UIImage(named: "btn_back")?.withTintColor(.white), for: .normal)
                                                self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:UIColor.white,.font:Def_NavFont]
                                }
 
                                if v == 1{
                                                barStyle = .darkContent
                                                setNeedsStatusBarAppearanceUpdate()
                                }else{
                                                barStyle = .lightContent
                                                setNeedsStatusBarAppearanceUpdate()
                                }
 
                                if let table = scrollView as? UITableView{
                                                if v > 0{
                                                                table.contentInset = UIEdgeInsets(top: JQ_NavBarHeight, left: 0, bottom: 118, right: 0)
                                                }else{
                                                                table.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 118, right: 0)
                                                }
                                }
 
                                let offSetY = scrollView.contentOffset.y + CGRectGetHeight(scrollView.frame) / 2;
 
                                if let currentIndex = tableView?.indexPathForRow(at: CGPoint(x: 0, y: offSetY)){
 
                                                if !isAnimationing{
                                                if currentShowIndex != currentIndex{
                                                                currentShowIndex = currentIndex
                                                                                pageMenu.selectedItemIndex = currentIndex.row
                                                                }
                                                }
                                }
                }
}
 
extension CourseDetialVC:SPPageMenuDelegate{
                func pageMenu(_ pageMenu: SPPageMenu, itemSelectedAt index: Int) {
 
                                if currentShowIndex != IndexPath(row: index, section: 0){
                                                currentShowIndex =  IndexPath(row: index, section: 0)
 
                                                if !(tableView?.isDragging ?? true){
                                                                isAnimationing = true
                                                                tableView?.scrollToRow(at: currentShowIndex, at: .top, animated: true)
                                                }
                                }
                }
}
 
extension CourseDetialVC:CLPlayerDelegate{
 
}