fix
无故事王国
2024-06-20 f2e891eecfac25bf6aed38c8eadfdf05704b16b6
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
//
//  HomeListenFight_lesson_5_VC.swift
//  DolphinEnglishLearnStudent
//
//  Created by 无故事王国 on 2024/5/28.
//
 
import UIKit
 
class HomeListenFight_lesson_5_VC: BaseVC {
 
                private var viewModel = FightAnswerViewModel()
                private var listenNewModel:ListenNewModel!
                private var page:Int!
                private var answterCount:Int = 0 //回答计数,用于确定角标
                private var playVoiceAt:Int? //播放声音的View
                private var playVoiceRealAt:Int? //播放声音的View -被乱序后,真实Index
                var rootViewModel:HomeListenFightViewModel!
                private var voicePlayer = VoicePlayer.share()
                private var isListen:Bool = false
 
                private lazy var collectionView:UICollectionView = {
                                let flowLayout = UICollectionViewFlowLayout()
                                let w = (JQ_ScreenW - 189 * 2 - 18) / 2.0
                                flowLayout.itemSize = CGSize(width: w, height: w * 0.70)
                                flowLayout.minimumInteritemSpacing = 15
                                flowLayout.minimumLineSpacing = 15
                                flowLayout.scrollDirection = .vertical
                                let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
                                collection.register(UINib(nibName: "ListenFight_lesson_1_CCell", bundle: nil), forCellWithReuseIdentifier: "_ListenFight_lesson_1_CCell")
                                collection.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
                                collection.isScrollEnabled = false
                                return collection
                }()
 
                private lazy var stackView:UIStackView = {
                                let sta = UIStackView()
                                sta.spacing = 89
                                sta.distribution = .equalSpacing
                                sta.axis = .horizontal
                                return sta
                }()
 
                private lazy var label_hint:UILabel = {
                                let label = UILabel()
                                label.font = .systemFont(ofSize: 18, weight: .medium)
                                label.textColor = .black
                                label.text = "语音对应内容"
                                label.textAlignment = .center
                                return label
                }()
 
