lmw
2024-06-18 1f45a54dc8e149548d3a61d1228741627aa4f23e
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
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.recyclerview.widget.GridLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.dialog.CountdownDialog
import com.dollearn.student.dialog.DifficultyDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.GameBean
import com.dollearn.student.network.entity.Subject
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.adapter.GameAdapter
import com.dollearn.student.utils.AudioUtils
import com.dollearn.student.utils.interfaces.StringCallback
import kotlinx.android.synthetic.main.activity_super_listen.*
import kotlinx.android.synthetic.main.fragmetn_look_img.*
 
class SuperListenActivity : TransparentStatusBarActivity(), AudioUtils.OnAudioStatusUpdateListener {
    override fun setContentView() = R.layout.activity_super_listen
 
 
    private val week by lazy {
        intent.getIntExtra("week",0)
    }
    private val season by lazy {
        intent.getIntExtra("season",0)
    }
    private var data:GameBean? = null
    private var index = 0
    private var handler:Handler? = null
 
    private var difficulty = 0 //难度
 
    private val TAG = "Listen====>"
 
    private val list = arrayListOf<Subject>()
    private val adapter = GameAdapter(list)
 
    private val PLAY_VOICE = 1
    private val COUNT_DOWN = 2
    private val STUDY_TIME = 3
 
    private val player by lazy { AudioUtils() }
 
    private val voiceList = arrayListOf<String>() //声音按顺序播放
 
    private var TIME = 10 //每题答题时间
    private var countTime = 10 //倒计时计数
 
    var totalCount = 0 //总答题次数
    var rightCount = 0 //正确答题次数
    var time = 0 //学习秒数
 
    override fun initClick() {
        adapter.setOnItemClickListener { view, position ->
            Log.e(TAG,"点击图片:${list[position].name}")
            Log.e(TAG,"音频position:${index}")
            Log.e(TAG,"音频路径:${voiceList[index]}")
 
            val subject = data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])] //当前音频对应题目
            Log.e(TAG,"正确答案:${subject.name}")
 
            totalCount++
 
            handler?.removeMessages(COUNT_DOWN) //选择答案后,停止倒计时
            if (list[position].id == subject.id){
                rightCount++
                subject.completed = true
                subject.right = true
                handler?.sendEmptyMessage(PLAY_VOICE)
                adapter.notifyItemChanged(position)
            }else{
                subject.completed = true
                subject.right = false
                index++
                startGame()
            }
        }
 
        tv_exit.setOnClickListener {
            if (tv_exit.text == "提交"){
                ResultActivity.startResult(this,0,0,0,6,totalCount,rightCount,list.filter { it.right }.sumBy { 1 },time,data!!.data.id)
                finish()
            }else
                finish()
        }
    }
 
    override fun initView() {
        tv_sort.postDelayed({
             val difficultyDialog = DifficultyDialog()
            difficultyDialog.setCallback(object :DifficultyDialog.OnClickCallback{
                override fun onOk(d: Int) {
                    difficulty = d
                    HttpManager.gameHearing(season,week,difficulty).request(this@SuperListenActivity){_,data->
                        difficultyDialog.dismissAllowingStateLoss()
                        this@SuperListenActivity.data = data
                        refreshUi()
                    }
                }
 
                override fun onCancel() {
                    finish()
                }
            })
            difficultyDialog.show(supportFragmentManager,"dif")
        },500)
 
        player.setOnAudioStatusUpdateListener(this)
        player.stopPlayMusic()
        handler = object : Handler(Looper.getMainLooper()){
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                when(msg.what){
                    STUDY_TIME->{
                        time++
                        sendEmptyMessageDelayed(STUDY_TIME,1000)
                    }
                    PLAY_VOICE->{
                        if (index<voiceList.size)
                            player.startPlayMusic(this@SuperListenActivity,voiceList[index])
                    }
                    COUNT_DOWN->{
                        countTime --
                        tv_tip.text = "请在${countTime}s内选择答案!"
                        if ( countTime == 0){
                            index++
                            startGame()
                        }else{
                            sendEmptyMessageDelayed(COUNT_DOWN,1000)
                        }
                    }
                }
            }
        }
    }
 
    private fun refreshUi() {
        rv_list.layoutManager = GridLayoutManager(this,5)
        list.clear()
        list.addAll(data?.subjectList?: arrayListOf())
        voiceList.clear()
        voiceList.addAll(list.map { it.correct }) //声音顺序
        Log.e(TAG,"声音顺序:${voiceList.joinToString(",,,") { it }}")
        list.shuffle() //图片随机顺序
        rv_list.adapter = adapter
        Log.e(TAG,"图片打乱后顺序:${list.joinToString(",,,") { it.name }}")
 
        val countdownDialog = CountdownDialog()
        countdownDialog.callback = object :StringCallback{
            override fun onResult(rst: String) {
                index = 0
                startGame()
            }
        }
        countdownDialog.show(supportFragmentManager,"timer")
    }
 
    private fun startGame() {
        if (index < voiceList.size){
            Log.e(TAG,"开始答题:index=${index}")
            tv_sort.text = (index+1).toString()
            handler?.removeMessages(COUNT_DOWN)
            countTime = TIME //重置答题时间
            tv_tip.text = "准备听题"
            handler?.sendEmptyMessageDelayed(PLAY_VOICE,3000)
        }else{
            tv_sort.visibility = View.INVISIBLE
            tv_tip.text = ""
            tv_end.text = "已完成全部问题"
            tv_exit.text = "提交"
            handler?.removeMessages(STUDY_TIME)
        }
    }
 
    override fun onUpdate(db: Double, time: Long) {
    }
 
    override fun onStop(filePath: String?) {
    }
 
    override fun onStartPlay() {
        if (!data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])].completed){//首次播放
            tv_tip.text = "请在${countTime}s内选择答案!"
            handler?.sendEmptyMessageDelayed(COUNT_DOWN,1000)
        }
        iv1.gone()
        iv2.gone()
        iv_playing.visible()
    }
 
    override fun onFinishPlay() {
        iv1.visible()
        iv2.visible()
        iv_playing.gone()
        if (data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])].completed){ //当前题目已作答,3秒后进入下一题
            index++
            startGame()
        }
    }
 
}