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
package com.dollearn.student.ui.home
 
import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.invisible
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.extention.clickDelay
import kotlinx.android.synthetic.main.fragment_q_a.*
import org.jetbrains.anko.support.v4.dip
 
class QAFragment:BaseFragment(), AudioUtils.OnAudioStatusUpdateListener {
    override fun contentViewId() = R.layout.fragment_q_a
 
    private val TAG = "QA=======》"
 
    private var handler: Handler? = null
    private val PLAY_VOICE = 1
    private val TO_NEXT = 2
 
    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 val player by lazy { AudioUtils() }
    private val group by lazy { arguments?.getInt("group")?:0 }
    private val data by lazy {
        (requireActivity() as QAActivity).data
    }
    private val list by lazy {  //题组内图片
        data!!.subjectList[group]
    }
 
    private val act by lazy { requireActivity() as QAActivity }
 
    private val voiceViews by lazy { arrayListOf(cl_voice_1,cl_voice_2,cl_voice_3,cl_voice_4) } //语音蓝条
    private val v1Views by lazy { arrayListOf(iv1_1,iv1_2,iv1_3,iv1_4,iv1_answer_1,iv1_answer_2,iv1_answer_3) }
    private val v2Views by lazy { arrayListOf(iv2_1,iv2_2,iv2_3,iv2_4,iv2_answer_1,iv2_answer_2,iv2_answer_3) }
    private val resultViews by lazy { arrayListOf(iv_right_1,iv_right_2,iv_right_3,iv_right_4) }
    private val playingViews by lazy { arrayListOf(iv_playing_1,iv_playing_2,iv_playing_3,iv_playing_4,iv_playing_answer_1,iv_playing_answer_2,iv_playing_answer_3) }
 
    private var voiceIndex = -1 //点击播放的声音序号 0-3取值
    private var questionIndex = -1 //当前题号
 
    private var playing = false
 
    private var layoutChanged = false
 
    private var answerList = arrayListOf<String>() //右侧答案语音
 
    private var nowVoiceView: View? = null
 
    private var isAutoPlaying = 0
 
    var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误
 
 
    override fun onFirstVisibleToUser() {
        player.setOnAudioStatusUpdateListener(this)
        player.stopPlayMusic()
        handler = object : Handler(Looper.getMainLooper()){
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                when(msg.what){
                    PLAY_VOICE->{
                        playing = true
                        if (voiceIndex in 0..3)
                            player.startPlayMusic(requireContext(),list[voiceIndex].correct)
                        else
                            player.startPlayMusic(requireContext(),answerList[voiceIndex-4])
                    }
                    TO_NEXT->{
                        if (isAdded&&!playing){
                            (requireActivity() as QAActivity).next()
                        }
                    }
                    PLAY_RIGHT->{
                        rightPlaying = true
                        player.startPlayMusic(requireContext(),rightVoice)
                    }
                    PLAY_ERROR->{
                        errorPlaying = true
                        player.startPlayMusic(requireContext(),errorVoice)
                    }
                }
            }
        }
        iv_1.setImageURI(list[0].img)
        iv_2.setImageURI(list[1].img)
        iv_3.setImageURI(list[2].img)
        iv_4.setImageURI(list[3].img)
        recover()
        rl_check_1.clickDelay {
            if (playing)
                return@clickDelay
            if (list[questionIndex].completed) //已作答
                return@clickDelay
            if (!rl_check_1.isSelected||!rl_check_2.isSelected||!rl_check_3.isSelected){
                myToast("请先听完")
                return@clickDelay
            }
            act.totalCount++
            if (list[questionIndex].correct == answerList[0]){
                act.rightCount++
                Log.e(TAG,"选择正确")
                transAnimation(cl_answer_1,voiceViews[questionIndex])
                val resultView = resultViews[questionIndex]
                resultView.visible()
                resultView.postDelayed({
                    resultView.gone()
                },2000)
                nowVoiceView = voiceViews[questionIndex]
                handler?.sendEmptyMessage(PLAY_RIGHT)
            }else{
                right = false
                recoverAnswer()
                handler?.sendEmptyMessage(PLAY_ERROR)
                dot_1.visible()
                iv_error_1.visible()
                dot_1.postDelayed({
                    dot_1.gone()
                    iv_error_1.gone()
                },2000)
            }
        }
        rl_check_2.clickDelay {
            if (playing)
                return@clickDelay
            if (list[questionIndex].completed) //已作答
                return@clickDelay
            if (!rl_check_1.isSelected||!rl_check_2.isSelected||!rl_check_3.isSelected){
                myToast("请先听完")
                return@clickDelay
            }
            act.totalCount++
            if (list[questionIndex].correct == answerList[1]){
                act.rightCount++
                Log.e(TAG,"选择正确")
                transAnimation(cl_answer_2,voiceViews[questionIndex])
                val resultView = resultViews[questionIndex]
                resultView.visible()
                resultView.postDelayed({
                    resultView.gone()
                },2000)
                nowVoiceView = voiceViews[questionIndex]
                handler?.sendEmptyMessage(PLAY_RIGHT)
            }else{
                recoverAnswer()
                handler?.sendEmptyMessage(PLAY_ERROR)
                dot_2.visible()
                iv_error_2.visible()
                dot_2.postDelayed({
                    dot_2.gone()
                    iv_error_2.gone()
                },2000)
            }
        }
        rl_check_3.clickDelay {
            if (playing)
                return@clickDelay
            if (list[questionIndex].completed) //已作答
                return@clickDelay
            if (!rl_check_1.isSelected||!rl_check_2.isSelected||!rl_check_3.isSelected){
                myToast("请先听完")
                return@clickDelay
            }
            act.totalCount++
            if (list[questionIndex].correct == answerList[2]){
                act.rightCount++
                Log.e(TAG,"选择正确")
                transAnimation(cl_answer_3,voiceViews[questionIndex])
                val resultView = resultViews[questionIndex]
                resultView.visible()
                resultView.postDelayed({
                    resultView.gone()
                },2000)
                nowVoiceView = voiceViews[questionIndex]
                handler?.sendEmptyMessage(PLAY_RIGHT)
            }else{
                recoverAnswer()
                handler?.sendEmptyMessage(PLAY_ERROR)
                dot_3.visible()
                iv_error_3.visible()
                dot_3.postDelayed({
                    dot_3.gone()
                    iv_error_3.gone()
                },2000)
            }
        }
 
