lmw
昨天 9904e5f900ba751c1fe719cdf889f00e9f1418e8
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package com.dollearn.student.ui.home
 
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.View
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.os.bundleOf
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.utils.AudioUtils
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import com.dollearn.student.utils.extention.clickDelay
import kotlinx.android.synthetic.main.fragment_choose_voice.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.support.v4.toast
 
class ChooseVoiceFragment : BaseFragment(), AudioUtils.OnAudioStatusUpdateListener {
    override fun contentViewId() = R.layout.fragment_choose_voice
 
    private val TAG = "Choose=======》"
 
    private var handler:Handler? = null
 
    private val PLAY_VOICE = 1
    private val TO_NEXT = 10
 
    private val PLAY_ERROR = 3
    private val PLAY_RIGHT = 4
 
    private val errorVoice by lazy { SPUtils.instance().getString(Const.EV) }
    private val rightVoice by lazy { SPUtils.instance().getString(Const.RV) }
 
    private var rightPlaying = false
    private var errorPlaying = false
 
    private var voiceList = arrayListOf<String>() //本题音频,每题有3个音频
    private val player by lazy { AudioUtils() }
    private val group by lazy { arguments?.getInt("group")?:0 }
    private val data by lazy {
        (requireActivity() as ChooseVoiceActivity).data
    }
 
    private var currentIndex = 0 //本题组的图片序号,0-3取值
    private var voiceIndex = -1 //点击播放的声音序号 0-2取值 3-6代表下面图片上的语音
 
    private var playing = false //播放中,不能有任何操作
 
    private val nameViews by lazy { arrayListOf(tv_1,tv_2,tv_3,tv_4) }
    private val bgs by lazy { arrayListOf(cl_1,cl_2,cl_3,cl_4) }
    private val endViews by lazy { arrayListOf(cl_voice_end_1,cl_voice_end_2,cl_voice_end_3,cl_voice_end_4) }
 
    private val act by lazy { (requireActivity() as ChooseVoiceActivity) }
 
    private var nowVoiceView:View? = null
 
//    private val isShowText by lazy { SPUtils.instance().getBoolean(Const.IS_SHOW_TEXT,true) }
    private val isShowText = false
 
    private var isAutoPlaying = false //true 自动播放流程
 
    var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误
 
