//
|
// 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{
|
|
}
|