        isAutoPlaying = 1
        voiceViews.filterIndexed { index, constraintLayout ->
            val subject = list[index]
            constraintLayout.isEnabled&&subject.isQuestion == 1
        }.firstOrNull()?.callOnClick()
    }
 
 
    /**
     * 恢复答案和题目ui
     */
    fun recover(){
        right = true
        showVoiceUi()
        list.forEach { it.completed = false }
        recoverAnswer()
    }
 
    /**
     * 只恢复答案ui
     */
    private fun recoverAnswer(){
        rl_check_1.isSelected = false
        rl_check_2.isSelected = false
        rl_check_3.isSelected = false
        if (list.filter { it.completed }.isEmpty()){
            voiceViews[2].isEnabled = false
            voiceViews[3].isEnabled = false
        }else{
            voiceViews[2].isEnabled = true
            voiceViews[3].isEnabled = true
        }
    }
 
    private fun showVoiceUi(){
        list.forEachIndexed { index, subject ->
            if (subject.isQuestion == 1){
                voiceViews[index].visible()
            }else{
                voiceViews[index].invisible()
            }
            voiceViews[index].clickDelay {
                if (!playing){
                    voiceIndex = index
                    handler?.sendEmptyMessage(PLAY_VOICE)
                }
            }
        }
    }
 
    private fun showAnswerView(position:Int){
        if (!layoutChanged){ //布局未改变,执行属性动画
            val anim = ValueAnimator.ofInt(dip(120))
            anim.duration = 500
            anim.addUpdateListener {
                val i = it.animatedValue as Int
                val layoutParams = center.layoutParams as ConstraintLayout.LayoutParams
                layoutParams.marginEnd = i
                center.layoutParams = layoutParams
            }
            anim.start()
            layoutChanged = true
        }
        questionIndex = position
        if (questionIndex == 0||questionIndex == 2){
            iv2_answer_1.setImageResource(R.mipmap.voice_question_2)
            iv2_answer_2.setImageResource(R.mipmap.voice_question_2)
            iv2_answer_3.setImageResource(R.mipmap.voice_question_2)
        }else{
            iv2_answer_1.setImageResource(R.mipmap.voice_answer_2)
            iv2_answer_2.setImageResource(R.mipmap.voice_answer_2)
            iv2_answer_3.setImageResource(R.mipmap.voice_answer_2)
        }
        rl_check_1.visible()
        rl_check_2.visible()
        rl_check_3.visible()
        cl_answer_1.visible()
        cl_answer_2.visible()
        cl_answer_3.visible()
        answerList.clear()
        answerList.add(list[position].correct)
        answerList.addAll(list[position].error?.split(",")?: arrayListOf())
        answerList.shuffle()
        cl_answer_1.setOnClickListener {
            if (playing)
                return@setOnClickListener
            voiceIndex = 4
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
        cl_answer_2.setOnClickListener {
            if (playing)
                return@setOnClickListener
            voiceIndex = 5
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
        cl_answer_3.setOnClickListener {
            if (playing)
                return@setOnClickListener
            voiceIndex = 6
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
    }
 
 
    private fun transAnimation(v:View,endView: View){
        val offx = endView.left - v.left
        val offy = endView.top - v.top
        val x = PropertyValuesHolder.ofFloat("translationX",0f,offx.toFloat())
        val y = PropertyValuesHolder.ofFloat("translationY",0f,offy.toFloat())
        val animator = ObjectAnimator.ofPropertyValuesHolder(v, x, y)
        animator.duration = 500
        animator.interpolator = LinearInterpolator()
        animator.start()
        animator.addListener(object :Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator?) {
 
            }
 
            override fun onAnimationEnd(animation: Animator?) {
                endView.visible()
                v.translationX = 0f
                v.translationY = 0f
                rl_check_1.gone()
                rl_check_2.gone()
                rl_check_3.gone()
                cl_answer_1.gone()
                cl_answer_2.gone()
                cl_answer_3.gone()
            }
 
            override fun onAnimationCancel(animation: Animator?) {
            }
 
            override fun onAnimationRepeat(animation: Animator?) {
            }
        })
    }
 
 
    companion object{
        /**
         * @param group 题组号
         * @param index 组内序号
         */
        fun getInstance(group: Int):QAFragment{
            val listenFragment = QAFragment()
            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 (rightPlaying||errorPlaying)
            return
        v1Views[voiceIndex].gone()
        v2Views[voiceIndex].gone()
        playingViews[voiceIndex].visible()
        if (voiceIndex == 0&&list[0].isQuestion == 1&&!list[1].completed){
            showAnswerView(1)
        }
        if (voiceIndex == 1&&list[1].isQuestion == 1&&!list[0].completed){
            showAnswerView(0)
 
        }
        if (voiceIndex == 2&&list[2].isQuestion == 1&&!list[3].completed){
            showAnswerView(3)
        }
        if (voiceIndex == 3&&list[3].isQuestion == 1&&!list[2].completed){
            showAnswerView(2)
        }
    }
 
    override fun onFinishPlay() {
        playing = false
        v1Views[voiceIndex].visible()
        v2Views[voiceIndex].visible()
        playingViews[voiceIndex].gone()
        if (!errorPlaying&&!rightPlaying&&!list[questionIndex].completed){
            if (voiceIndex == 4){
                rl_check_1.isSelected = true
            }
            if (voiceIndex == 5){
                rl_check_2.isSelected = true
            }
            if (voiceIndex == 6){
                rl_check_3.isSelected = true
            }
        }
 
        if (isAutoPlaying in 1..2){
            if (voiceIndex in 0..3){
                cl_answer_1.callOnClick()
            }else if (voiceIndex == 4)
                cl_answer_2.callOnClick()
            else if (voiceIndex == 5)
                cl_answer_3.callOnClick()
            else if (voiceIndex == 6){
                isAutoPlaying = if (isAutoPlaying == 1) 0 else -1
            }
        }
 
        if (errorPlaying)
            errorPlaying = false
        else if (rightPlaying){
            rightPlaying = false
            nowVoiceView?.callOnClick()
            list[questionIndex].completed = true
            recoverAnswer()
        }else if (list.filter { it.completed }.size == 2){ //全部作答,3秒后下一题
            handler?.sendEmptyMessageDelayed(TO_NEXT,500) //万俊杰:缩短为0.5秒
        }else if (list.filter { it.completed }.size == 1&& isAutoPlaying == 0){//答完一题,自动播放下一题
            isAutoPlaying = 2
            voiceViews.filterIndexed { index, constraintLayout ->
                val subject = list[index]
                constraintLayout.isEnabled&&subject.isQuestion == 1
            }.lastOrNull()?.callOnClick()
        }
    }
 
    override fun onPause() {
        super.onPause()
        player.stopPlayMusic()
    }
 
    override fun onDestroy() {
        super.onDestroy()
        handler?.removeCallbacksAndMessages(null)
    }
}