无故事王国
5 天以前 510991a68acc59e22602d95582e3b1cabdd093c2
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
//
//  HomeListenStory_1_VC.swift
//  DolphinEnglishLearnStudent
//
//  Created by 无故事王国 on 2024/5/30.
//
 
import UIKit
 
class HomeListenStory_1_VC: BaseVC {
 
                private var viewModel = FightAnswerViewModel()
 
                private var listen1Model:Listen1Model!
                private var page:Int!
                var rootViewModel:HomeListenFightViewModel!
 
                private lazy var stackView:UIStackView = {
                                let stackView = UIStackView()
                                stackView.spacing = 78
                                stackView.distribution = .equalSpacing
                                return stackView
                }()
 
                private lazy var collectionView:UICollectionView = {
                                let flowLayout = UICollectionViewFlowLayout()
                                let w = (JQ_ScreenW - 235 * 2)
                                flowLayout.itemSize = CGSize(width: w, height: w * 0.6666)
                                flowLayout.minimumInteritemSpacing = 0
                                flowLayout.scrollDirection = .vertical
                                let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
                                collection.contentInset = UIEdgeInsets(top: 130, left: 0, bottom: 0, right: 0)
                                collection.register(UINib(nibName: "SimpleImageCCell", bundle: nil), forCellWithReuseIdentifier: "_SimpleImageCCell")
                                return collection
                }()
 
                private var voicePlayer = VoicePlayer.share()
 
                private var isPlayedSet = Set<Int>()
 
                required init(page:Int,listen1Model:Listen1Model){
                                super.init(nibName: nil, bundle: nil)
                                self.page = page
                                self.listen1Model = listen1Model
                }
 
 
                override func viewDidAppear(_ animated: Bool) {
                                super.viewDidAppear(animated)
                                voicePlayer.delegate = self
                }
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                voicePlayer.delegate = nil
                                voicePlayer.playerInterrupt()
                }
 
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
 
                override func setUI() {
                                super.setUI()
 
                                viewModel.selectIndex.accept(IndexPath(row: 0, section: 0))
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.isScrollEnabled = false
                                collectionView.showsVerticalScrollIndicator = false
                                collectionView.backgroundColor = .clear
                                collectionView.jq_addShadows(shadowColor: UIColor.black.withAlphaComponent(0.1), corner: 8, radius: 10, offset: CGSize(width: 0, height: 2), opacity: 1)
                                view.addSubview(collectionView)
                                collectionView.snp.makeConstraints { make in
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(0)
                                                make.left.equalTo(164)
                                                make.width.equalTo(JQ_ScreenW - 164 * 2)
                                                make.bottom.equalToSuperview()
                                }
 
                                view.addSubview(stackView)
                                stackView.snp.makeConstraints { make in
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(24)
                                                make.centerX.equalToSuperview()
                                                make.height.equalTo(52)
                                                make.width.greaterThanOrEqualTo(100)
                                }
 
                                stackView.removeArrangedSubviews()
 
                                var tempHandleViews = [StudyHandleView]()
                                for index in 0...2{
                                                let handleView = StudyHandleView.jq_loadNibView()
                                                handleView.listenType = .lesson2
 
                                                if index == 0{
                                                                handleView.vioceSoundUrl = listen1Model.storyList[page].correct
                                                }else if index == 1{
                                                                handleView.vioceSoundUrl = listen1Model.storyList[page].error.components(separatedBy: ",").first
                                                }else{
                                                                handleView.vioceSoundUrl = listen1Model.storyList[page].error.components(separatedBy: ",").last
                                                }
                                                handleView.btn_choose.tag = 10 + index
                                                handleView.tag = 20 + index
                                                handleView.chooseClouse {[weak self] btn in
                                                                guard let weakSelf = self else { return }
                                                                if weakSelf.isPlayedSet.count != 3{
                                                                                handleView.btn_choose.isSelected = false
                                                                                alertError(msg: "请听完");return
                                                                }
 
                                                                var lessionType:Fight_lessonType = .none
                                                                if weakSelf.listen1Model.storyList[weakSelf.page].correct == handleView.vioceSoundUrl{
                                                                                lessionType = .success
                                                                                weakSelf.voicePlayer.playSuccessVoice()
                                                                }else{
                                                                                lessionType = .fail
                                                                                weakSelf.voicePlayer.playFailVoice()
                                                                }
 
                                                                switch lessionType {
                                                                                case .success:
                                                                                                weakSelf.viewModel.answerType.accept(.success)
                                                                                                weakSelf.rootViewModel.correctNum += 1
                                                                                                handleView.btn_choose.isSelected = true
                                                                                                handleView.btn_state.setImage(UIImage(named: "icon_success_small"), for: .normal)
                                                                                                UIView.animate(withDuration: 0.5) {
                                                                                                                handleView.btn_state.alpha = 1
                                                                                                }
 
                                                                                                UIView.animate(withDuration: 0.5, delay: 3) {
                                                                                                                handleView.btn_state.alpha = 0
                                                                                                }
 
                                                                                                weakSelf.voicePlayer.playerAt(url: handleView.vioceSoundUrl)
                                                                                                let copyView = handleView.copyView()
                                                                                                copyView.btn_choose.alpha = 0
                                                                                                copyView.listenType = .lesson2
                                                                                                copyView.resetView()
                                                                                                copyView.isplaying()
                                                                                                let newRect = handleView.convert(handleView.bounds, to: weakSelf.view)
                                                                                                weakSelf.view.addSubview(copyView)
                                                                                                copyView.snp.makeConstraints { make in
                                                                                                                make.top.equalTo(newRect.origin.y)
                                                                                                                make.left.equalTo(newRect.origin.x)
                                                                                                                make.width.equalTo(221)
                                                                                                                make.height.equalTo(52)
                                                                                                }
                                                                                                weakSelf.view.layoutIfNeeded()
 
                                                                                                let rect = weakSelf.collectionView.convert(weakSelf.collectionView.bounds, to: weakSelf.view)
 
                                                                                                UIView.animate(withDuration: 0.5) {
                                                                                                                copyView.snp.remakeConstraints { make in
                                                                                                                                make.top.equalTo(rect.origin.y + 135)
                                                                                                                                make.centerX.equalToSuperview().offset(30)
                                                                                                                                make.width.equalTo((JQ_ScreenW - 210 * 2))
                                                                                                                                make.height.equalTo(52)
                                                                                                                }
                                                                                                                weakSelf.view.layoutIfNeeded()
                                                                                                }
 
                                                                                case .fail:
                                                                                                                weakSelf.isPlayedSet.removeAll()
                                                                                                for subV in weakSelf.stackView.arrangedSubviews as! [StudyHandleView]{
                                                                                                                subV.view_choose.alpha = 0
                                                                                                                subV.btn_choose.isSelected = false
                                                                                                }
 
                                                                                                weakSelf.viewModel.answerType.accept(.fail)
                                                                                                weakSelf.rootViewModel.errorNum += 1
                                                                                                handleView.btn_state.setImage(UIImage(named: "icon_waring_small"), for: .normal)
                                                                                                UIView.animate(withDuration: 0.5) {
                                                                                                                handleView.btn_state.alpha = 1
                                                                                                }
                                                                                                DispatchQueue.main.asyncAfter(deadline:.now()+2) {
                                                                                                                UIView.animate(withDuration: 0.5) {
                                                                                                                                handleView.btn_state.alpha = 0
                                                                                                                                handleView.btn_choose.isSelected = false
                                                                                                                }
                                                                                                }
 
 
                                                                                default:
                                                                                                handleView.btn_state.setImage(nil, for: .normal)
                                                                }
                                                }
 
                                                handleView.playAt {[weak self] index in
                                                                self?.isPlayedSet.insert(index)
                                                                print("播放过的\(index)")
                                                }
 
                                                handleView.snp.makeConstraints { make in
                                                                make.height.equalTo(52)
                                                                make.width.greaterThanOrEqualTo(221)
                                                }
                                                tempHandleViews.append(handleView)
                                }
 
                                tempHandleViews.shuffle()
                                stackView.addArrangedSubviews(tempHandleViews)
                }
 
                func restore(){
                                viewModel.answerType.accept(.none)
                                isPlayedSet.removeAll()
                                for v in stackView.arrangedSubviews as! [StudyHandleView]{
                                                v.resetView()
                                                v.view_choose.alpha = 0
                                }
                                setUI()
                                collectionView.reloadData()
                }
}
 