    override fun onFirstVisibleToUser() {
        player.setOnAudioStatusUpdateListener(this)
        player.stopPlayMusic()
        EventBus.getDefault().register(this)
        handler = object : Handler(Looper.getMainLooper()){
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                when(msg.what){
                    PLAY_VOICE->{
                        player.startPlayMusic(requireContext(),voiceList[voiceIndex])
                    }
                    TO_NEXT->{
                        if (isAdded&&!playing)
                            (requireActivity() as ChooseVoiceActivity).next()
                    }
                    PLAY_RIGHT->{
                        rightPlaying = true
                        player.startPlayMusic(requireContext(),rightVoice)
                    }
                    PLAY_ERROR->{
                        errorPlaying = true
                        player.startPlayMusic(requireContext(),errorVoice)
                    }
                }
            }
        }
        data?.apply {
            val originList = subjectList[group]
            iv_1.setImageURI(originList[0].img)
            iv_2.setImageURI(originList[1].img)
            iv_3.setImageURI(originList[2].img)
            iv_4.setImageURI(originList[3].img)
        }
        initClick()
        showSubImage()
 
    }
 
    private fun initClick() {
        cl_voice1.clickDelay {
            if (playing)
                return@clickDelay
            voiceIndex = 0
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
 
        cl_voice2.clickDelay {
            if (playing)
                return@clickDelay
            voiceIndex = 1
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
 
        cl_voice3.clickDelay {
            if (playing)
                return@clickDelay
            voiceIndex = 2
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
 
        rl_check_1.clickDelay {
            if (playing||data!!.subjectList[group].filter { it.completed }.size == data!!.subjectList[group].size)
                return@clickDelay
            if (rl_check_2.visibility == View.VISIBLE&&rl_check_3.visibility == View.VISIBLE){
                dot_1.visible()
                act.totalCount++
                if (voiceList[0] == data!!.subjectList[group][currentIndex].correct){
                    act.rightCount++
                    motion.transitionToEnd()
                    nowVoiceView = cl_voice1
                    handler?.sendEmptyMessage(PLAY_RIGHT)
                }else{
                    right = false
                    handler?.sendEmptyMessage(PLAY_ERROR)
                    iv_error_1.visible()
                    dot_1.postDelayed({
                        iv_error_1.gone()
                        dot_1.gone()
                        rl_check_1.gone()
                        rl_check_2.gone()
                        rl_check_3.gone()
                    },2000)
                }
            }else
                myToast("请听完题")
        }
 
        rl_check_2.clickDelay {
            if (playing||data!!.subjectList[group].filter { it.completed }.size == data!!.subjectList[group].size)
                return@clickDelay
            if (rl_check_1.visibility == View.VISIBLE&&rl_check_3.visibility == View.VISIBLE) {
                dot_2.visible()
                act.totalCount++
                if (voiceList[1] == data!!.subjectList[group][currentIndex].correct){
                    act.rightCount++
                    motion.transitionToEnd()
                    nowVoiceView = cl_voice2
                    handler?.sendEmptyMessage(PLAY_RIGHT)
                }else{
 
                    handler?.sendEmptyMessage(PLAY_ERROR)
                    iv_error_2.visible()
                    dot_1.postDelayed({
                        dot_2.gone()
                        iv_error_2.gone()
                        rl_check_1.gone()
                        rl_check_2.gone()
                        rl_check_3.gone()
                    },2000)
                }
            }else
                myToast("请听完题")
 
        }
        rl_check_3.clickDelay {
            if (playing||data!!.subjectList[group].filter { it.completed }.size == data!!.subjectList[group].size)
                return@clickDelay
            if (rl_check_2.visibility == View.VISIBLE&&rl_check_3.visibility == View.VISIBLE) {
                dot_3.visible()
                act.totalCount++
                if (voiceList[2] == data!!.subjectList[group][currentIndex].correct){
                    act.rightCount++
                    nowVoiceView = cl_voice3
                    handler?.sendEmptyMessage(PLAY_RIGHT)
                    motion.transitionToEnd()
                }else{
                    handler?.sendEmptyMessage(PLAY_ERROR)
                    iv_error_3.visible()
                    dot_1.postDelayed({
                        iv_error_3.gone()
                        dot_3.gone()
                        rl_check_1.gone()
                        rl_check_2.gone()
                        rl_check_3.gone()
                    },2000)
                }
            }else
                myToast("请听完题")
 
        }
 
        cl_voice_end_1.clickDelay {
            if (!playing){
                voiceIndex = 3
                player.startPlayMusic(requireContext(),data!!.subjectList[group][0].correct)
            }
        }
 
        cl_voice_end_2.clickDelay {
            if (!playing){
                voiceIndex = 4
                player.startPlayMusic(requireContext(),data!!.subjectList[group][1].correct)
            }
        }
 
        cl_voice_end_3.clickDelay {
            if (!playing){
                voiceIndex = 5
                player.startPlayMusic(requireContext(),data!!.subjectList[group][2].correct)
            }
        }
 
        cl_voice_end_4.clickDelay {
            if (!playing){
                voiceIndex = 6
                player.startPlayMusic(requireContext(),data!!.subjectList[group][3].correct)
            }
        }
    }
 
    /**
     * 展示题组内的小题
     */
    private fun showSubImage(){
        rl_check_1.gone()
        rl_check_2.gone()
        rl_check_3.gone()
        dot_1.gone()
        dot_2.gone()
        dot_3.gone()
        val originList = data?.subjectList!![group]
        val imageNow = originList[currentIndex]
        val list = arrayListOf<String>()
        list.add(imageNow.correct)
        list.addAll((imageNow.error?:"").split(","))
        val shuffledList = list.shuffled()
        voiceList.clear()
        voiceList.addAll(shuffledList)
            nameViews.forEachIndexed { index, textView ->
                if (index == currentIndex){
                    bgs[index].backgroundResource = R.drawable.bg_orange_8dp
                    if (isShowText){
                        textView.text = originList[index].name
                        textView.backgroundResource = R.color.textColor99
                    }
                }else{
                    bgs[index].backgroundResource = R.drawable.bg_white_8dp_padding
                    textView.text = ""
                    textView.backgroundResource = R.color.page_bg
                }
            }
        //设置动画飞行路径 3对4
        val rightVoiceIndex = voiceList.indexOf(imageNow.correct)
        var start = 0
        var end = 0
        when(rightVoiceIndex){
            0->{
                start = R.id.start_1
                when(currentIndex){
                    0-> end = R.id.end_1_1
                    1-> end = R.id.end_1_2
                    2-> end = R.id.end_1_3
                    3-> end = R.id.end_1_4
                }
            }
            1->{
                start = R.id.start_2
                when(currentIndex){
                    0-> end = R.id.end_2_1
                    1-> end = R.id.end_2_2
                    2-> end = R.id.end_2_3
                    3-> end = R.id.end_2_4
                }
            }
            2->{
                start = R.id.start_3
                when(currentIndex){
                    0-> end = R.id.end_3_1
                    1-> end = R.id.end_3_2
                    2-> end = R.id.end_3_3
                    3-> end = R.id.end_3_4
                }
            }
        }
        motion.progress = 0f
        motion.setTransition(start,end)
        motion.setTransitionListener(object :MotionLayout.TransitionListener{
            override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
 
            }
 
            override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
            }
 
            override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
                endViews[currentIndex].visible()
 
            }
 
            override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
            }
        })
 
        if (act.fragments[act.vp.currentItem] == this&&act.recoverd){ //自动播放
            isAutoPlaying = true
            cl_voice1.callOnClick()
        }
 
    }
 
    @Subscribe
    fun onEvent(e:EmptyEvent){
        if (e.code == Const.EventCode.RECOVERD){
            if (act.fragments[act.vp.currentItem] == this){ //自动播放
                isAutoPlaying = true
                cl_voice1.callOnClick()
            }
        }
    }
 
    /**
     * 回到上一题,恢复状态
     */
    fun recover(){
        Log.e(TAG,"回到上一题,恢复答题前的状态")
        right = true
        motion.progress = 0f
        currentIndex = 0
        endViews.forEach { it.gone() }
        data!!.subjectList[group].forEach { it.completed = false }
        showSubImage()
    }
 
    companion object{
        /**
         * @param group 题组号
         * @param index 组内序号
         */
        fun getInstance(group:Int):ChooseVoiceFragment{
            val listenFragment = ChooseVoiceFragment()
            listenFragment.arguments = bundleOf("group" to group)
            return listenFragment
        }
    }
 
    override fun onUpdate(db: Double, time: Long) {
    }
 
    override fun onStop(filePath: String?) {
    }
 
 
    override fun onStartPlay() {
        playing = true
        if (!errorPlaying&&!rightPlaying){
            if (voiceIndex == 0){
                iv1_1.gone()
                iv2_1.gone()
                iv_playing_1.visible()
            }
            if (voiceIndex == 1){
                iv1_2.gone()
                iv2_2.gone()
                iv_playing_2.visible()
            }
            if (voiceIndex == 2){
                iv1_3.gone()
                iv2_3.gone()
                iv_playing_3.visible()
            }
            if (voiceIndex == 3){
                iv1_1_end.gone()
                iv2_1_end.gone()
                iv_playing_1_end.visible()
            }
            if (voiceIndex == 4){
                iv1_2_end.gone()
                iv2_2_end.gone()
                iv_playing_2_end.visible()
            }
            if (voiceIndex == 5){
                iv1_3_end.gone()
                iv2_3_end.gone()
                iv_playing_3_end.visible()
            }
            if (voiceIndex == 6){
                iv1_4_end.gone()
                iv2_4_end.gone()
                iv_playing_4_end.visible()
            }
        }
    }
 
    override fun onFinishPlay() {
        playing = false
        if (voiceIndex == 0){
            rl_check_1.visible()
            iv1_1.visible()
            iv2_1.visible()
            iv_playing_1.gone()
            if (isAutoPlaying)
                cl_voice2.callOnClick()
        }else if (voiceIndex == 1){
            rl_check_2.visible()
            iv1_2.visible()
            iv2_2.visible()
            iv_playing_2.gone()
            if (isAutoPlaying)
                cl_voice3.callOnClick()
        }else if (voiceIndex == 2){
            rl_check_3.visible()
            iv1_3.visible()
            iv2_3.visible()
            iv_playing_3.gone()
            if (isAutoPlaying)
                isAutoPlaying = false
        }
        if (voiceIndex == 3){
            iv1_1_end.visible()
            iv2_1_end.visible()
            iv_playing_1_end.gone()
        }
        if (voiceIndex == 4){
            iv1_2_end.visible()
            iv2_2_end.visible()
            iv_playing_2_end.gone()
        }
        if (voiceIndex == 5){
            iv1_3_end.visible()
            iv2_3_end.visible()
            iv_playing_3_end.gone()
        }
        if (voiceIndex == 6){
            iv1_4_end.visible()
            iv2_4_end.visible()
            iv_playing_4_end.gone()
        }
 
        if (data!!.subjectList[group][currentIndex].completed){ //本题已答对
            Log.e(TAG,"选对答案后的播放结束3秒后进入下一图")
            if (currentIndex == 3){
                handler?.sendEmptyMessageDelayed(TO_NEXT,500)//万俊杰:缩短为0.5秒
            }else{
                currentIndex++
                showSubImage()
            }
        }
 
        if (rightPlaying){
            rightPlaying = false
            data!!.subjectList[group][currentIndex].completed = true
            nowVoiceView?.callOnClick()
        }
        if (errorPlaying)
            errorPlaying = false
    }
 
    override fun onPause() {
        super.onPause()
        player.stopPlayMusic()
    }
 
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        handler?.removeCallbacksAndMessages(null)
    }
}