                required init(page:Int,listenNewModel:ListenNewModel){
                                super.init(nibName: nil, bundle: nil)
                                self.page = page
                                self.listenNewModel = listenNewModel
//                                self.listen1Model.subjectList.shuffle()
                }
 
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
 
 
                override func viewDidLoad() {
                                super.viewDidLoad()
 
                                collectionView.reloadData()
                }
 
 
                override func viewDidAppear(_ animated: Bool) {
                                super.viewDidAppear(animated)
                                voicePlayer.delegate = self
                }
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                voicePlayer.delegate = nil
                                VoicePlayer.share().playerInterrupt()
                }
 
                override func setUI() {
                                super.setUI()
 
                                view.addSubview(stackView)
                                stackView.snp.makeConstraints { make in
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(19)
                                                make.centerX.equalToSuperview()
                                                make.height.equalTo(52)
                                }
 
                                view.addSubview(label_hint)
                                label_hint.snp.makeConstraints { make in
                                                make.left.right.equalToSuperview()
                                                make.top.equalTo(stackView.snp.bottom).offset(6)
                                                make.height.equalTo(0)
                                }
 
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.showsVerticalScrollIndicator = false
                                collectionView.backgroundColor = .clear
                                view.addSubview(collectionView)
                                collectionView.snp.makeConstraints { make in
                                                make.top.equalTo(self.label_hint.snp.bottom).offset(11)
                                                make.left.equalTo(189)
                                                make.width.equalTo(JQ_ScreenW - 189 * 2)
                                                make.bottom.equalToSuperview()
                                }
 
                                showHintText(false)
 
                                setAnswerStackView()
                }
 
                override func viewDidLayoutSubviews() {
                                super.viewDidLayoutSubviews()
                                let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
 
                                let w = (collectionView.size.width - flowLayout.minimumLineSpacing) / 2
                                let h = (collectionView.size.height - flowLayout.minimumInteritemSpacing) / 2.0
                                
                                if flowLayout.itemSize.width != w || flowLayout.itemSize.height != h{
                                                flowLayout.itemSize = CGSize(width: w, height: h)
                                                collectionView.reloadData()
                                }
                }
 
                override func setRx() {
 
                }
 
                func restore(){
                                viewModel.answerType.accept(.none)
                                answterCount  = 0
                                playVoiceAt = nil
                                playVoiceRealAt = nil
 
                                for subV in view.subviews{
                                                if subV is VoiceHandleView{
                                                                subV.removeFromSuperview()
                                                }
                                }
                                setUI()
                                collectionView.reloadData()
                }
 
                private func setAnswerStackView(){
 
                                for subView in stackView.arrangedSubviews{
                                                subView.removeFromSuperview()
                                }
 
                                if !view.subviews.contains(stackView){
                                                view.addSubview(stackView)
                                                stackView.snp.makeConstraints { make in
                                                                make.right.equalToSuperview().offset(-14)
                                                                make.centerY.equalToSuperview()
                                                }
                                }
 
                                var tempArray = [VoiceHandleView]()
                                for (i,value) in listenNewModel.subjectList[page].enumerated(){
                                                let answerView = VoiceHandleView()
                                                answerView.listenType = .lesson5
                                                answerView.tag = 1000+i
                                                answerView.playAt {[weak self] index in
                                                                print("--->\(index)--\(answerView.frame.origin.x / 248)")
                                                                self?.playVoiceRealAt = Int(answerView.frame.origin.x) / 248
                                                                self?.playVoiceAt = index - 1000
                                                                self?.showHintText(true)
                                                }
                                                answerView.playUrl = value.correct
                                                answerView.snp.makeConstraints { make in
                                                                make.width.equalTo(159)
                                                                make.height.equalTo(52)
                                                }
 
                                                UIView.animate(withDuration: 0.05 + Double(i)) {
                                                                answerView.alpha = 1
                                                }
                                                tempArray.append(answerView)
                                }
                                tempArray.shuffle()
                                stackView.addArrangedSubviews(tempArray)
                }
 
                private func showHintText(_ state:Bool){
                                if let at = playVoiceAt{
                                                label_hint.text = listenNewModel.subjectList[page][at].name
                                                UIView.animate(withDuration: 0.5) {
                                                                if state{
                                                                                self.label_hint.snp.remakeConstraints { make in
                                                                                                make.left.right.equalToSuperview()
                                                                                                make.top.equalTo(self.stackView.snp.bottom).offset(6)
                                                                                                make.height.equalTo(25)
                                                                                }
                                                                }else{
                                                                                self.label_hint.snp.remakeConstraints { make in
                                                                                                make.left.right.equalToSuperview()
                                                                                                make.top.equalTo(self.stackView.snp.bottom).offset(6)
                                                                                                make.height.equalTo(0)
                                                                                }
                                                                }
                                                                self.view.layoutIfNeeded()
                                                }
                                }
                }
}
 
extension HomeListenFight_lesson_5_VC:UICollectionViewDelegate{
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
 
                                if isListen == false{
                                                alertError(msg: "请先听题");return
                                }
                                isListen = false
 
                                viewModel.selectIndex.accept(indexPath)
 
                                let answer = listenNewModel.subjectList[page][playVoiceAt!]
                                let selectAnswer = listenNewModel.subjectList[page][indexPath.row]
 
                                var answerType:Fight_lessonType = .none
                                if answer.id == selectAnswer.id{
                                                answerType = .success
                                                voicePlayer.playSuccessVoice()
                                                let teamId = listenNewModel.data?.id.components(separatedBy: ",")[page]
                                                rootViewModel.insertCorrectAnswer(teamId: teamId, answerId: selectAnswer.id)
                                }else{
                                                answerType = .fail
                                                voicePlayer.playFailVoice()
                                }
 