extension HomeListenStory_1_VC:UICollectionViewDelegate{
 
}
 
extension HomeListenStory_1_VC:UICollectionViewDelegateFlowLayout{
 
}
 
extension HomeListenStory_1_VC:UICollectionViewDataSource{
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let m = listen1Model.storyList[page]
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_SimpleImageCCell", for: indexPath) as! SimpleImageCCell
                                cell.img_cover.sd_setImage(with: URL(string: m.img))
                                cell.jq_cornerRadius = 10
                                return cell
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return 1
                }
 
                func numberOfSections(in collectionView: UICollectionView) -> Int {
                                return 1
                }
 
}
 
extension HomeListenStory_1_VC:VoicePlayerDelegate{
                func playComplete() {
                                view.isUserInteractionEnabled = true
                                if viewModel.answerType.value == .success{
                                                DispatchQueue.main.asyncAfter(delay: 3.0) {
                                                                var dict = Dictionary<String,Any>()
                                                                dict["storyIntegral"] = self.listen1Model.data?.integral ?? 0
                                                                dict["storyId"] = self.listen1Model.data?.id ?? 0
                                                                NotificationCenter.default.post(name: NextLession_Noti, object: dict)
                                                }
                                                return
                                }
 
                                for v in stackView.arrangedSubviews as! [StudyHandleView]{
                                                v.resetView()
                                                v.view_choose.alpha = isPlayedSet.contains(v.tag) ? 1:0
                                }
                }
 
                func playing() {
                                view.isUserInteractionEnabled = false
                }
}