杨锴
2025-05-11 7453d2d0cef415b34323d1b91e6cfa4a6ba31178
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
//
//  CourseOnlineListVC.swift
//  WanPai
//
//  Created by 无故事王国 on 2023/6/15.
//
 
import UIKit
import JQTools
import MJRefresh
import RxCocoa
import RxSwift
import QMUIKit
 
 
class VideoViewModel:RefreshModel<VideosModel>{
    let position = BehaviorRelay<Int>(value: 1) //1=线上课得积分,2=看视频得奖励
    let search = BehaviorRelay<String>(value: "")
 
    override func api() -> (Observable<BaseResponse<[VideosModel]>>)? {
        return Services.benefitsVideoList(position: position.value, search: search.value)
    }
}
 
 
class CourseOnlineListVC: BaseVC {
 
    private var viewModel = VideoViewModel()
 
    lazy var searchView:CourseOnlineSearchView = {
        let searchV = CourseOnlineSearchView.jq_loadNibView()
        searchV.frame = CGRect(x: 0, y: JQ_NavBarHeight, width: JQ_ScreenW, height: JQ_ScreenW * 0.564 + 57)
        searchV.alpha = 1
        return searchV
    }()
 
    var collectionView:BaseCollectionView!
 
   required init(position:Int){
        super.init(nibName: nil, bundle: nil)
        self.viewModel.position.accept(position)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        var type:BannerPositionType!
        if viewModel.position.value == 1{
            type = .onlineCourse
        }else{
            type = .video
        }
        Services.bannerList(position: type).subscribe(onNext: {[weak self] data in
            guard let weakSelf = self else { return }
            if let models = data.data{
                let imgs = models.map({$0.img})
                var items = [CommonBannerModel]()
                for (index,img) in imgs.enumerated(){
                    items.append(CommonBannerModel(index: index,resource: img,mediaType: .imageUrl))
                }
 
                if models.count > 0{
                    weakSelf.view.addSubview(weakSelf.searchView)
                    self?.searchView.btn_search.addTarget(self, action: #selector(weakSelf.beginSearch), for: .touchUpInside)
                    self?.searchView.tf_search.delegate = self!
                    let insertH = JQ_ScreenW * 0.564 + 57
                    self?.collectionView.contentInset = UIEdgeInsets(top: insertH, left: 14, bottom: 14, right: 14)
                    self?.collectionView.scrollIndicatorInsets = UIEdgeInsets(top: insertH, left: 0, bottom: 0, right: 0)
 
                    self?.searchView.bannerView.setItems(items: items,selectClouse: { m in
                        let index = m.index
                        jumpPage(model: models[index].model, page: models[index].page, type: models[index].type, id: models[index].turnId)
                    })
                }
            }
        }).disposed(by: disposeBag)
 
        viewModel.configure(collectionView,needMore: false)
        viewModel.beginRefresh()
    }
 
    override func setUI() {
        let flowlayout = OnlineLayout()
 
        flowlayout.itemSize = CGSize(width: (JQ_ScreenW - 50) / 2, height: 195)
        flowlayout.minimumLineSpacing = 22
        flowlayout.minimumLineSpacing = 22
//        flowlayout.sectionHeadersPinToVisibleBounds = true
        flowlayout.headerReferenceSize = CGSize(width: JQ_ScreenW, height: 52)
        flowlayout.footerReferenceSize = CGSize(width: JQ_ScreenW, height: 10)
        collectionView = BaseCollectionView(frame: .zero, collectionViewLayout: flowlayout)
        collectionView.register(UINib(nibName: "CourseOnlineCCell", bundle: nil), forCellWithReuseIdentifier: "_CourseOnlineCCell")
        collectionView.register(UINib(nibName: "CourseOnlineHeadView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "_CourseOnlineHeadView")
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "_footer")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        collectionView.contentInset = UIEdgeInsets(top: JQ_ScreenW * 0.564 + 57, left: 14, bottom: 14, right: 14)
 
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
 
    override func setRx() {
        searchView.tf_search.rx.text.orEmpty.bind(to: viewModel.search).disposed(by: disposeBag)
    }
 
    @objc func beginSearch(){
        searchView.tf_search.resignFirstResponder()
        viewModel.beginRefresh()
 
    }
 
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let insertH = scrollView.contentOffset.y  + JQ_ScreenW * 0.5641 + 57
        searchView.jq_y = -insertH
    }
}
 
extension CourseOnlineListVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = viewModel.dataSource.value[indexPath.section].list[indexPath.row]
        let vc = CourseVideoDetailVC(id: model.id)
        vc.title = title ?? ""
        push(vc: vc)
    }
}
 
extension CourseOnlineListVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "_CourseOnlineHeadView", for: indexPath) as! CourseOnlineHeadView
            headerView.indexPath = indexPath
            headerView.moreClouse = { [weak self] index in
                let id = self?.viewModel.dataSource.value[index.section].id
                let vc = CourseOnlineSubListVC(classificationId: id!)
                vc.title = self?.title ?? ""
                self?.push(vc: vc)
            }
            headerView.label_name.text = viewModel.dataSource.value[indexPath.section].name
            return headerView
        }
 
        if kind == UICollectionView.elementKindSectionFooter{
            var footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "_footer", for: indexPath)
 
            let lineView = UIView()
            lineView.backgroundColor = UIColor(hexStr: "#979797").withAlphaComponent(0.29)
            footerView.addSubview(lineView)
            lineView.snp.makeConstraints { make in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(1)
            }
            return footerView
        }
 
 
        return UICollectionReusableView()
    }
 
 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = viewModel.dataSource.value[indexPath.section].list[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_CourseOnlineCCell", for: indexPath) as! CourseOnlineCCell
        cell.videoDetailModel = model
        return cell
    }
 
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return viewModel.dataSource.value.count
    }
 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.dataSource.value[section].list.count
    }
}
 
class OnlineLayout:UICollectionViewFlowLayout{
 
}
 
extension CourseOnlineListVC:QMUITextFieldDelegate{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        viewModel.beginRefresh()
        return true
    }
}