杨锴
2024-09-12 e15c976316feef72ff9bcabce38e0a078f9505db
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
//
//  SearchContentVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/9/10.
//
 
import UIKit
import JQTools
import RxDataSources
import RxRelay
import RxSwift
 
class SearchContentViewModel:RefreshInnerModel<MeditationModel>{
 
                var search = BehaviorRelay<String>(value: "")
 
                override func api() -> (Observable<BaseResponse<BaseResponseList<MeditationModel>>>)? {
                                return Services.search(text: search.value, page: page)
                }
}
 
class SearchContentVC: BaseVC {
 
                @IBOutlet weak var tf_search: UITextField!
                @IBOutlet weak var collectionView: UICollectionView!
                private var viewModel = SearchContentViewModel()
 
                init(content:String) {
                                super.init(nibName: nil, bundle: nil)
                                self.viewModel.search.accept(content)
                }
                
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
                
    override func viewDidLoad() {
        super.viewDidLoad()
                                title = "心泉·疗愈"
 
                                tf_search.text = viewModel.search.value
    }
 
                override func setUI() {
                                view.backgroundColor = UIColor(hexString: "f6f6f6")
                                tf_search.delegate = self
                                tf_search.returnKeyType = .search
 
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.backgroundColor = .clear
                                collectionView.contentInset = UIEdgeInsets(top: 0, left: 21, bottom: 0, right: 21)
                                collectionView.register(UINib(nibName: "HomeRelaxBanner_2_CCell", bundle: nil), forCellWithReuseIdentifier: "_HomeRelaxBanner_2_CCell")
 
                                collectionView.emptyDataSetView {[unowned self] v in
                                                let emptyImageView = UIImageView(image: UIImage(named: "bg_empty"))
                                                v.addSubview(emptyImageView)
                                                emptyImageView.snp.makeConstraints { make in
                                                                make.top.equalTo(56.5)
                                                                make.centerX.equalToSuperview().offset(-self.collectionView.contentInset.left)
                                                }
 
                                                let label = UILabel(text: "暂未找到相关内容哦~")
                                                label.font = .systemFont(ofSize: 12.74, weight: .medium)
                                                label.textColor = UIColor(hexString: "#5B5B5B")
                                                v.addSubview(label)
                                                label.snp.makeConstraints { make in
                                                                make.top.equalTo(emptyImageView.snp.bottom).offset(26.5)
                                                                make.centerX.equalTo(emptyImageView)
                                                }
                                }
 
                                viewModel.configure(collectionView)
                                viewModel.beginRefresh()
                }
 
 
                @IBAction func searchAction(_ sender: UIButton) {
                                tf_search.resignFirstResponder()
 
                                guard !(tf_search.text!.isEmpty) else {
                                                alert(msg: "请输入搜索内容");return
                                }
 
                                viewModel.search.accept(tf_search.text!)
                                viewModel.beginRefresh()
                }
}
 
 
extension SearchContentVC:UICollectionViewDelegate & UICollectionViewDataSource{
 
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                                let m = viewModel.dataSource.value!.list[indexPath.row]
                                let vc = CourseDetialVC(courseId: m.id)
                                JQ_currentViewController().jq_push(vc: vc)
                }
 
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let m = viewModel.dataSource.value!.list[indexPath.row]
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeRelaxBanner_2_CCell", for: indexPath) as! HomeRelaxBanner_2_CCell
                                cell.backgroundColor = .jq_randomColor
                                cell.setMeditationModel(m)
                                return cell
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return viewModel.dataSource.value?.list.count ?? 0
                }
}
 
extension SearchContentVC:UICollectionViewDelegateFlowLayout{
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                                let w = (JQ_ScreenW - 21 * 2 - 14) / 2
 
                                return CGSize(width: w, height: w * 1.314)
                }
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
                                return 14
                }
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                                return 14
                }
}
 
extension SearchContentVC:UITextFieldDelegate{
                func textFieldShouldReturn(_ textField: UITextField) -> Bool {
 
                                textField.resignFirstResponder()
 
                                guard !(textField.text!.isEmpty) else {
                                                alert(msg: "请输入搜索内容")
                                                return true
                                }
 
                                viewModel.search.accept(textField.text!)
                                viewModel.beginRefresh()
                                return true
                }
}