lmw
2024-07-17 98faa1d5d540a7e4de4086ea11df3fd61b5c7ab9
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
package com.dollearn.student.ui.home
 
import android.os.Handler
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.GridLayoutManager
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.showAllowingStateLoss
import com.dollearn.student.R
import com.dollearn.student.dialog.TipDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Card
import com.dollearn.student.network.entity.MemoryBean
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.adapter.CardAdapter
import com.dollearn.student.utils.AudioUtils
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import kotlinx.android.synthetic.main.activity_memory.*
import org.greenrobot.eventbus.EventBus
 
class MemoryActivity : TransparentStatusBarActivity(), AudioUtils.OnAudioStatusUpdateListener {
    override fun setContentView() = R.layout.activity_memory
 
    private val data by lazy {
        intent.getParcelableExtra<MemoryBean>("data")!!
    }
    private val list = arrayListOf<Card>()
    private val adapter = CardAdapter(list)
 
    private var lastTime = 600 //剩余秒
 
    private var totalCount = 0
    private var rightCount = 0
 
    private var handler: Handler? = null
    var time = 0 //学习秒数
 
 
    private val PLAY_VOICE = 1
    private val STUDY_TIME = 2
 
    val PLAY_RIGHT = 4
    val PLAY_ERROR = 5
 
    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 var index = 0
 
    var playing = false
 
    override fun initClick() {
        tv_exit.setOnClickListener {
            if (tv_exit.text == "提交"){
                val right = list.filter { !it.isBack }.size / 2
                val total = data.photoList.size
                val score = right*data.data.answerIntegral/total
                ResultActivity.startResult(this,0,0,0,7, total, right,score,time,data!!.data.id)
                finish()
            }else
                onBackPressed()
        }
    }
 
    override fun initView() {
        player.setOnAudioStatusUpdateListener(this)
        player.stopPlayMusic()
        handler = object : Handler(android.os.Looper.getMainLooper()){
            override fun handleMessage(msg: android.os.Message) {
                super.handleMessage(msg)
                when(msg.what){
                    STUDY_TIME->{
                        time++
                        lastTime --
                        tv_timer.text = "${lastTime}S"
                        if (lastTime == 0){
                            val tipDialog = TipDialog()
                            tipDialog.arguments = bundleOf("msg" to "答题时间已结束,停止作答!","isAlert" to true,"ok" to "查看成绩")
                            tipDialog.setDismissCallback(object :TipDialog.OnDismiss{
                                override fun onDismiss() {
                                    val right = list.filter { !it.isBack }.size / 2
                                    val total = data.photoList.size
                                    val score = right*data.data.answerIntegral/total
                                    ResultActivity.startResult(this@MemoryActivity,0,0,0,7,total,right
                                        ,score,time,data.data.id)
                                    finish()
                                }
                            })
                            tipDialog.showAllowingStateLoss(supportFragmentManager,"finish")
                        }else
                            sendEmptyMessageDelayed(STUDY_TIME,1000)
                    }
                    PLAY_VOICE->{
                        player.startPlayMusic(this@MemoryActivity,list[index].url)
                    }
                    PLAY_RIGHT->{
                        rightPlaying = true
                        player.startPlayMusic(this@MemoryActivity,rightVoice)
                    }
                    PLAY_ERROR->{
                        errorPlaying = true
                        player.startPlayMusic(this@MemoryActivity,errorVoice)
                    }
                }
            }
        }
 
        lastTime = data.data.answerTime //倒计时时间
        tv_timer.text = "${lastTime}S"
        handler?.sendEmptyMessageDelayed(STUDY_TIME,1000)
        rv_list.layoutManager = GridLayoutManager(this,5)
        rv_list.adapter = adapter
        list.addAll(data.photoList.map { Card(it.id,1,it.photo) })
        list.addAll(data.voiceList.map { Card(it.id,2,it.voice) })
        list.shuffle()
        adapter.notifyDataSetChanged()
    }
 
    /**
     * 翻开卡片
     */
    fun flyover(position:Int){
        val card = list[position]
        if (card.type == 2){ //如果是语音 先播放语音
            index = position
            handler?.sendEmptyMessage(PLAY_VOICE)
        }else{ //不是语音直接比较结果
            checkResult()
        }
    }
 
    private fun checkResult(){
        if (adapter.position1!=-1&&adapter.position2!=-1){ //已经翻开2张卡片,比较结果是否匹配
            totalCount++
            val card1 = list[adapter.position1]
            val card2 = list[adapter.position2]
            if (card1.type!=card2.type&&card1.id == card2.id){//匹配成功
                handler?.sendEmptyMessage(PLAY_RIGHT)
                rightCount++
                tv_timer.postDelayed({
                    if (list.filter { !it.isBack }.size == list.size){ //全部答对
                        tv_exit.text = "提交"
                        handler?.removeMessages(STUDY_TIME) //停止计时
                    }
                },500)
            }else{
                handler?.sendEmptyMessage(PLAY_ERROR)
            }
        }
    }
 
    override fun onBackPressed() {
        HttpManager.exitGameOrStory(time).request(this){ _, _->
            super.onBackPressed()
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        handler?.removeMessages(PLAY_VOICE)
        handler?.removeMessages(STUDY_TIME)
        handler = null
        EventBus.getDefault().post(EmptyEvent(Const.EventCode.STOP_TIMER))
    }
 
    override fun onPause() {
        super.onPause()
        player.stopPlayMusic()
    }
 
    override fun onUpdate(db: Double, time: Long) {
 
    }
 
    override fun onStop(filePath: String?) {
    }
 
    override fun onStartPlay() {
        playing = true
    }
 
    override fun onFinishPlay() {
        playing = false
        if (errorPlaying){
            errorPlaying = false
            tv_timer.postDelayed({
                adapter.bothToBack()
            },500)
        }else if (rightPlaying){
            rightPlaying = false
            val card1 = list[adapter.position1]
            if (card1.type == 2)
                index = adapter.position1
            else
                index = adapter.position2
            adapter.releaseHolder()
            handler?.sendEmptyMessage(PLAY_VOICE)
        }else{
            checkResult()
        }
    }
}