                                let tempSubV = stackView.arrangedSubviews[self.playVoiceRealAt!] as! VoiceHandleView
 
                                switch answerType {
                                                case .success:
                                                                answterCount += 1
                                                                rootViewModel.correctNum += 1
                                                                viewModel.answerType.accept(.success)
 
                                                                let copyView = tempSubV.copyView()
                                                                copyView.listenType = .lesson5
                                                                copyView.playUrl = selectAnswer.correct
                                                                let newRect = tempSubV.convert(tempSubV.bounds, to: self.view)
                                                                copyView.frame = CGRect(origin: newRect.origin, size: CGSize(width: 159, height: 52))
                                                                self.view.addSubview(copyView)
                                                                tempSubV.alpha = 0
 
                                                                DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
 
                                                                                //获取Cell的顶部试图
                                                                                let flowLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
 
                                                                                if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as? ListenFight_lesson_1_CCell{
                                                                                                var newRect1 = cell.convert(cell.bounds, to: self.collectionView)
                                                                                                newRect1.origin.x += (collectionView.frame.origin.x + 5)
                                                                                                newRect1.origin.y += 94 + 25
 
                                                                                                UIView.animateKeyframes(withDuration: 0.4, delay: 0,options: .calculationModeLinear) {
                                                                                                                copyView.frame = CGRect(origin: newRect1.origin, size: CGSize(width: flowLayout.itemSize.width - 10 , height: 40))
 
                                                                                                }completion: { _ in
                                                                                                                copyView.playingAction()
                                                                                                                self.playVoiceRealAt = nil
                                                                                                                self.playVoiceAt = nil
                                                                                                                self.collectionView.reloadData()
                                                                                                }
                                                                                }
                                                                }
 
                                                case .fail:
                                                                viewModel.answerType.accept(.fail)
                                                                rootViewModel.errorNum += 1
                                                                collectionView.reloadData()
                                                case .none:
                                                                break
                                }
                }
}
 
extension HomeListenFight_lesson_5_VC:UICollectionViewDelegateFlowLayout{
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
                                return CGSize.zero
                }
}
 
extension HomeListenFight_lesson_5_VC:UICollectionViewDataSource{
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let model = listenNewModel.subjectList[page][indexPath.row]
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as! ListenFight_lesson_1_CCell
                                cell.jq_addShadows(shadowColor: .black.withAlphaComponent(0.31), corner: 8, radius: 3, offset: CGSize(width: 0, height: 1), opacity: 1)
                                if viewModel.selectIndex.value == indexPath{
                                                cell.setState(state: viewModel.answerType.value)
                                }else{
                                                cell.setState(state: .none)
                                }
                                cell.setListen1SubModel(model)
                                return cell
                }
 
                func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
                                let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
                                return reusableView
 
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return listenNewModel.subjectList[page].count
                }
 
                func numberOfSections(in collectionView: UICollectionView) -> Int {
                                return 1
                }
 
}
 
extension HomeListenFight_lesson_5_VC:VoicePlayerDelegate{
                func playComplete() {
                                view.isUserInteractionEnabled = true
                                isListen = true
                                for subV in stackView.arrangedSubviews as! [VoiceHandleView]{
                                                subV.resetView()
                                }
 
                                if viewModel.answerType.value == .success{
                                                let v = rootViewModel.answerCount.value + 1
                                                rootViewModel.answerCount.accept(v)
                                                viewModel.answerType.accept(.none)
                                }
 
                                DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
                                                if self.answterCount >= 4{
//                                                                self.rootViewModel.answerItems[self.page] = self.listenNewModel.subjectList[self.page]
                                                                self.voicePlayer.playerEnd()
                                                                NotificationCenter.default.post(name: NextLession_Noti, object: nil)
                                                }
                                }
                }
                
                func playing() {
                                view.isUserInteractionEnabled = false
                }
 
}